Allow for skipping update check

This commit is contained in:
Kevin Bost
2024-09-15 21:26:22 -07:00
committed by Caelan
parent b282ae0b90
commit 3c23c1eedd
3 changed files with 23 additions and 10 deletions

View File

@@ -1,4 +1,4 @@
using System.ComponentModel;
using System.ComponentModel;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
@@ -38,6 +38,11 @@ public class Program
.SetRecursive(true)
.SetDescription("'yes' by instead of 'no' in non-interactive prompts.");
public static CliOption<bool> SkipUpdatesOption { get; }
= new CliOption<bool>("--skip-updates")
.SetRecursive(true)
.SetDescription("Skip update checks");
public static CliDirective WindowsDirective { get; } = new CliDirective("win") {
Description = "Show and run Windows specific commands."
};
@@ -61,6 +66,7 @@ public class Program
rootCommand.Options.Add(LegacyConsoleOption);
rootCommand.Options.Add(YesOption);
rootCommand.Options.Add(VerboseOption);
rootCommand.Options.Add(SkipUpdatesOption);
rootCommand.Directives.Add(WindowsDirective);
rootCommand.Directives.Add(LinuxDirective);
rootCommand.Directives.Add(OsxDirective);
@@ -75,6 +81,7 @@ public class Program
bool directiveWin = parseResult.GetResult(WindowsDirective) != null;
bool directiveLinux = parseResult.GetResult(LinuxDirective) != null;
bool directiveOsx = parseResult.GetResult(OsxDirective) != null;
bool skipUpdates = parseResult.GetValue(SkipUpdatesOption);
rootCommand.TreatUnmatchedTokensAsErrors = true;
var builder = Host.CreateEmptyApplicationBuilder(new HostApplicationBuilderSettings {
@@ -102,7 +109,7 @@ public class Program
targetOs = RuntimeOs.OSX;
}
builder.Services.AddSingleton(new VelopackDefaults(defaultYes, targetOs));
builder.Services.AddSingleton(new VelopackDefaults(defaultYes, targetOs, skipUpdates));
var host = builder.Build();
var provider = host.Services;
@@ -162,12 +169,12 @@ public class Program
rootCommand.Add(flowCommand);
var cli = new CliConfiguration(rootCommand);
return await cli.InvokeAsync(args);
return await cli.InvokeAsync(args);
static void HideCommand(CliCommand command) => command.Hidden = true;
}
private static void SetupConfig(IHostApplicationBuilder builder)
private static void SetupConfig(HostApplicationBuilder builder)
{
//builder.Configuration.AddJsonFile("vpk.json", optional: true);
builder.Configuration.AddEnvironmentVariables("VPK_");
@@ -176,7 +183,7 @@ public class Program
builder.Services.AddTransient(s => s.GetService<ILoggerFactory>().CreateLogger("vpk"));
}
private static void SetupLogging(IHostApplicationBuilder builder, bool verbose, bool legacyConsole)
private static void SetupLogging(HostApplicationBuilder builder, bool verbose, bool legacyConsole)
{
var levelSwitch = new LoggingLevelSwitch {
MinimumLevel = verbose ? LogEventLevel.Debug : LogEventLevel.Information
@@ -185,7 +192,7 @@ public class Program
.MinimumLevel.ControlledBy(levelSwitch)
.MinimumLevel.Override("Microsoft", LogEventLevel.Warning)
.MinimumLevel.Override("System", LogEventLevel.Warning);
if (legacyConsole) {
// spectre can have issues with redirected output, so we disable it.
builder.Services.AddSingleton<IFancyConsole, BasicConsole>();
@@ -261,7 +268,7 @@ public static class ProgramCommandExtensions
command.Initialize(logLevelSwitch);
logger.LogInformation($"[bold]{Program.INTRO}[/]");
var updateCheck = new UpdateChecker(logger);
var updateCheck = new UpdateChecker(logger, defaults);
await updateCheck.CheckForUpdates();
command.SetProperties(ctx, config, defaults.TargetOs);

View File

@@ -6,16 +6,20 @@ namespace Velopack.Vpk.Updates;
public class UpdateChecker
{
private readonly ILogger _logger;
private readonly VelopackDefaults _defaults;
private IPackageSearchMetadata _cache;
public UpdateChecker(ILogger logger)
public UpdateChecker(ILogger logger, VelopackDefaults defaults)
{
_logger = logger;
_defaults = defaults;
}
public async Task<bool> CheckForUpdates()
{
if (_defaults.SkipUpdates) return false;
try {
var myVer = VelopackRuntimeInfo.VelopackNugetVersion;
var isPre = myVer.IsPrerelease || myVer.HasMetadata;

View File

@@ -2,16 +2,18 @@
public record VelopackDefaults
{
public bool SkipUpdates { get; }
public bool DefaultPromptValue { get; }
public RuntimeOs TargetOs { get; }
public VelopackDefaults(bool defaultPromptValue)
: this(defaultPromptValue, VelopackRuntimeInfo.SystemOs)
: this(defaultPromptValue, VelopackRuntimeInfo.SystemOs, true)
{
}
public VelopackDefaults(bool defaultPromptValue, RuntimeOs targetOs)
public VelopackDefaults(bool defaultPromptValue, RuntimeOs targetOs, bool skipUpdates)
{
SkipUpdates = skipUpdates;
DefaultPromptValue = defaultPromptValue;
TargetOs = targetOs;
}