feat: Dynamic command loader, default command with module registry

This commit is contained in:
2025-04-20 12:38:27 +02:00
parent a7cae86254
commit cea3d11a41
13 changed files with 373 additions and 3 deletions

View File

@ -0,0 +1,26 @@
using Automancer.Command.Image;
using Automancer.Common;
using Spectre.Console.Cli;
namespace Automancer.Command.Container;
public class Module : ICommandModuleWithRegistry
{
public void Configure(IConfigurator config)
{
// No implementation needed here
}
public void Configure(IConfigurator config, CommandRegistry registry)
{
config.AddBranch("container", container =>
{
var description = "Container operations";
container.SetDescription(description);
registry.Add("container", description);
container.AddCommand<PsCommand>("ps").WithDescription("List containers");
});
}
}

View File

@ -0,0 +1,31 @@
using System.ComponentModel;
using Spectre.Console.Cli;
namespace Automancer.Command.Image;
public class PsSettings: CommandSettings
{
[CommandArgument(0, "[path]")]
[Description("Path to the Dockerfile")]
public string? Path { get; set; }
[CommandOption("-t|--tag")]
[Description("Tag to use for the image")]
public string? Tag { get; set; }
[CommandOption("-f|--file")]
[Description("Path to the Dockerfile")]
public string? File { get; set; }
}
public class PsCommand: Command<BuldSettings>
{
public override int Execute(CommandContext context, BuldSettings settings)
{
Console.WriteLine($"Building image from {settings.Path} with tag {settings.Tag}");
return 0;
}
}

View File

@ -0,0 +1,34 @@
using System.ComponentModel;
using Spectre.Console.Cli;
namespace Automancer.Command.Image;
public class BuldSettings: CommandSettings
{
[CommandArgument(0, "[path]")]
[Description("Path to the Dockerfile")]
public string? Path { get; set; }
[CommandOption("-t|--tag")]
[Description("Tag to use for the image")]
public string? Tag { get; set; }
[CommandOption("-f|--file")]
[Description("Path to the Dockerfile")]
public string? File { get; set; }
[CommandOption("--push")]
[Description("Push the image after building it")]
public bool Push { get; set; }
}
public class BuildCommand : Command<BuldSettings>
{
public override int Execute(CommandContext context, BuldSettings settings)
{
Console.WriteLine($"Building image from {settings.Path} with tag {settings.Tag}");
return 0;
}
}

25
Commands/Image/Module.cs Normal file
View File

@ -0,0 +1,25 @@
using Automancer.Common;
using Spectre.Console.Cli;
namespace Automancer.Command.Image;
public class Module : ICommandModuleWithRegistry
{
public void Configure(IConfigurator config)
{
// No implementation needed here
}
public void Configure(IConfigurator config, CommandRegistry registry)
{
config.AddBranch("image", image => {
var description = "Docker/podman image operations";
image.SetDescription(description);
registry.Add("image", description);
image.AddCommand<BuildCommand>("build").WithDescription("Build a docker image");
image.AddCommand<PushCommand>("push").WithDescription("Push a docker image");
});
}
}

View File

@ -0,0 +1,30 @@
using System.ComponentModel;
using Spectre.Console.Cli;
namespace Automancer.Command.Image;
public class PushSettings: CommandSettings
{
[CommandArgument(0, "[path]")]
[Description("Path to the Dockerfile")]
public string? Path { get; set; }
[CommandOption("-t|--tag")]
[Description("Tag to use for the image")]
public string? Tag { get; set; }
[CommandOption("-f|--file")]
[Description("Path to the Dockerfile")]
public string? File { get; set; }
}
public class PushCommand : Command<BuldSettings>
{
public override int Execute(CommandContext context, BuldSettings settings)
{
Console.WriteLine($"Building image from {settings.Path} with tag {settings.Tag}");
return 0;
}
}

38
Commands/RootCommand.cs Normal file
View File

@ -0,0 +1,38 @@
using Automancer.Common;
using Spectre.Console;
using Spectre.Console.Cli;
using System.Diagnostics.CodeAnalysis;
namespace Automancer.Commands;
public class RootCommand : Spectre.Console.Cli.Command
{
private readonly CommandRegistry _registry;
public RootCommand(CommandRegistry registry)
{
_registry = registry;
}
public override int Execute([NotNull] CommandContext context)
{
AnsiConsole.Write(
new FigletText("AUTOMANCER")
.Centered()
.Color(Color.Cyan1));
AnsiConsole.MarkupLine("[bold]Automancer[/] is a modular CLI automation tool. It is designed to be extensible and easy to use.");
AnsiConsole.MarkupLine("Use [green]--help[/] or [green]<command> --help[/] to get started.");
var table = new Table().Border(TableBorder.Rounded);
table.AddColumn("[bold yellow]Module[/]");
table.AddColumn("[bold yellow]Description[/]");
foreach (var cmd in _registry.Commands.OrderBy(c => c.Path))
{
table.AddRow($"[green]{cmd.Path}[/]", cmd.Description ?? "[dim]n/a[/]");
}
AnsiConsole.Write(table);
return 0;
}
}