Merge pull request #465 from NextTurn/commands

Split commands into functions
pull/480/head
Oleg Nenashev 2020-04-06 09:37:51 +02:00 committed by GitHub
commit 638d78f297
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 66 additions and 43 deletions

View File

@ -583,8 +583,63 @@ namespace winsw
args = args.GetRange(2, args.Count - 2); args = args.GetRange(2, args.Count - 2);
} }
args[0] = args[0].ToLower(); switch (args[0].ToLower())
if (args[0] == "install") {
case "install":
Install();
return;
case "uninstall":
Uninstall();
return;
case "start":
Start();
return;
case "stop":
Stop();
return;
case "restart":
Restart();
return;
case "restart!":
RestartSelf();
return;
case "status":
Status();
return;
case "test":
Test();
return;
case "testwait":
TestWait();
return;
case "help":
case "--help":
case "-h":
case "-?":
case "/?":
printHelp();
return;
case "version":
printVersion();
return;
default:
Console.WriteLine("Unknown command: " + args[0]);
printAvailableCommandsInfo();
throw new Exception("Unknown command: " + args[0]);
}
void Install()
{ {
Log.Info("Installing the service with id '" + descriptor.Id + "'"); Log.Info("Installing the service with id '" + descriptor.Id + "'");
@ -667,11 +722,9 @@ namespace winsw
rawSecurityDescriptor.GetBinaryForm(securityDescriptorBytes, 0); rawSecurityDescriptor.GetBinaryForm(securityDescriptorBytes, 0);
_ = Advapi32.SetServiceObjectSecurity(sc.Handle, SecurityInfos.DiscretionaryAcl, securityDescriptorBytes); _ = Advapi32.SetServiceObjectSecurity(sc.Handle, SecurityInfos.DiscretionaryAcl, securityDescriptorBytes);
} }
return;
} }
if (args[0] == "uninstall") void Uninstall()
{ {
Log.Info("Uninstalling the service with id '" + descriptor.Id + "'"); Log.Info("Uninstalling the service with id '" + descriptor.Id + "'");
if (s is null) if (s is null)
@ -708,11 +761,9 @@ namespace winsw
throw e; throw e;
} }
return;
} }
if (args[0] == "start") void Start()
{ {
Log.Info("Starting the service with id '" + descriptor.Id + "'"); Log.Info("Starting the service with id '" + descriptor.Id + "'");
if (s is null) if (s is null)
@ -733,11 +784,9 @@ namespace winsw
throw; throw;
} }
} }
return;
} }
if (args[0] == "stop") void Stop()
{ {
Log.Info("Stopping the service with id '" + descriptor.Id + "'"); Log.Info("Stopping the service with id '" + descriptor.Id + "'");
if (s is null) if (s is null)
@ -758,11 +807,9 @@ namespace winsw
throw; throw;
} }
} }
return;
} }
if (args[0] == "restart") void Restart()
{ {
Log.Info("Restarting the service with id '" + descriptor.Id + "'"); Log.Info("Restarting the service with id '" + descriptor.Id + "'");
if (s is null) if (s is null)
@ -778,10 +825,9 @@ namespace winsw
} }
s.StartService(); s.StartService();
return;
} }
if (args[0] == "restart!") void RestartSelf()
{ {
Log.Info("Restarting the service with id '" + descriptor.Id + "'"); Log.Info("Restarting the service with id '" + descriptor.Id + "'");
@ -792,11 +838,9 @@ namespace winsw
{ {
throw new Exception("Failed to invoke restart: " + Marshal.GetLastWin32Error()); throw new Exception("Failed to invoke restart: " + Marshal.GetLastWin32Error());
} }
return;
} }
if (args[0] == "status") void Status()
{ {
Log.Debug("User requested the status of the process with id '" + descriptor.Id + "'"); Log.Debug("User requested the status of the process with id '" + descriptor.Id + "'");
if (s is null) if (s is null)
@ -805,45 +849,24 @@ namespace winsw
Console.WriteLine("Started"); Console.WriteLine("Started");
else else
Console.WriteLine("Stopped"); Console.WriteLine("Stopped");
return;
} }
if (args[0] == "test") void Test()
{ {
WrapperService wsvc = new WrapperService(descriptor); WrapperService wsvc = new WrapperService(descriptor);
wsvc.OnStart(args.ToArray()); wsvc.OnStart(args.ToArray());
Thread.Sleep(1000); Thread.Sleep(1000);
wsvc.OnStop(); wsvc.OnStop();
return;
} }
if (args[0] == "testwait") void TestWait()
{ {
WrapperService wsvc = new WrapperService(descriptor); WrapperService wsvc = new WrapperService(descriptor);
wsvc.OnStart(args.ToArray()); wsvc.OnStart(args.ToArray());
Console.WriteLine("Press any key to stop the service..."); Console.WriteLine("Press any key to stop the service...");
Console.Read(); Console.Read();
wsvc.OnStop(); wsvc.OnStop();
return;
} }
if (args[0] == "help" || args[0] == "--help" || args[0] == "-h"
|| args[0] == "-?" || args[0] == "/?")
{
printHelp();
return;
}
if (args[0] == "version")
{
printVersion();
return;
}
Console.WriteLine("Unknown command: " + args[0]);
printAvailableCommandsInfo();
throw new Exception("Unknown command: " + args[0]);
} }
private static void InitLoggers(ServiceDescriptor d, bool enableCLILogging) private static void InitLoggers(ServiceDescriptor d, bool enableCLILogging)

View File

@ -35,7 +35,7 @@ namespace winswTests
public void FailOnUnsupportedCommand() public void FailOnUnsupportedCommand()
{ {
const string commandName = "nonExistentCommand"; const string commandName = "nonExistentCommand";
string expectedMessage = "Unknown command: " + commandName.ToLower(); string expectedMessage = "Unknown command: " + commandName;
CLITestResult res = CLITestHelper.CLIErrorTest(new[] { commandName }); CLITestResult res = CLITestHelper.CLIErrorTest(new[] { commandName });
Assert.True(res.HasException, "Expected an exception due to the wrong command"); Assert.True(res.HasException, "Expected an exception due to the wrong command");