Skip to content

Commit 1e1fb14

Browse files
committed
Check for updates once a day max unless forced
The --version will always check. Otherwise, we'll check at most once a day to improve performance in repetitive usage. Fixes #54
1 parent b0cab3f commit 1e1fb14

File tree

2 files changed

+19
-4
lines changed

2 files changed

+19
-4
lines changed

src/dotnet-openai/AppExtensions.cs

+18-4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using NuGet.Configuration;
1+
using DotNetConfig;
2+
using NuGet.Configuration;
23
using NuGet.Protocol.Core.Types;
34
using NuGet.Versioning;
45
using Spectre.Console;
@@ -31,7 +32,7 @@ public static async Task<int> RunWithUpdatesAsync(this ICommandApp app, string[]
3132
/// </summary>
3233
public static async Task ShowUpdatesAsync(this ICommandApp app, string[] args)
3334
{
34-
if (await GetUpdatesAsync(args) is { Length: > 0 } messages)
35+
if (await GetUpdatesAsync(args, true) is { Length: > 0 } messages)
3536
{
3637
foreach (var message in messages)
3738
AnsiConsole.MarkupLine(message);
@@ -48,18 +49,29 @@ public static void ShowVersion(this ICommandApp app)
4849
AnsiConsole.MarkupLine($"[link]{ThisAssembly.Git.Url}/releases/tag/{ThisAssembly.Project.BuildRef}[/]");
4950
}
5051

51-
static async Task<string[]> GetUpdatesAsync(string[] args)
52+
static async Task<string[]> GetUpdatesAsync(string[] args, bool forced = false)
5253
{
5354
if (args.Contains("-u") || args.Contains("--unattended"))
5455
return [];
5556

57+
var config = Config.Build(ConfigLevel.Global).GetSection(ThisAssembly.Project.ToolCommandName);
58+
59+
// Check once a day max
60+
if (!forced)
61+
{
62+
var lastCheck = config.GetDateTime("checked") ?? DateTime.UtcNow.AddDays(-2);
63+
// if it's been > 24 hours since the last check, we'll check again
64+
if (lastCheck > DateTime.UtcNow.AddDays(-1))
65+
return [];
66+
}
67+
5668
// We check from a different feed in this case.
5769
var civersion = ThisAssembly.Project.VersionPrefix.StartsWith("42.42.");
5870

5971
var providers = Repository.Provider.GetCoreV3();
6072
var repository = new SourceRepository(new PackageSource(
6173
// use CI feed rather than production feed depending on which version we're using
62-
civersion ?
74+
civersion && !string.IsNullOrEmpty(ThisAssembly.Project.SLEET_FEED_URL) ?
6375
ThisAssembly.Project.SLEET_FEED_URL :
6476
"https://api.nuget.org/v3/index.json"), providers);
6577
var resource = await repository.GetResourceAsync<PackageMetadataResource>();
@@ -80,6 +92,8 @@ static async Task<string[]> GetUpdatesAsync(string[] args)
8092
.Select(x => x.Version)
8193
.FirstOrDefault();
8294

95+
config.SetDateTime("checked", DateTime.UtcNow);
96+
8397
if (update != null)
8498
{
8599
return [

src/dotnet-openai/dotnet-openai.csproj

+1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
<ItemGroup>
1717
<PackageReference Include="Devlooped.JQ" Version="1.7.1.8" />
18+
<PackageReference Include="DotNetConfig" Version="1.2.0" />
1819
<PackageReference Include="Humanizer.Core" Version="2.14.1" />
1920
<PackageReference Include="Microsoft.Extensions.Configuration" Version="8.0.0" />
2021
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="8.0.0" />

0 commit comments

Comments
 (0)