diff options
author | Wouter Tinus <wouter.tinus@gmail.com> | 2020-05-14 20:26:28 +0200 |
---|---|---|
committer | Wouter Tinus <wouter.tinus@gmail.com> | 2020-05-14 20:26:28 +0200 |
commit | 66ca970cbf63b871848f95b43e9066b7d934d148 (patch) | |
tree | 562b08a33da2548b4cb3cd3c543a0ca313e1174b /src | |
parent | dadac67bcc6fd786e34018a12eb361040528155d (diff) | |
download | letsencrypt-win-simple-66ca970cbf63b871848f95b43e9066b7d934d148.zip letsencrypt-win-simple-66ca970cbf63b871848f95b43e9066b7d934d148.tar.gz letsencrypt-win-simple-66ca970cbf63b871848f95b43e9066b7d934d148.tar.bz2 |
improve logging for invalid command lines
Diffstat (limited to 'src')
7 files changed, 44 insertions, 23 deletions
diff --git a/src/main.lib/Configuration/BaseArgumentsProvider.cs b/src/main.lib/Configuration/BaseArgumentsProvider.cs index e365084..df42949 100644 --- a/src/main.lib/Configuration/BaseArgumentsProvider.cs +++ b/src/main.lib/Configuration/BaseArgumentsProvider.cs @@ -8,6 +8,7 @@ namespace PKISharp.WACS.Configuration public abstract class BaseArgumentsProvider<T> : IArgumentsProvider<T> where T : class, new() { private readonly FluentCommandLineParser<T> _parser; + public ILogService? Log { get; set; } public BaseArgumentsProvider() { @@ -67,31 +68,36 @@ namespace PKISharp.WACS.Configuration return false; } - public virtual bool Validate(ILogService log, T current, MainArguments main) + public virtual bool Validate(T current, MainArguments main) { if (main.Renew) { if (IsActive(current)) { - log.Error($"Renewal {(string.IsNullOrEmpty(Group)?"":$"{Group} ")}parameters cannot be changed during a renewal. Recreate/overwrite the renewal or edit the .json file if you want to make changes."); + Log?.Error($"Renewal {(string.IsNullOrEmpty(Group)?"":$"{Group} ")}parameters cannot be changed during a renewal. Recreate/overwrite the renewal or edit the .json file if you want to make changes."); return false; } } return true; } - bool IArgumentsProvider.Validate(ILogService log, object current, MainArguments main) => Validate(log, (T)current, main); + bool IArgumentsProvider.Validate(object current, MainArguments main) => Validate((T)current, main); public IEnumerable<ICommandLineOption> Configuration => _parser.Options; public ICommandLineParserResult GetParseResult(string[] args) => _parser.Parse(args); - public T GetResult(string[] args) + public T? GetResult(string[] args) { - _parser.Parse(args); + var result = _parser.Parse(args); + if (result.HasErrors) + { + Log?.Error(result.ErrorText); + return null; + } return _parser.Object; } - object IArgumentsProvider.GetResult(string[] args) => GetResult(args); + object? IArgumentsProvider.GetResult(string[] args) => GetResult(args); } }
\ No newline at end of file diff --git a/src/main.lib/Plugins/InstallationPlugins/IISWeb/IISWebArgumentsProvider.cs b/src/main.lib/Plugins/InstallationPlugins/IISWeb/IISWebArgumentsProvider.cs index f690e69..8b00da3 100644 --- a/src/main.lib/Plugins/InstallationPlugins/IISWeb/IISWebArgumentsProvider.cs +++ b/src/main.lib/Plugins/InstallationPlugins/IISWeb/IISWebArgumentsProvider.cs @@ -15,9 +15,9 @@ namespace PKISharp.WACS.Plugins.InstallationPlugins public override string Group => "Installation"; public override string Condition => "--installation iis"; - public override bool Validate(ILogService log, IISWebArguments current, MainArguments main) + public override bool Validate(IISWebArguments current, MainArguments main) { - if (!base.Validate(log, current, main)) + if (!base.Validate(current, main)) { return false; } @@ -27,13 +27,13 @@ namespace PKISharp.WACS.Plugins.InstallationPlugins { if (port < 1 || port > 65535) { - log.Error("Invalid --{param}, value should be between 1 and 65535", SslPortParameterName); + Log?.Error("Invalid --{param}, value should be between 1 and 65535", SslPortParameterName); return false; } } else { - log.Error("Invalid --{param}, value should be a number", SslPortParameterName); + Log?.Error("Invalid --{param}, value should be a number", SslPortParameterName); return false; } } @@ -41,7 +41,7 @@ namespace PKISharp.WACS.Plugins.InstallationPlugins { if (!IPAddress.TryParse(current.SSLIPAddress, out _)) { - log.Error("Invalid --{sslipaddress}", SslIpParameterName); + Log?.Error("Invalid --{sslipaddress}", SslIpParameterName); return false; } } diff --git a/src/main.lib/Services/ArgumentsParser.cs b/src/main.lib/Services/ArgumentsParser.cs index 20529f0..8f63a08 100644 --- a/src/main.lib/Services/ArgumentsParser.cs +++ b/src/main.lib/Services/ArgumentsParser.cs @@ -11,7 +11,7 @@ namespace PKISharp.WACS.Configuration private readonly string[] _args; private readonly IEnumerable<IArgumentsProvider> _providers; - public T GetArguments<T>() where T : class, new() + public T? GetArguments<T>() where T : class, new() { foreach (var provider in _providers) { @@ -52,14 +52,18 @@ namespace PKISharp.WACS.Configuration return false; } var mainProvider = _providers.OfType<IArgumentsProvider<MainArguments>>().First(); - if (mainProvider.Validate(_log, main, main)) + if (mainProvider.Validate(main, main)) { // Validate the others var others = _providers.Except(new[] { mainProvider }); foreach (var other in others) { var opt = other.GetResult(_args); - if (!other.Validate(_log, opt, main)) + if (opt == null) + { + return false; + } + if (!other.Validate(opt, main)) { return false; } diff --git a/src/main.lib/Services/ArgumentsService.cs b/src/main.lib/Services/ArgumentsService.cs index 685ca55..2ec6022 100644 --- a/src/main.lib/Services/ArgumentsService.cs +++ b/src/main.lib/Services/ArgumentsService.cs @@ -17,6 +17,10 @@ namespace PKISharp.WACS.Services if (_mainArguments == null) { _mainArguments = _parser.GetArguments<MainArguments>(); + if (_mainArguments == null) + { + _mainArguments = new MainArguments(); + } } return _mainArguments; } @@ -28,7 +32,7 @@ namespace PKISharp.WACS.Services _parser = parser; } - public T GetArguments<T>() where T : class, new() => _parser.GetArguments<T>(); + public T? GetArguments<T>() where T : class, new() => _parser.GetArguments<T>(); public async Task<string?> TryGetArgument(string? providedValue, IInputService input, string what, bool secret = false) => await TryGetArgument(providedValue, input, new[] { what }, secret); diff --git a/src/main.lib/Services/Interfaces/IArgumentsProvider.cs b/src/main.lib/Services/Interfaces/IArgumentsProvider.cs index 58c6a16..a6643ee 100644 --- a/src/main.lib/Services/Interfaces/IArgumentsProvider.cs +++ b/src/main.lib/Services/Interfaces/IArgumentsProvider.cs @@ -28,6 +28,11 @@ namespace PKISharp.WACS.Services bool Default { get; } /// <summary> + /// Reference to the logging service + /// </summary> + ILogService? Log { get; set; } + + /// <summary> /// Which options are available /// </summary> IEnumerable<ICommandLineOption> Configuration { get; } @@ -40,7 +45,7 @@ namespace PKISharp.WACS.Services /// <summary> /// Get the parsed result /// </summary> - object GetResult(string[] args); + object? GetResult(string[] args); /// <summary> /// Validate against the main arguments @@ -48,7 +53,7 @@ namespace PKISharp.WACS.Services /// <param name="current"></param> /// <param name="main"></param> /// <returns></returns> - bool Validate(ILogService log, object current, MainArguments main); + bool Validate(object current, MainArguments main); /// <summary> /// Are the arguments provided? @@ -58,12 +63,12 @@ namespace PKISharp.WACS.Services bool Active(object current); } - public interface IArgumentsProvider<T> : IArgumentsProvider where T : new() + public interface IArgumentsProvider<T> : IArgumentsProvider where T : class, new() { /// <summary> /// Get the parsed result /// </summary> - new T GetResult(string[] args); + new T? GetResult(string[] args); /// <summary> /// Validate against the main arguments @@ -71,6 +76,6 @@ namespace PKISharp.WACS.Services /// <param name="current"></param> /// <param name="main"></param> /// <returns></returns> - bool Validate(ILogService log, T current, MainArguments main); + bool Validate(T current, MainArguments main); } } diff --git a/src/main.lib/Services/Interfaces/IArgumentsService.cs b/src/main.lib/Services/Interfaces/IArgumentsService.cs index 4c00c51..08d2767 100644 --- a/src/main.lib/Services/Interfaces/IArgumentsService.cs +++ b/src/main.lib/Services/Interfaces/IArgumentsService.cs @@ -6,7 +6,7 @@ namespace PKISharp.WACS.Services public interface IArgumentsService { MainArguments MainArguments { get; } - T GetArguments<T>() where T : class, new(); + T? GetArguments<T>() where T : class, new(); bool Active { get; } bool Valid { get; } bool HasFilter(); diff --git a/src/main.lib/Services/PluginService.cs b/src/main.lib/Services/PluginService.cs index 404e65d..023f0f0 100644 --- a/src/main.lib/Services/PluginService.cs +++ b/src/main.lib/Services/PluginService.cs @@ -17,7 +17,7 @@ namespace PKISharp.WACS.Services private readonly List<Type> _argumentProviders; private readonly List<Type> _optionFactories; private readonly List<Type> _plugins; - + internal readonly ILogService _log; public IEnumerable<IArgumentsProvider> ArgumentsProviders() @@ -25,7 +25,9 @@ namespace PKISharp.WACS.Services return _argumentProviders.Select(x => { var c = x.GetConstructor(new Type[] { }); - return (IArgumentsProvider)c.Invoke(new object[] { }); + var ret = (IArgumentsProvider)c.Invoke(new object[] { }); + ret.Log = _log; + return ret; }).ToList(); } |