diff --git a/Source/v2/Meadow.Cli/CommandErrors.cs b/Source/v2/Meadow.Cli/CommandErrors.cs index 20f2b598..314456f1 100644 --- a/Source/v2/Meadow.Cli/CommandErrors.cs +++ b/Source/v2/Meadow.Cli/CommandErrors.cs @@ -1,11 +1,10 @@ -namespace Meadow.CLI +namespace Meadow.CLI; + +public enum CommandExitCode { - public enum CommandErrors - { - Success = 0, - GeneralError = 1, - UserCancelled = 2, - FileNotFound = 3, - DirectoryNotFound = 4, - } + Success = 0, + GeneralError = 1, + UserCancelled = 2, + FileNotFound = 3, + DirectoryNotFound = 4, } \ No newline at end of file diff --git a/Source/v2/Meadow.Cli/CommandException.cs b/Source/v2/Meadow.Cli/CommandException.cs new file mode 100644 index 00000000..7d725a7a --- /dev/null +++ b/Source/v2/Meadow.Cli/CommandException.cs @@ -0,0 +1,37 @@ +namespace Meadow.CLI; + +/// +public class CommandException : CliFx.Exceptions.CommandException +{ + public CommandException(string message) + : base(message, (int)CommandExitCode.GeneralError, showHelp: false, innerException: null) + { } + + public CommandException(string message, bool showHelp) + : base(message, (int)CommandExitCode.GeneralError, showHelp, innerException: null) + { } + + public CommandException(string message, CommandExitCode exitCode) + : base(message, (int)exitCode, showHelp: false, innerException: null) + { } + + public CommandException(string message, CommandExitCode exitCode, bool showHelp) + : base(message, (int)exitCode, showHelp, innerException: null) + { } + + public CommandException(string message, Exception innerException) + : base(message, (int)CommandExitCode.GeneralError, showHelp: false, innerException) + { } + + public CommandException(string message, bool showHelp, Exception innerException) + : base(message, (int)CommandExitCode.GeneralError, showHelp, innerException) + { } + + public CommandException(string message, CommandExitCode exitCode, Exception innerException) + : base(message, (int)exitCode, showHelp: false, innerException: innerException) + { } + + public CommandException(string message, CommandExitCode exitCode, bool showHelp, Exception innerException) + : base(message, (int)exitCode, showHelp, innerException: innerException) + { } +} \ No newline at end of file diff --git a/Source/v2/Meadow.Cli/Commands/Current/App/AppBuildCommand.cs b/Source/v2/Meadow.Cli/Commands/Current/App/AppBuildCommand.cs index 92612bd1..a0c4a3b9 100644 --- a/Source/v2/Meadow.Cli/Commands/Current/App/AppBuildCommand.cs +++ b/Source/v2/Meadow.Cli/Commands/Current/App/AppBuildCommand.cs @@ -1,5 +1,4 @@ using CliFx.Attributes; -using CliFx.Exceptions; using Meadow.Package; using Microsoft.Extensions.Logging; @@ -32,7 +31,7 @@ protected override ValueTask ExecuteCommand() // is it a valid directory? if (!Directory.Exists(path)) { - throw new CommandException($"Invalid application path '{path}'", (int)CommandErrors.FileNotFound); + throw new CommandException($"Invalid application path '{path}'", CommandExitCode.FileNotFound); } } @@ -45,7 +44,7 @@ protected override ValueTask ExecuteCommand() if (!success) { - throw new CommandException($"Build failed.", (int)CommandErrors.GeneralError); + throw new CommandException("Build failed", CommandExitCode.GeneralError); } else { diff --git a/Source/v2/Meadow.Cli/Commands/Current/App/AppRunCommand.cs b/Source/v2/Meadow.Cli/Commands/Current/App/AppRunCommand.cs index 114f8fe2..6eefbde6 100644 --- a/Source/v2/Meadow.Cli/Commands/Current/App/AppRunCommand.cs +++ b/Source/v2/Meadow.Cli/Commands/Current/App/AppRunCommand.cs @@ -1,5 +1,4 @@ using CliFx.Attributes; -using CliFx.Exceptions; using Meadow.Hcom; using Meadow.Package; using Microsoft.Extensions.Logging; @@ -37,7 +36,7 @@ protected override async ValueTask ExecuteCommand() // is it a valid directory? if (!Directory.Exists(path)) { - throw new CommandException($"Invalid application path '{path}'", (int)CommandErrors.FileNotFound); + throw new CommandException($"Invalid application path '{path}'", CommandExitCode.FileNotFound); } } diff --git a/Source/v2/Meadow.Cli/Commands/Current/BaseCommand.cs b/Source/v2/Meadow.Cli/Commands/Current/BaseCommand.cs index f407178d..8684dac6 100644 --- a/Source/v2/Meadow.Cli/Commands/Current/BaseCommand.cs +++ b/Source/v2/Meadow.Cli/Commands/Current/BaseCommand.cs @@ -1,5 +1,4 @@ using CliFx; -using CliFx.Exceptions; using CliFx.Infrastructure; using Microsoft.Extensions.Logging; @@ -7,15 +6,17 @@ namespace Meadow.CLI.Commands.DeviceManagement; public abstract class BaseCommand : ICommand { - protected ILogger? Logger { get; } - protected ILoggerFactory? LoggerFactory { get; } - protected IConsole? Console { get; private set; } + private IConsole? _console; + + protected ILogger Logger { get; } + protected ILoggerFactory LoggerFactory { get; } + protected IConsole Console => _console ?? throw new InvalidOperationException("The Console property has not yet been initialized. It can only be used within in the ExecuteCommand() method."); protected CancellationToken CancellationToken { get; private set; } - public BaseCommand(ILoggerFactory? loggerFactory) + public BaseCommand(ILoggerFactory loggerFactory) { LoggerFactory = loggerFactory; - Logger = loggerFactory?.CreateLogger(); + Logger = loggerFactory.CreateLogger(); } protected abstract ValueTask ExecuteCommand(); @@ -24,28 +25,19 @@ public async ValueTask ExecuteAsync(IConsole console) { try { - Console = console; - CancellationToken = Console.RegisterCancellationHandler(); + _console = console; + CancellationToken = _console.RegisterCancellationHandler(); await ExecuteCommand(); } - catch (CommandException) - { - throw; - } - catch (Exception ex) + catch (Exception ex) when (ex is not CommandException && ex is not CliFx.Exceptions.CommandException) { - throw new CommandException( - message: ex.Message, - exitCode: (int)CommandErrors.GeneralError, - innerException: ex); + throw new CommandException(ex.Message, ex); } if (CancellationToken.IsCancellationRequested) { - throw new CommandException( - message: "Cancelled", - exitCode: (int)CommandErrors.UserCancelled); + throw new CommandException("Cancelled", CommandExitCode.UserCancelled); } } } \ No newline at end of file diff --git a/Source/v2/Meadow.Cli/Commands/Current/Cloud/ApiKey/CloudApiKeyCreateCommand.cs b/Source/v2/Meadow.Cli/Commands/Current/Cloud/ApiKey/CloudApiKeyCreateCommand.cs index 54d76c87..67007cf0 100644 --- a/Source/v2/Meadow.Cli/Commands/Current/Cloud/ApiKey/CloudApiKeyCreateCommand.cs +++ b/Source/v2/Meadow.Cli/Commands/Current/Cloud/ApiKey/CloudApiKeyCreateCommand.cs @@ -1,5 +1,4 @@ using CliFx.Attributes; -using CliFx.Exceptions; using Meadow.Cloud.Client; using Meadow.Cloud.Client.Identity; using Microsoft.Extensions.Logging; diff --git a/Source/v2/Meadow.Cli/Commands/Current/Cloud/ApiKey/CloudApiKeyDeleteCommand.cs b/Source/v2/Meadow.Cli/Commands/Current/Cloud/ApiKey/CloudApiKeyDeleteCommand.cs index c7514dae..231d2c0e 100644 --- a/Source/v2/Meadow.Cli/Commands/Current/Cloud/ApiKey/CloudApiKeyDeleteCommand.cs +++ b/Source/v2/Meadow.Cli/Commands/Current/Cloud/ApiKey/CloudApiKeyDeleteCommand.cs @@ -1,5 +1,4 @@ using CliFx.Attributes; -using CliFx.Exceptions; using Meadow.Cloud.Client; using Meadow.Cloud.Client.Identity; using Microsoft.Extensions.Logging; diff --git a/Source/v2/Meadow.Cli/Commands/Current/Cloud/ApiKey/CloudApiKeyListCommand.cs b/Source/v2/Meadow.Cli/Commands/Current/Cloud/ApiKey/CloudApiKeyListCommand.cs index d86ffc77..47d1f6fa 100644 --- a/Source/v2/Meadow.Cli/Commands/Current/Cloud/ApiKey/CloudApiKeyListCommand.cs +++ b/Source/v2/Meadow.Cli/Commands/Current/Cloud/ApiKey/CloudApiKeyListCommand.cs @@ -1,5 +1,4 @@ using CliFx.Attributes; -using CliFx.Exceptions; using Meadow.Cloud.Client; using Meadow.Cloud.Client.Identity; using Microsoft.Extensions.Logging; diff --git a/Source/v2/Meadow.Cli/Commands/Current/Cloud/ApiKey/CloudApiKeyUpdateCommand.cs b/Source/v2/Meadow.Cli/Commands/Current/Cloud/ApiKey/CloudApiKeyUpdateCommand.cs index e065a8c2..ecf11da6 100644 --- a/Source/v2/Meadow.Cli/Commands/Current/Cloud/ApiKey/CloudApiKeyUpdateCommand.cs +++ b/Source/v2/Meadow.Cli/Commands/Current/Cloud/ApiKey/CloudApiKeyUpdateCommand.cs @@ -1,5 +1,4 @@ using CliFx.Attributes; -using CliFx.Exceptions; using Meadow.Cloud.Client; using Meadow.Cloud.Client.Identity; using Microsoft.Extensions.Logging; diff --git a/Source/v2/Meadow.Cli/Commands/Current/Cloud/Collection/JsonDocumentBindingConverter.cs b/Source/v2/Meadow.Cli/Commands/Current/Cloud/Collection/JsonDocumentBindingConverter.cs index c9120958..de53ef3b 100644 --- a/Source/v2/Meadow.Cli/Commands/Current/Cloud/Collection/JsonDocumentBindingConverter.cs +++ b/Source/v2/Meadow.Cli/Commands/Current/Cloud/Collection/JsonDocumentBindingConverter.cs @@ -1,5 +1,4 @@ -using CliFx.Exceptions; -using CliFx.Extensibility; +using CliFx.Extensibility; using System.Text.Json; namespace Meadow.CLI.Commands.DeviceManagement; diff --git a/Source/v2/Meadow.Cli/Commands/Current/Cloud/Command/CloudCommandPublishCommand.cs b/Source/v2/Meadow.Cli/Commands/Current/Cloud/Command/CloudCommandPublishCommand.cs index c2d3b559..8738a321 100644 --- a/Source/v2/Meadow.Cli/Commands/Current/Cloud/Command/CloudCommandPublishCommand.cs +++ b/Source/v2/Meadow.Cli/Commands/Current/Cloud/Command/CloudCommandPublishCommand.cs @@ -1,5 +1,4 @@ using CliFx.Attributes; -using CliFx.Exceptions; using Meadow.Cloud.Client; using Meadow.Cloud.Client.Identity; using Microsoft.Extensions.Logging; diff --git a/Source/v2/Meadow.Cli/Commands/Current/Cloud/Package/CloudPackageCreateCommand.cs b/Source/v2/Meadow.Cli/Commands/Current/Cloud/Package/CloudPackageCreateCommand.cs index 49da8c19..d6af472b 100644 --- a/Source/v2/Meadow.Cli/Commands/Current/Cloud/Package/CloudPackageCreateCommand.cs +++ b/Source/v2/Meadow.Cli/Commands/Current/Cloud/Package/CloudPackageCreateCommand.cs @@ -1,5 +1,4 @@ using CliFx.Attributes; -using CliFx.Exceptions; using Meadow.Cloud.Client; using Meadow.Cloud.Client.Identity; using Meadow.Package; @@ -47,21 +46,21 @@ protected override async ValueTask ExecuteCommand() ProjectPath = Path.GetFullPath(ProjectPath); if (!Directory.Exists(ProjectPath)) { - throw new CommandException($"Directory not found '{ProjectPath}'. Check path to project file.", (int)CommandErrors.DirectoryNotFound); + throw new CommandException($"Directory not found '{ProjectPath}'. Check path to project file.", CommandExitCode.DirectoryNotFound); } // build Logger?.LogInformation($"Building {Configuration} version of application..."); if (!_packageManager.BuildApplication(ProjectPath, Configuration, true, CancellationToken)) { - throw new CommandException($"Build failed.", (int)CommandErrors.GeneralError); + throw new CommandException($"Build failed"); } var candidates = PackageManager.GetAvailableBuiltConfigurations(ProjectPath, "App.dll"); if (candidates.Length == 0) { - throw new CommandException($"Cannot find a compiled application at '{ProjectPath}'", (int)CommandErrors.FileNotFound); + throw new CommandException($"Cannot find a compiled application at '{ProjectPath}'", CommandExitCode.FileNotFound); } var store = _fileManager.Firmware["Meadow F7"]; @@ -86,7 +85,7 @@ protected override async ValueTask ExecuteCommand() } else { - throw new CommandException($"Package assembly failed.", (int)CommandErrors.GeneralError); + throw new CommandException($"Package assembly failed"); } } } \ No newline at end of file diff --git a/Source/v2/Meadow.Cli/Commands/Current/Config/ConfigCommand.cs b/Source/v2/Meadow.Cli/Commands/Current/Config/ConfigCommand.cs index b1e47b1f..c59f0346 100644 --- a/Source/v2/Meadow.Cli/Commands/Current/Config/ConfigCommand.cs +++ b/Source/v2/Meadow.Cli/Commands/Current/Config/ConfigCommand.cs @@ -1,5 +1,4 @@ using CliFx.Attributes; -using CliFx.Exceptions; using Microsoft.Extensions.Logging; namespace Meadow.CLI.Commands.DeviceManagement; diff --git a/Source/v2/Meadow.Cli/Commands/Legacy/FlashOsCommand.cs b/Source/v2/Meadow.Cli/Commands/Legacy/FlashOsCommand.cs index 3538247c..00fad646 100644 --- a/Source/v2/Meadow.Cli/Commands/Legacy/FlashOsCommand.cs +++ b/Source/v2/Meadow.Cli/Commands/Legacy/FlashOsCommand.cs @@ -1,5 +1,4 @@ using CliFx.Attributes; -using CliFx.Exceptions; using Microsoft.Extensions.Logging; namespace Meadow.CLI.Commands.DeviceManagement;