public void IsSatisfiedBy_ShouldBeFalseIfExactValueDoesNotMatchButContainsOtherValue() { OptionValueSpecification.ForCommand("scheduler") .IsSatisfiedBy(0, Argument.CreateCommand("schedulers")) .Should() .BeFalse("because the values aren't exactly the same"); }
public void IsSatisfiedBy_ShouldBeTrueIfStringIsExactValue() { const string value = "value"; var specification = OptionValueSpecification.ForCommand(value); specification.IsSatisfiedBy(0, Argument.CreateCommand(value)).Should().BeTrue(); }
public void IsSatisfied_ShouldBeFalseForValueThatDoesNotMatch() { OptionValueSpecification.ForCommand("something") .IsSatisfiedBy(0, Argument.CreateCommand("by a different value")) .Should() .BeFalse("because the value doesn't exactly match"); }
private static void AddCheckProductVersionTo(OptionGroup inspecGroup, string productName, Func <IProductServer <ProductStatus> > restClient) { inspecGroup.WithOption( new CheckProductVersionOption(productName, restClient), OptionValueSpecification.ForCommand("version?"), OptionValueSpecification.ForVersion(), OnNode()); }
public void ParseArgument_ShouldParseCommandArgument() { var specification = OptionValueSpecification.ForCommand(Command); var argument = specification.ParseArgument(null, Command); AssertArgumentIsCommandArgument(Command, argument); }
private static OptionValueSpecification[] CreateInstallVersionSpecifications() { return(new[] { OptionValueSpecification.ForCommand("install"), OptionValueSpecification.ForVersion(), OnNode() }); }
private static OptionValueSpecification[] CreateDownloadVersionSpecifications() { return(new[] { OptionValueSpecification.ForCommand("download"), OptionValueSpecification.ForVersion(), OnNode() }); }
public void IsSatisfiedBy_ShouldBeTrueForOptionalArgumentThatIsNotThere() { var specification = new OptionSpecification(OptionValueSpecification.ForCommand("required"), OptionValueSpecification.ForOptionalCommand("optional")); specification.IsSatisfiedBy(Argument.CreateCommand("required")) .Should() .BeTrue("because the command is optional"); }
private static void AddProductOptionsTo(OptionGroup productGroup, string productName, Func <IProductServer <ProductStatus> > productServerFactory, ISchedulerWaiter schedulerWaiter) { productGroup.WithOption(new ShowInSpecStatusOption(productServerFactory), OptionValueSpecification.ForCommand("status"), OnNode()); productGroup.WithOption( new InstallOption <IProductServer <ProductStatus>, ProductStatus>(productName, productServerFactory, schedulerWaiter), CreateInstallVersionSpecifications()); productGroup.WithOption( new DownloadProductOption <IProductServer <ProductStatus>, ProductStatus>(productName, productServerFactory, schedulerWaiter), CreateDownloadVersionSpecifications()); AddCheckProductVersionTo(productGroup, productName, productServerFactory); }
public void ParseArguments_ShouldParseCommandAndTwoLabels() { const string groupLabel = "group:"; const string policyLabel = "policy:"; var specification = new OptionSpecification(OptionValueSpecification.ForCommand("chef"), OptionValueSpecification.ForValue(policyLabel, "the policy"), OptionValueSpecification.ForValue(groupLabel, "the group")); const string policyValue = "policy-value"; const string groupValue = "group-value"; var arguments = specification.ParseArguments("chef", policyLabel, policyValue, groupLabel, groupValue); arguments.Should().NotBeNull("because the arguments fit the specification"); arguments.Length.Should().Be(3); OptionValueSpecificationTest.AssertArgumentIsCommandArgument("chef", arguments[0]); OptionValueSpecificationTest.AssertArgumentIsValueArgument(policyLabel, policyValue, arguments[1]); OptionValueSpecificationTest.AssertArgumentIsValueArgument(groupLabel, groupValue, arguments[2]); }
public static OptionGroup CreateRootGroup(IClientFactory clientFactory, ISchedulerWaiter schedulerWaiter, IFileSystemCommands fileSystemCommands, ProcessExecutor processExecutor, IFileSystem fileSystem, ServiceStatusWaiter serviceStatusWaiter, IEnvironment environment) { var root = new OptionGroup() .WithGroup("chef", chefGroup => { const string chefProduct = "Chef"; chefGroup.WithOption(new RunChefOption(clientFactory.RestClientForChefServer, schedulerWaiter), OptionValueSpecification.ForCommand("run"), OnNode()); chefGroup.WithOption( new DownloadProductOption <IChefServer, ChefStatus>(chefProduct, clientFactory.RestClientForChefServer, schedulerWaiter), CreateDownloadVersionSpecifications()); chefGroup.WithOption(new ShowChefStatusOption(clientFactory.RestClientForChefServer), OptionValueSpecification.ForCommand("status"), OnNode()); chefGroup.WithOption( new BootstrapChefRunListOption(clientFactory.RestClientForChefServer, schedulerWaiter, fileSystemCommands), OptionValueSpecification.ForCommand("bootstrap"), OptionValueSpecification.ForValue("run-list:", "the run list"), OptionValueSpecification.ForValue("config:", "the client.rb file"), OptionValueSpecification.ForValue("validator:", "the validator.pem file used to join the node"), OnNode()); chefGroup.WithOption( new BootstrapChefPolicyOption(clientFactory.RestClientForChefServer, schedulerWaiter, fileSystemCommands), OptionValueSpecification.ForCommand("bootstrap"), OptionValueSpecification.ForValue("policy:", "the policy name"), OptionValueSpecification.ForValue("group:", "the policy group"), OptionValueSpecification.ForValue("config:", "the client.rb file"), OptionValueSpecification.ForValue("validator:", "the validator.pem file used to join the node"), OnNode()); var installChefOption = new InstallOption <IChefServer, ChefStatus>(chefProduct, clientFactory.RestClientForChefServer, schedulerWaiter); chefGroup.WithOption(installChefOption, CreateInstallVersionSpecifications()); chefGroup.WithOption(installChefOption, OptionValueSpecification.ForCommand("upgrade"), OptionValueSpecification.ForVersion(), OnNode()); chefGroup.WithOption( ChangeChefRunningStatusOption.CreatePauseChefOption(clientFactory.RestClientForChefServer), OptionValueSpecification.ForCommand("pause"), OnNode()); chefGroup.WithOption( ChangeChefRunningStatusOption.CreateResumeChefOption(clientFactory.RestClientForChefServer), OptionValueSpecification.ForCommand("resume"), OnNode()); }) .WithGroup("inspec", inspecGroup => { const string inspecProduct = "InSpec"; inspecGroup.WithOption(new ShowInSpecStatusOption(clientFactory.RestClientForInspecServer), OptionValueSpecification.ForCommand("status"), OnNode()); inspecGroup.WithOption( new InstallOption <IProductServer <ProductStatus>, ProductStatus>(inspecProduct, clientFactory.RestClientForInspecServer, schedulerWaiter), CreateInstallVersionSpecifications()); inspecGroup.WithOption( new DownloadProductOption <IProductServer <ProductStatus>, ProductStatus>(inspecProduct, clientFactory.RestClientForInspecServer, schedulerWaiter), CreateDownloadVersionSpecifications()); }) .WithGroup("server", serverGroup => { serverGroup.WithDefaultOption(new ServerInteractiveOption()); serverGroup.WithOption(new ServerWindowsServiceOption(), "--run-as-service"); }) .WithGroup("service", serviceGroup => { serviceGroup.WithOption(new RegisterServerWindowsServiceOption(), "register"); serviceGroup.WithOption(new UnregisterServerWindowsServiceOption(), "unregister"); serviceGroup.WithOption(ChangeStateForCafeWindowsServiceOption.StartCafeWindowsServiceOption( processExecutor, fileSystem, serviceStatusWaiter), "start"); serviceGroup.WithOption(ChangeStateForCafeWindowsServiceOption.StopCafeWindowsServiceOption( processExecutor, fileSystem, serviceStatusWaiter), "stop"); serviceGroup.WithOption(new CafeWindowsServiceStatusOption(processExecutor, fileSystem), "status"); }) .WithGroup("job", statusGroup => { var statusOption = new StatusOption(clientFactory.RestClientForJobServer); statusGroup.WithDefaultOption(statusOption); statusGroup.WithOption(statusOption, OptionValueSpecification.ForCommand("status"), OnNode()); statusGroup.WithOption(new JobRunStatusOption(clientFactory.RestClientForJobServer), OptionValueSpecification.ForCommand("status"), OptionValueSpecification.ForValue("id:", "job run id"), OnNode()); }) .WithOption(new InitOption(AssemblyDirectory, environment), "init"); var helpOption = new HelpOption(root); root.WithOption(helpOption, "help"); root.WithDefaultOption(helpOption); return(root); }
public static OptionGroup CreateRootGroup(IClientFactory clientFactory, ISchedulerWaiter schedulerWaiter, IFileSystemCommands fileSystemCommands, ProcessExecutor processExecutor, IFileSystem fileSystem, ServiceStatusWaiter serviceStatusWaiter, IEnvironment environment) { var root = new OptionGroup() .WithGroup("chef", chefGroup => { const string chefProduct = "Chef"; Func <IChefServer> restClientForChefServerFactory = clientFactory.RestClientForChefServer; chefGroup.WithOption(new RunChefOption(restClientForChefServerFactory, schedulerWaiter), OptionValueSpecification.ForCommand("run"), OnNode()); chefGroup.WithOption( new DownloadProductOption <IChefServer, ChefStatus>(chefProduct, restClientForChefServerFactory, schedulerWaiter), CreateDownloadVersionSpecifications()); chefGroup.WithOption(new ShowChefStatusOption(restClientForChefServerFactory), OptionValueSpecification.ForCommand("status"), OnNode()); chefGroup.WithOption( new BootstrapChefRunListOption(restClientForChefServerFactory, schedulerWaiter, fileSystemCommands), OptionValueSpecification.ForCommand("bootstrap"), OptionValueSpecification.ForValue("run-list:", "the run list"), OptionValueSpecification.ForValue("config:", "the client.rb file"), OptionValueSpecification.ForValue("validator:", "the validator.pem file used to join the node"), OnNode()); chefGroup.WithOption( new BootstrapChefPolicyOption(restClientForChefServerFactory, schedulerWaiter, fileSystemCommands), OptionValueSpecification.ForCommand("bootstrap"), OptionValueSpecification.ForValue("policy:", "the policy name"), OptionValueSpecification.ForValue("group:", "the policy group"), OptionValueSpecification.ForValue("config:", "the client.rb file"), OptionValueSpecification.ForValue("validator:", "the validator.pem file used to join the node"), OnNode()); var installChefOption = new InstallOption <IChefServer, ChefStatus>(chefProduct, restClientForChefServerFactory, schedulerWaiter); chefGroup.WithOption(installChefOption, CreateInstallVersionSpecifications()); chefGroup.WithOption(installChefOption, OptionValueSpecification.ForCommand("upgrade"), OptionValueSpecification.ForVersion(), OnNode(), ReturnImmediatelyOrDelayed()); chefGroup.WithOption( ChangeChefRunningStatusOption.CreatePauseChefOption(restClientForChefServerFactory), OptionValueSpecification.ForCommand("pause"), OnNode()); chefGroup.WithOption( ChangeChefRunningStatusOption.CreateResumeChefOption(restClientForChefServerFactory), OptionValueSpecification.ForCommand("resume"), OnNode()); chefGroup.WithOption( new CheckProductVersionOption("chef", clientFactory.GenericRestClientForChefServer), OptionValueSpecification.ForCommand("version?"), OptionValueSpecification.ForVersion(), OnNode()); }) .WithGroup("inspec", inspecGroup => { const string inspecProduct = "inspec"; Func <IProductServer <ProductStatus> > productServerFactory = clientFactory.RestClientForInspecServer; AddProductOptionsTo(inspecGroup, inspecProduct, productServerFactory, schedulerWaiter); }) .WithGroup("server", serverGroup => { const string application = "cafe"; Func <IWin32Service> serviceCreator = () => new CafeServerWindowsService(); serverGroup.WithDefaultOption(new ServerInteractiveOption(application, serviceCreator)); serverGroup.WithOption(new ServerWindowsServiceOption(application, serviceCreator), "--run-as-service"); }) .WithGroup("service", serviceGroup => { ServiceOptionInitializer.AddServiceOptionsTo(serviceGroup, serviceStatusWaiter, processExecutor, fileSystem, CafeServerWindowsServiceOptions.ServiceName, CafeServerWindowsServiceOptions.ServiceDisplayName, CafeServerWindowsServiceOptions.ServiceDescription); }) .WithGroup("job", statusGroup => { var statusOption = new StatusOption(clientFactory.RestClientForJobServer); statusGroup.WithDefaultOption(statusOption); statusGroup.WithOption(statusOption, OptionValueSpecification.ForCommand("status"), OnNode()); statusGroup.WithOption(new JobRunStatusOption(clientFactory.RestClientForJobServer), OptionValueSpecification.ForCommand("status"), OptionValueSpecification.ForValue("id:", "job run id"), OnNode()); }) .WithOption(new InitOption(AssemblyDirectory, environment), "init"); AddProductOptionsTo(root, "cafe", clientFactory.RestClientForCafeProductServer, schedulerWaiter); var helpOption = new HelpOption(root); root.WithOption(helpOption, "help"); root.WithDefaultOption(helpOption); return(root); }
public static int Main(string[] args) { Directory.SetCurrentDirectory(AssemblyDirectory); LoggingInitializer.ConfigureLogging(args); const string application = "cafe.Updater"; var processExecutor = new ProcessExecutor(() => new ProcessBoundary()); IWin32Service ServiceFactory() => new CafeUpdaterWindowsService(new CafeInstaller( new FileSystemCommandsBoundary(), processExecutor, UpdaterSettings.Instance.CafeApplicationDirectory)); var runner = new OptionGroup() .WithGroup("server", serverGroup => { serverGroup.WithDefaultOption(new ServerInteractiveOption(application, ServiceFactory)); serverGroup.WithOption(new ServerWindowsServiceOption(application, ServiceFactory), "--run-as-service"); }) .WithGroup("service", serviceGroup => { var fileSystem = new FileSystem(new EnvironmentBoundary(), new FileSystemCommandsBoundary()); var serviceStatusWaiter = new ServiceStatusWaiter("waiting for service status", new AutoResetEventBoundary(), new TimerFactory(), new ServiceStatusProvider(processExecutor, fileSystem), application); ServiceOptionInitializer.AddServiceOptionsTo(serviceGroup, serviceStatusWaiter, processExecutor, fileSystem, application, "cafe Updater", "updates cafe"); }) .WithOption(new WaitForInstallOption(new FileSystemCommandsBoundary()), OptionValueSpecification.ForCommand("wait"), OptionValueSpecification.ForValue("installer:", "the name of the installer file that should be processed by the updater")); var arguments = runner.ParseArguments(args); if (arguments != null) { var returnValue = runner.RunProgram(arguments); Logger.Debug($"Finishing {application} run"); return(returnValue); } else { Presenter.ShowError( $"No options match the supplied arguments. Run {application} help to view all options", Logger); return(-2); } }