diff --git a/src/Program.cs b/src/Program.cs index 5f197e0..c17fdbf 100644 --- a/src/Program.cs +++ b/src/Program.cs @@ -1,58 +1,93 @@ -using Cocona; +using System.ComponentModel; +using System.Diagnostics.CodeAnalysis; using Spectre.Console; +using Spectre.Console.Cli; -var app = CoconaLiteApp.Create(); +var app = new CommandApp(); +app.Configure(x => x.SetApplicationName("script-launcher")); +return app.Run(args); -app.AddCommand(RootCommand); -app.Run(); - -static async Task RootCommand( - [Option("extensions", new[] { 'x' }, Description = "Comma separated list of script extensions")] string? extensions, - [Option("depth", new[] { 'd' }, Description = "Search depth")] int depth = 1, - [Option("elevated", new[] { 'e' }, Description = "Run with elevated privileges")] bool elevated = false, - [Option("group", new[] { 'g' }, Description = "Group scripts by folder")] bool group = false, - [Option("brief", new[] { 'b' }, Description = "Show brief information")] bool brief = false, - [Argument(Name = "Directory", Description = "Starting directory")] string directory = ".") +sealed class RootCommand : AsyncCommand { - if (!Directory.Exists(directory)) + private const int Failure = 1; + private const int Success = 0; + + public override async Task ExecuteAsync( + [NotNull] CommandContext context, + [NotNull] RootCommandSettings settings + ) { - AnsiConsole.Markup($"[red]The directory '{directory}' does not exist.[/]"); - Environment.ExitCode = 1; - return; - } - - FileInfo[] files; - var finder = new ScriptFinder(extensions, directory, depth); - - if (group) - { - var dict = finder.GetScriptsByDirectory(); - - if (dict.Count == 0) + if (!Directory.Exists(settings.Directory)) { - AnsiConsole.Markup($"[red]No scripts script files found in '{finder.RootDirectory}' with extensions '{string.Join(", ", finder.Extensions)}'[/]"); - Environment.ExitCode = 1; - return; + AnsiConsole.Markup($"[red]The directory '{settings.Directory}' does not exist.[/]"); + // Environment.ExitCode = 1; + return Failure; } - var dirPrompt = PromptConstructor.GetDirectoryPrompt(dict.Keys.ToArray()); - var directoryInfo = AnsiConsole.Prompt(dirPrompt); - files = dict[directoryInfo]; - } - else - { - files = finder.GetScripts(); - } + FileInfo[] files; + var finder = new ScriptFinder(settings.Extensions, settings.Directory, settings.Depth); - if (files.Length == 0) - { - AnsiConsole.Markup($"[red]No scripts script files found in '{finder.RootDirectory}' with extensions '{string.Join(", ", finder.Extensions)}'[/]"); - Environment.ExitCode = 1; - return; + if (settings.Group) + { + var dict = finder.GetScriptsByDirectory(); + + if (dict.Count == 0) + { + AnsiConsole.Markup( + $"[red]No scripts script files found in '{finder.RootDirectory}' with extensions '{string.Join(", ", finder.Extensions)}'[/]" + ); + return Failure; + } + + var dirPrompt = PromptConstructor.GetDirectoryPrompt(dict.Keys.ToArray()); + var directoryInfo = AnsiConsole.Prompt(dirPrompt); + files = dict[directoryInfo]; + } + else + { + files = finder.GetScripts(); + } + + if (files.Length == 0) + { + AnsiConsole.Markup( + $"[red]No scripts script files found in '{finder.RootDirectory}' with extensions '{string.Join(", ", finder.Extensions)}'[/]" + ); + return Failure; + } + + var prompt = PromptConstructor.GetScriptPrompt(files, settings.Brief); + var scripts = AnsiConsole.Prompt(prompt); + + await ScriptExecutor.ExecAsync(scripts, settings.Elevated); + + return Success; } - - var prompt = PromptConstructor.GetScriptPrompt(files, brief); - var scripts = AnsiConsole.Prompt(prompt); - - await ScriptExecutor.ExecAsync(scripts, elevated); +} + +internal class RootCommandSettings : CommandSettings +{ + [Description("Comma separated list of script extensions")] + [CommandOption("-x|--extensions")] + public string? Extensions { get; init; } + + [Description("Search depth")] + [CommandOption("-d|--depth")] + public int Depth { get; init; } = 1; + + [Description("Run with elevated privileges")] + [CommandOption("-e|--elevated")] + public bool Elevated { get; init; } = false; + + [Description("Group scripts by folder")] + [CommandOption("-g|--group")] + public bool Group { get; init; } = false; + + [Description("Show brief information")] + [CommandOption("-b|--brief")] + public bool Brief { get; init; } = false; + + [Description("Starting directory (Default: .)")] + [CommandArgument(0, "")] + public string Directory { get; init; } = "."; } diff --git a/src/ScriptLauncher.csproj b/src/ScriptLauncher.csproj index df35267..1e68697 100644 --- a/src/ScriptLauncher.csproj +++ b/src/ScriptLauncher.csproj @@ -5,14 +5,14 @@ net6.0 enable enable - 0.1.0 + 0.1.1 true script-launcher - +