private static int StopAndUninstall( CommandlineOptions options, Shell shell) { shell.StopMe(true); return(shell.UninstallMe()); }
private static int?RunOnceResultFor <T>(T instance, CommandlineOptions cli) where T : Shell, new() { if (cli.RunOnce || cli.Debug) { if (cli.Wait > 0) { Thread.Sleep(cli.Wait); } try { instance.RunningOnceFromCLI = true; instance.RunOnce(); return((int)CommandlineOptions.ExitCodes.Success); } catch (Exception ex) { Console.WriteLine("Error running main routine:"); Console.WriteLine(ex.Message); return((int)CommandlineOptions.ExitCodes.Failure); } } return(null); }
private static int StartIfPossible( CommandlineOptions arg1, Shell arg2) { var svc = new WindowsServiceUtil(arg2.ServiceName); if (svc.State == ServiceState.Unknown || svc.State == ServiceState.NotFound) { return(Fail($"{svc.ServiceName} not installed or not queryable")); } var entryExe = new Uri(Assembly.GetEntryAssembly()?.Location ?? "").LocalPath; if (!entryExe.Equals(svc.Commandline, StringComparison.InvariantCultureIgnoreCase)) { return(Fail( $"{svc.ServiceName} is installed at {svc.Commandline}.", "Issuing start command here will probably not do what you expect." )); } return(Starters.TryGetValue(svc.State, out var handler) ? handler(svc) : Fail($"No handler found for service state {svc.State}")); }
private static int?CLIRunResultFor <T>(string[] args, T instance) where T : Shell, new() { var cli = new CommandlineOptions(args, instance.DisplayName, instance.CopyrightInformation); var runResult = PerformServiceShellTasksFor(instance, cli); if (runResult.HasValue) { return(runResult.Value); } if (cli.Debug || cli.RunOnce) { ConfigureLogLevel(); EnsureHaveConsoleLogger(); } var lastRun = DateTime.Now; var lastResult = RunOnceResultFor(instance, cli); if (!cli.Debug) { return(lastResult); } instance.Running = true; Console.CancelKeyPress += Stop(instance); do { instance.WaitForIntervalFrom(lastRun); lastRun = DateTime.Now; lastResult = RunOnceResultFor(instance, cli); } while (instance.Running); return(lastResult); }
private static int InstallAndPerhapsStart( CommandlineOptions cli, Shell instance) { var result = instance.InstallMe(cli); return(cli.StartService ? instance.StartMe() : result); }
private static int?PerformServiceShellTasksFor <T>(T instance, CommandlineOptions cli) where T : Shell, new() { if (cli.ExitCode == CommandlineOptions.ExitCodes.ShowedHelp) { return((int)cli.ExitCode); } var task = ShellTasks.FirstOrDefault(t => t.Selector(cli)); return(task?.Logic.Invoke(cli, instance)); }
private static int?CLIRunResultFor <T>(string[] args, T instance) where T : Shell, new() { var cli = new CommandlineOptions(args, instance.DisplayName, instance.CopyrightInformation); var runResult = PerformServiceShellTasksFor(instance, cli); if (runResult.HasValue) { return(runResult.Value); } return(RunOnceResultFor(instance, cli)); }
private static int StopIfPossible( CommandlineOptions cli, Shell instance) { if (!instance.ServiceUtil.IsStoppable) { return(Fail($"Service '{instance.ServiceName}' cannot be stopped in stated '{instance.State}'")); } return(TryRunShellTask( () => instance.ServiceUtil.Stop(true), ex => $"Unable to stop '{instance.ServiceName}': {ex.Message}")); }
private ServiceBootFlag ResolveBootFlagFor(CommandlineOptions cli) { if (!cli.ManualStart && !cli.Disabled) { return(ServiceBootFlag.AutoStart); } if (cli.ManualStart && cli.Disabled) { throw new ArgumentException("Cannot specify that the service be both disabled and manual start"); } return(cli.ManualStart ? ServiceBootFlag.ManualStart : ServiceBootFlag.Disabled); }
private int InstallMe(CommandlineOptions cli) { var myExePath = new FileInfo(Environment.GetCommandLineArgs()[0]).FullName; var existingSvcUtil = new WindowsServiceUtil(ServiceName); if (existingSvcUtil.Commandline == myExePath) { Console.WriteLine("Service already installed correctly"); return((int)CommandlineOptions.ExitCodes.Success); } try { if (existingSvcUtil.IsInstalled) { existingSvcUtil.Uninstall(true); } } catch (Exception ex) { Console.WriteLine("Service already installed at: " + existingSvcUtil.Commandline + " and I can't uninstall it: " + ex.Message); return((int)CommandlineOptions.ExitCodes.InstallFailed); } var svcUtil = new WindowsServiceUtil(ServiceName, DisplayName, myExePath); try { var bootFlag = ResolveBootFlagFor(cli); svcUtil.Install(bootFlag); Console.WriteLine("Installed!"); return((int)CommandlineOptions.ExitCodes.Success); } catch (Exception ex) { Console.WriteLine("Unable to install: " + ex.Message); return((int)CommandlineOptions.ExitCodes.InstallFailed); } }
private static int?PerformServiceShellTasksFor <T>(T instance, CommandlineOptions cli) where T : Shell, new() { if (cli.ExitCode == CommandlineOptions.ExitCodes.ShowedHelp) { return((int)cli.ExitCode); } if (cli.ShowVersion) { return(instance.ShowVersion <T>()); } if (cli.Uninstall) { instance.StopMe(true); return(instance.UninstallMe()); } if (cli.Install) { var result = instance.InstallMe(); return(cli.StartService ? instance.StartMe() : result); } return(null); }
private static int ShowVersionFor( CommandlineOptions cli, Shell shell) { return(shell.ShowVersion()); }