From f70ff1f358374f60f71991c36676ba9212c226ed Mon Sep 17 00:00:00 2001 From: Daryl Ronningen Date: Wed, 16 Nov 2022 22:45:25 -0800 Subject: [PATCH] add support for just specifying service name --- netsd/Handlers/Start.cs | 20 +++++++++++++++----- netsd/Program.cs | 26 +++++++++++++++++++++----- netsd/Protos/Start.proto | 2 +- 3 files changed, 37 insertions(+), 11 deletions(-) diff --git a/netsd/Handlers/Start.cs b/netsd/Handlers/Start.cs index 2d346a7..239f61f 100644 --- a/netsd/Handlers/Start.cs +++ b/netsd/Handlers/Start.cs @@ -3,16 +3,26 @@ using StartGrpc; namespace netsd.Handlers; -public static class Start +public class Start { - public static async Task StartAsync() + public Start(string? ServiceFilePath = null, string? ServiceName = null) { - var parseServiceFile = await ServiceParser.Parse("/lib/systemd/system/sddm.service"); - Console.WriteLine($"ExecStart: {parseServiceFile.ExecStart}"); + if (ServiceFilePath == null && ServiceName != null) + { + if (!Path.Exists($"/etc/netsd/services/{ServiceName}.service")) + { + // TODO: Add proper error messages (and most likely something like serilog for logging) + Environment.Exit(1); + return; + } + + ServiceFilePath = Path.GetFullPath($"/etc/netsd/services/{ServiceName}.service"); + } + using var channel = UDSConnector.CreateChannel(); var client = new StartRpc.StartRpcClient(channel); - var reply = client.StartService(new StartRequest{ Name = "Awoo" }); + var reply = client.StartService(new StartRequest{ Path = ServiceFilePath }); Console.WriteLine(reply.Success); } diff --git a/netsd/Program.cs b/netsd/Program.cs index b785304..cd0093a 100644 --- a/netsd/Program.cs +++ b/netsd/Program.cs @@ -2,17 +2,33 @@ using System.CommandLine; using netsd.Handlers; var initCommand = new Command("init", "Run and bring up all default services"); + var startCommand = new Command("start", "Starts up a netsd service"); +var startServiceName = new Argument("file", "The name of the service you want to start"); +var startServicePath = new Option("--file", "The path to the service file"); + +startServiceName.SetDefaultValue(""); +startServicePath.AddAlias("-f"); + +startCommand.AddArgument(startServiceName); +startCommand.AddOption(startServicePath); var rootCommand = new RootCommand("Linux service manager written in C#"); rootCommand.AddCommand(initCommand); rootCommand.AddCommand(startCommand); -initCommand.SetHandler(() => -{ - new Init(); -}); +initCommand.SetHandler(() => new Init()); -startCommand.SetHandler(async () => await Start.StartAsync()); +startCommand.SetHandler((file, name) => +{ + if (name.Length != 0) + { + new Start(ServiceName: name); + } + else + { + new Start(ServiceFilePath: file.FullName); + } +}, startServicePath, startServiceName); return await rootCommand.InvokeAsync(args); diff --git a/netsd/Protos/Start.proto b/netsd/Protos/Start.proto index a817f25..edf079f 100644 --- a/netsd/Protos/Start.proto +++ b/netsd/Protos/Start.proto @@ -9,7 +9,7 @@ service StartRpc { } message StartRequest { - string name = 1; + string path = 1; } message StartReply {