[feat] Abyssctl Basic functions

This commit is contained in:
acite
2025-10-05 16:48:54 +08:00
parent 50eae5e275
commit a0273e3334
22 changed files with 428 additions and 238 deletions

View File

@@ -2,8 +2,8 @@
using System.Net.Sockets;
using System.Reflection;
using System.Text;
using abyssctl.App.Attributes;
using abyssctl.App.Interfaces;
using abyssctl.App.Modules;
using abyssctl.Model;
using abyssctl.Static;
using CommandLine;
@@ -13,16 +13,18 @@ namespace abyssctl.App;
public class App
{
private static readonly string SocketPath = "ctl.sock";
private static readonly string SocketPath = Path.Combine(Path.GetTempPath(), "abyss-ctl.sock");
public static async Task<Ctl> CtlWriteRead(Ctl ctl)
public static async Task<Ctl> CtlWriteRead<T>(string[] param)
{
var attr = typeof(T).GetCustomAttribute<ModuleAttribute>()!;
var endPoint = new UnixDomainSocketEndPoint(SocketPath);
using var socket = new Socket(AddressFamily.Unix, SocketType.Stream, ProtocolType.Unspecified);
try
{
await socket.ConnectAsync(endPoint);
await socket.WriteBase64Async(Ctl.MakeBase64(ctl.Head, ctl.Params));
await socket.WriteBase64Async(Ctl.MakeBase64(attr.Head, param));
var s = Encoding.UTF8.GetString(
Convert.FromBase64String(await socket.ReadBase64Async()));
return JsonConvert.DeserializeObject<Ctl>(s)!;
@@ -41,18 +43,7 @@ public class App
{
return await Task.Run(() =>
{
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)
return Parser.Default.ParseArguments(args, ModuleAttribute.Modules)
.MapResult(
(object obj) =>
{

View File

@@ -0,0 +1,28 @@
using System.Reflection;
using abyssctl.App.Interfaces;
using CommandLine;
namespace abyssctl.App.Attributes;
[AttributeUsage(AttributeTargets.Class, Inherited = false, AllowMultiple = false)]
public class ModuleAttribute(int head) : Attribute
{
public int Head { get; } = head;
public static Type[] Modules
{
get
{
Assembly assembly = Assembly.GetExecutingAssembly();
const string targetNamespace = "abyssctl.App.Modules";
return 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(typeof(VerbAttribute), inherit: true))
.Where(t => t.IsDefined(typeof(ModuleAttribute), inherit: false))
.ToArray();
}
}
}

View File

@@ -1,22 +1,30 @@
using abyssctl.App.Attributes;
using abyssctl.App.Interfaces;
using abyssctl.Model;
using CommandLine;
namespace abyssctl.App.Modules;
[Module(100)]
[Verb("hello", HelpText = "Say hello to abyss server")]
public class HelloOptions: IOptions
{
[Option('r', "raw", Default = false, HelpText = "Show raw response.")]
public bool Raw { get; set; }
public async Task<int> Run()
{
var r = await App.CtlWriteRead(new Ctl
var r = await App.CtlWriteRead<HelloOptions>([]);
if (Raw)
{
Head = 100,
Params = []
});
Console.WriteLine($"Response Code: {r.Head}");
Console.WriteLine($"Params: {string.Join(",", r.Params)}");
Console.WriteLine($"Response Code: {r.Head}");
Console.WriteLine($"Params: {string.Join(",", r.Params)}");
}
else
{
Console.WriteLine($"Server: {string.Join(",", r.Params)}");
}
return 0;
}
}

View File

@@ -0,0 +1,20 @@
using abyssctl.App.Attributes;
using abyssctl.App.Interfaces;
using CommandLine;
namespace abyssctl.App.Modules;
[Module(103)]
[Verb("init", HelpText = "Initialize abyss server")]
public class InitOptions: IOptions
{
public async Task<int> Run()
{
var r = await App.CtlWriteRead<InitOptions>([]);
Console.WriteLine($"Response Code: {r.Head}");
Console.WriteLine($"Params: {string.Join(",", r.Params)}");
return 0;
}
}

View File

@@ -0,0 +1,26 @@
using abyssctl.App.Attributes;
using abyssctl.App.Interfaces;
using CommandLine;
namespace abyssctl.App.Modules;
[Module(104)]
[Verb("useradd", HelpText = "Add user")]
public class UserAddOptions: IOptions
{
[Option('u', "username", Required = true, HelpText = "Username for new user.")]
public string Username { get; set; } = "";
[Option('p', "privilege", Required = true, HelpText = "User privilege.")]
public int Privilege { get; set; }
public async Task<int> Run()
{
var r = await App.CtlWriteRead<UserAddOptions>([Username, Privilege.ToString()]);
Console.WriteLine($"Response Code: {r.Head}");
Console.WriteLine($"Params: {string.Join(",", r.Params)}");
return 0;
}
}

View File

@@ -1,8 +1,10 @@
using abyssctl.App.Attributes;
using abyssctl.App.Interfaces;
using CommandLine;
namespace abyssctl.App.Modules;
[Module(101)]
[Verb("ver", HelpText = "Get server version")]
public class VersionOptions: IOptions
{