Feat: Switch to Spectre.Console.Cli as CLI framework (#3)

This commit is contained in:
Marcello 2022-10-15 12:24:30 +02:00
parent 4ab62ff538
commit 1cf2e05a67
2 changed files with 84 additions and 49 deletions

View file

@ -1,58 +1,93 @@
using Cocona; using System.ComponentModel;
using System.Diagnostics.CodeAnalysis;
using Spectre.Console; using Spectre.Console;
using Spectre.Console.Cli;
var app = CoconaLiteApp.Create(); var app = new CommandApp<RootCommand>();
app.Configure(x => x.SetApplicationName("script-launcher"));
return app.Run(args);
app.AddCommand(RootCommand); sealed class RootCommand : AsyncCommand<RootCommandSettings>
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 = ".")
{ {
if (!Directory.Exists(directory)) private const int Failure = 1;
private const int Success = 0;
public override async Task<int> ExecuteAsync(
[NotNull] CommandContext context,
[NotNull] RootCommandSettings settings
)
{ {
AnsiConsole.Markup($"[red]The directory '{directory}' does not exist.[/]"); if (!Directory.Exists(settings.Directory))
Environment.ExitCode = 1;
return;
}
FileInfo[] files;
var finder = new ScriptFinder(extensions, directory, depth);
if (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)}'[/]"); AnsiConsole.Markup($"[red]The directory '{settings.Directory}' does not exist.[/]");
Environment.ExitCode = 1; // Environment.ExitCode = 1;
return; return Failure;
} }
var dirPrompt = PromptConstructor.GetDirectoryPrompt(dict.Keys.ToArray()); FileInfo[] files;
var directoryInfo = AnsiConsole.Prompt(dirPrompt); var finder = new ScriptFinder(settings.Extensions, settings.Directory, settings.Depth);
files = dict[directoryInfo];
}
else
{
files = finder.GetScripts();
}
if (files.Length == 0) if (settings.Group)
{ {
AnsiConsole.Markup($"[red]No scripts script files found in '{finder.RootDirectory}' with extensions '{string.Join(", ", finder.Extensions)}'[/]"); var dict = finder.GetScriptsByDirectory();
Environment.ExitCode = 1;
return; 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); internal class RootCommandSettings : CommandSettings
{
await ScriptExecutor.ExecAsync(scripts, elevated); [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, "<path>")]
public string Directory { get; init; } = ".";
} }

View file

@ -5,14 +5,14 @@
<TargetFramework>net6.0</TargetFramework> <TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings> <ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable> <Nullable>enable</Nullable>
<Version>0.1.0</Version> <Version>0.1.1</Version>
<PackAsTool>true</PackAsTool> <PackAsTool>true</PackAsTool>
<ToolCommandName>script-launcher</ToolCommandName> <ToolCommandName>script-launcher</ToolCommandName>
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<PackageReference Include="Cocona.Lite" Version="2.0.3" />
<PackageReference Include="Spectre.Console" Version="0.45.0" /> <PackageReference Include="Spectre.Console" Version="0.45.0" />
<PackageReference Include="Spectre.Console.Cli" Version="0.45.0" />
</ItemGroup> </ItemGroup>
</Project> </Project>