Add dev ps command

This commit is contained in:
NextTurn
2020-08-02 00:00:00 +08:00
committed by Next Turn
parent fcbc087b4b
commit 9573a78668
4 changed files with 170 additions and 50 deletions

View File

@@ -26,6 +26,7 @@ using log4net.Core;
using log4net.Layout;
using WinSW.Logging;
using WinSW.Native;
using WinSW.Util;
using Process = System.Diagnostics.Process;
using TimeoutException = System.ServiceProcess.TimeoutException;
@@ -238,6 +239,22 @@ namespace WinSW
root.Add(refresh);
}
{
var dev = new Command("dev");
dev.Add(config);
dev.Add(noElevate);
root.Add(dev);
var ps = new Command("ps")
{
Handler = CommandHandler.Create<string?, bool>(DevPs),
};
dev.Add(ps);
}
return new CommandLineBuilder(root)
// see UseDefaults
@@ -774,6 +791,58 @@ namespace WinSW
}
}
void DevPs(string? pathToConfig, bool noElevate)
{
XmlServiceConfig config = XmlServiceConfig.Create(pathToConfig);
if (!elevated)
{
Elevate(noElevate);
return;
}
using ServiceManager scm = ServiceManager.Open();
using Service sc = scm.OpenService(config.Id);
int processId = sc.ProcessId;
if (processId >= 0)
{
const string Vertical = " \u2502 ";
const string Corner = " \u2514\u2500";
const string Cross = " \u251c\u2500";
const string Space = " ";
using Process process = Process.GetProcessById(processId);
Draw(process, string.Empty, true);
static void Draw(Process process, string indentation, bool isLastChild)
{
Console.Write(indentation);
if (isLastChild)
{
Console.Write(Corner);
indentation += Space;
}
else
{
Console.Write(Cross);
indentation += Vertical;
}
Console.WriteLine(process.Format());
List<Process> children = process.GetChildren();
int count = children.Count;
for (int i = 0; i < count; i++)
{
using Process child = children[i];
Draw(child, indentation, i == count - 1);
}
}
}
}
// [DoesNotReturn]
static void Elevate(bool noElevate)
{