[feat] Abyssctl Automatic module discovery

This commit is contained in:
acite
2025-10-05 11:41:06 +08:00
parent af6dfbac8c
commit 50eae5e275
6 changed files with 47 additions and 34 deletions

View File

@@ -1,6 +1,8 @@
using System.Net.Sockets;
using System.Reflection;
using System.Text;
using abyssctl.App.Interfaces;
using abyssctl.App.Modules;
using abyssctl.Model;
using abyssctl.Static;
@@ -39,10 +41,24 @@ public class App
{
return await Task.Run(() =>
{
return Parser.Default.ParseArguments<HelloOptions, VersionOptions>(args)
Assembly assembly = Assembly.GetExecutingAssembly();
Type attributeType = typeof(VerbAttribute);
const string targetNamespace = "abyssctl.App.Modules";
var moduleTypes = assembly.GetTypes()
.Where(t => t is { IsClass: true, IsAbstract: false, IsInterface: false })
.Where(t => t.Namespace == targetNamespace)
.Where(t => typeof(IOptions).IsAssignableFrom(t))
.Where(t => t.IsDefined(attributeType, inherit: true))
.ToArray();
return Parser.Default.ParseArguments(args, moduleTypes)
.MapResult(
(HelloOptions opt) => HelloOptions.Run(opt),
(VersionOptions opt) => VersionOptions.Run(opt),
(object obj) =>
{
var s = (obj as IOptions)?.Run().GetAwaiter().GetResult();
return s!.Value;
},
_ => 1);
});
}

View File

@@ -0,0 +1,6 @@
namespace abyssctl.App.Interfaces;
public interface IOptions
{
public Task<int> Run();
}

View File

@@ -1,18 +1,20 @@
using abyssctl.App.Interfaces;
using abyssctl.Model;
using CommandLine;
namespace abyssctl.App.Modules;
[Verb("hello", HelpText = "Say hello to abyss server")]
public class HelloOptions
public class HelloOptions: IOptions
{
public static int Run(HelloOptions opts)
public async Task<int> Run()
{
var r = App.CtlWriteRead(new Ctl
var r = await App.CtlWriteRead(new Ctl
{
Head = 100,
Params = []
}).GetAwaiter().GetResult();
});
Console.WriteLine($"Response Code: {r.Head}");
Console.WriteLine($"Params: {string.Join(",", r.Params)}");
return 0;

View File

@@ -1,11 +1,12 @@
using abyssctl.App.Interfaces;
using CommandLine;
namespace abyssctl.App.Modules;
[Verb("ver", HelpText = "Get server version")]
public class VersionOptions
public class VersionOptions: IOptions
{
public static int Run(VersionOptions opts)
public async Task<int> Run()
{
Console.WriteLine("Version");
return 0;