summary refs log tree commit diff
path: root/LibSystemdCli
diff options
context:
space:
mode:
authorRory& <root@rory.gay>2024-01-20 11:34:00 +0100
committerRory& <root@rory.gay>2024-01-20 11:34:00 +0100
commit8c15a67166e545169216eee8be7c44acf03a27cb (patch)
tree2667e4899e14bf66e2d31b7d33978d7d2dd679b4 /LibSystemdCli
parentJson logs (diff)
downloadSystemdCtl-8c15a67166e545169216eee8be7c44acf03a27cb.tar.xz
Add actions and unit data
Diffstat (limited to 'LibSystemdCli')
-rw-r--r--LibSystemdCli/SystemdExecutor.cs43
1 files changed, 27 insertions, 16 deletions
diff --git a/LibSystemdCli/SystemdExecutor.cs b/LibSystemdCli/SystemdExecutor.cs
index 06c9538..ead11fd 100644
--- a/LibSystemdCli/SystemdExecutor.cs
+++ b/LibSystemdCli/SystemdExecutor.cs
@@ -4,35 +4,46 @@ using LibSystemdCli.Models;
 
 namespace LibSystemdCli;
 
-public class SystemdExecutor
-{
-    public static async IAsyncEnumerable<SystemdUnitListItem> GetUnits()
-    {
+public class SystemdExecutor {
+    public static async IAsyncEnumerable<SystemdUnitListItem> GetUnits() {
         var output = await CommandExecutor.ExecuteCommand("systemctl", "list-units --all --no-legend --no-pager --no-legend -o json-pretty");
-        
+
         var data = JsonSerializer.Deserialize<List<SystemdUnitListItem>>(output);
-        
-        foreach (var unit in data)
-        {
-            try
-            {
+
+        foreach (var unit in data) {
+            try {
                 var fragmentOutput = await CommandExecutor.ExecuteCommand("systemctl", $"show -P FragmentPath --no-pager --no-legend -- {unit.Unit} ");
                 // Console.WriteLine(fragmentOutput);
                 unit.FragmentPaths = fragmentOutput.Split('\n', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries).ToList();
             }
-            catch
-            {
-            }
+            catch { }
 
             yield return unit;
             // await Task.Delay(100);
         }
     }
 
-    public static async IAsyncEnumerable<SystemdJournalLogItem> GetUnitLogs(string serviceName) {
-        await foreach (var line in CommandExecutor.ExecuteCommandAsync("journalctl", $"--catalog --all --pager-end --follow --output=json --unit={serviceName}"))
-        {
+    public static async IAsyncEnumerable<SystemdJournalLogItem> GetUnitLogs(string serviceName, int contextLines = 100) {
+        await foreach (var line in CommandExecutor.ExecuteCommandAsync("journalctl", $"--catalog --all --pager-end --output=json --follow --lines={contextLines} --unit={serviceName}")) {
             yield return JsonSerializer.Deserialize<SystemdJournalLogItem>(line)!;
         }
     }
+
+    public static async Task<SystemdServiceData> GetUnitData(string serviceName) {
+        var output = await CommandExecutor.ExecuteCommand("systemctl", $"show --no-pager --no-legend -- {serviceName} ");
+        var data = new SystemdServiceData();
+        foreach (var line in output.Split('\n', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries)) {
+            var parts = line.Split('=', 2);
+            var key = parts[0];
+            var value = parts[1];
+            data.AddData(key, value);
+        }
+
+        return data;
+    }
+
+    private class TypeDef {
+        public bool IsList { get; set; }
+        public string Type { get; set; } = null!;
+    }
 }
\ No newline at end of file