2023-05-26 16:28:48 +02:00
|
|
|
using System.ComponentModel;
|
|
|
|
|
|
|
|
using Spectre.Console;
|
|
|
|
using Spectre.Console.Cli;
|
|
|
|
|
|
|
|
namespace ScriptLauncher;
|
|
|
|
|
2023-11-14 19:10:31 +01:00
|
|
|
public sealed class RootCommand : AsyncCommand<RootCommandSettings>
|
2023-07-20 14:29:47 +02:00
|
|
|
{
|
|
|
|
private const int Failure = 1;
|
|
|
|
private const int Success = 0;
|
|
|
|
|
|
|
|
public override async Task<int> ExecuteAsync(
|
|
|
|
CommandContext context,
|
|
|
|
RootCommandSettings settings
|
|
|
|
)
|
2023-05-26 16:28:48 +02:00
|
|
|
{
|
2023-11-14 19:10:31 +01:00
|
|
|
ArgumentNullException.ThrowIfNull(settings);
|
|
|
|
|
2023-07-20 14:29:47 +02:00
|
|
|
if (!Directory.Exists(settings.Directory))
|
2023-05-26 16:28:48 +02:00
|
|
|
{
|
2023-07-20 14:29:47 +02:00
|
|
|
AnsiConsole.Markup($"[red]The directory '{settings.Directory}' does not exist.[/]");
|
|
|
|
return Failure;
|
|
|
|
}
|
2023-05-26 16:28:48 +02:00
|
|
|
|
2023-07-20 14:29:47 +02:00
|
|
|
FileInfo[] files;
|
|
|
|
var finder = new ScriptFinder(settings.Extensions, settings.Directory, settings.Depth);
|
2023-05-26 16:28:48 +02:00
|
|
|
|
2023-07-20 14:29:47 +02:00
|
|
|
if (settings.Group)
|
|
|
|
{
|
|
|
|
var dict = finder.GetScriptsByDirectory();
|
2023-05-26 16:28:48 +02:00
|
|
|
|
2023-07-20 14:29:47 +02:00
|
|
|
if (dict.Count == 0)
|
2023-05-26 16:28:48 +02:00
|
|
|
{
|
|
|
|
AnsiConsole.Markup(
|
|
|
|
$"[red]No scripts script files found in '{finder.RootDirectory}' with extensions '{string.Join(", ", finder.Extensions)}'[/]"
|
|
|
|
);
|
|
|
|
return Failure;
|
|
|
|
}
|
|
|
|
|
2023-07-20 14:29:47 +02:00
|
|
|
var dirPrompt = PromptConstructor.GetDirectoryPrompt(dict.Keys.ToArray());
|
|
|
|
var directoryInfo = AnsiConsole.Prompt(dirPrompt);
|
|
|
|
files = dict[directoryInfo];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
files = finder.GetScripts();
|
2023-05-26 16:28:48 +02:00
|
|
|
}
|
|
|
|
|
2023-07-20 14:29:47 +02:00
|
|
|
if (files.Length == 0)
|
|
|
|
{
|
|
|
|
AnsiConsole.Markup(
|
|
|
|
$"[red]No scripts script files found in '{finder.RootDirectory}' with extensions '{string.Join(", ", finder.Extensions)}'[/]"
|
|
|
|
);
|
|
|
|
return Failure;
|
|
|
|
}
|
2023-05-26 16:28:48 +02:00
|
|
|
|
2023-07-20 14:29:47 +02:00
|
|
|
var prompt = PromptConstructor.GetScriptPrompt(files, settings.Brief);
|
|
|
|
var scripts = AnsiConsole.Prompt(prompt);
|
2023-05-26 16:28:48 +02:00
|
|
|
|
2023-07-20 14:29:47 +02:00
|
|
|
try
|
|
|
|
{
|
|
|
|
await ScriptExecutor.ExecAsync(scripts, settings.Elevated).ConfigureAwait(false);
|
|
|
|
}
|
|
|
|
catch (Exception ex)
|
|
|
|
when (ex is Win32Exception or InvalidOperationException or PlatformNotSupportedException
|
|
|
|
)
|
|
|
|
{
|
|
|
|
AnsiConsole.Markup($"[red]{ex.Message}[/]");
|
|
|
|
return Failure;
|
|
|
|
}
|
2023-05-26 16:28:48 +02:00
|
|
|
|
2023-07-20 14:29:47 +02:00
|
|
|
return Success;
|
2023-05-26 16:28:48 +02:00
|
|
|
}
|
2023-07-20 14:29:47 +02:00
|
|
|
}
|
|
|
|
|
2023-11-14 19:10:31 +01:00
|
|
|
public sealed class RootCommandSettings : CommandSettings
|
2023-07-20 14:29:47 +02:00
|
|
|
{
|
|
|
|
[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; } = ".";
|
2023-11-14 19:10:31 +01:00
|
|
|
}
|