private void addCommand(Subparser subparser, Command command) { commands.put(command.getName(), command); subparser.addSubparsers().help("available commands"); final Subparser commandSubparser = subparser.addSubparsers().addParser(command.getName(), false); command.configure(commandSubparser); commandSubparser.addArgument("-h", "--help") .action(new HelpArgumentAction()) .help("show this help message and exit") .setDefault(Arguments.SUPPRESS); commandSubparser.description(command.getDescription()) .setDefault(COMMAND_NAME_ATTR, command.getName()) .defaultHelp(true); }
private void printCommands(final DiagnosticConfig config, final StringBuilder res) { final List<Class<Command>> commands = service.getCommands(); if (!config.isPrintCommands() || commands.isEmpty()) { return; } // modules will never be empty res.append(NEWLINE).append(NEWLINE).append(TAB).append("COMMANDS = ").append(NEWLINE); final List<String> markers = Lists.newArrayList(); for (Class<Command> command : commands) { markers.clear(); final CommandItemInfo info = service.getData().getInfo(command); commonMarkers(markers, info); if (info.isEnvironmentCommand()) { markers.add("GUICE_ENABLED"); } res.append(TAB).append(TAB).append(renderClassLine(command, markers)).append(NEWLINE); } }
private void buildBinders() { if (!bindersBuilt) { jerseyBinder = Multibinder.newSetBinder(binder(), Object.class, Graceland.class); managedBinder = Multibinder.newSetBinder(binder(), Managed.class, Graceland.class); managedClassBinder = Multibinder.newSetBinder(binder(), TypeLiterals.ManagedClass, Graceland.class); healthCheckBinder = Multibinder.newSetBinder(binder(), HealthCheck.class, Graceland.class); healthCheckClassBinder = Multibinder.newSetBinder(binder(), TypeLiterals.HealthCheckClass, Graceland.class); taskBinder = Multibinder.newSetBinder(binder(), Task.class, Graceland.class); taskClassBinder = Multibinder.newSetBinder(binder(), TypeLiterals.TaskClass, Graceland.class); bundleBinder = Multibinder.newSetBinder(binder(), Bundle.class, Graceland.class); bundleClassBinder = Multibinder.newSetBinder(binder(), TypeLiterals.BundleClass, Graceland.class); commandBinder = Multibinder.newSetBinder(binder(), Command.class, Graceland.class); commandClassBinder = Multibinder.newSetBinder(binder(), TypeLiterals.CommandClass, Graceland.class); initializerBinder = Multibinder.newSetBinder(binder(), Initializer.class, Graceland.class); initializerClassBinder = Multibinder.newSetBinder(binder(), TypeLiterals.InitializerClass, Graceland.class); configuratorBinder = Multibinder.newSetBinder(binder(), Configurator.class, Graceland.class); configuratorClassBinder = Multibinder.newSetBinder(binder(), TypeLiterals.ConfiguratorClass, Graceland.class); bindersBuilt = true; } }
/** * Ran when the Dropwizard service initializes. This method is responsible for setting up the * {@link io.dropwizard.Bundle}s and {@link io.dropwizard.cli.Command}s. * * @param bootstrap Provided by Dropwizard. */ @Override public void initialize(Bootstrap<PlatformConfiguration> bootstrap) { for (Initializer initializer : wrapper.getInitializers()) { initializer.initialize(bootstrap); LOGGER.debug("Registered Initializer: {}", initializer.getClass().getCanonicalName()); } for (Bundle bundle : wrapper.getBundles()) { bootstrap.addBundle(bundle); LOGGER.debug("Registered Bundle: {}", bundle.getClass().getCanonicalName()); } for (Command command : wrapper.getCommands()) { bootstrap.addCommand(command); LOGGER.debug("Registered Command: {}", command.getClass().getCanonicalName()); } }
@Test public void command_binds_work() { final Command command = mock(Command.class); final Class<TestCommand> commandClass = TestCommand.class; Injector injector = Guice.createInjector(new AbstractPlugin() { @Override protected void configure() { bindCommand(command); bindCommand(commandClass); } }); Set<Command> commandSet = injector.getInstance(Keys.Commands); Set<Class<? extends Command>> commandClassSet = injector.getInstance(Keys.CommandClasses); assertThat(commandSet, hasSize(1)); assertThat(commandSet, hasItem(command)); assertThat(commandClassSet, hasSize(1)); assertThat(commandClassSet, hasItem(TestCommand.class)); }
@Test public void initialize_adds_commands() { final Command command = mock(Command.class); final Class<TestCommand> commandClass = TestCommand.class; Application application = buildApplication( new AbstractPlugin() { @Override protected void configure() { bindCommand(command); bindCommand(commandClass); } } ); new Platform(application).initialize(bootstrap); verify(bootstrap).addCommand(eq(command)); verify(bootstrap).addCommand(isA(TestCommand.class)); }
private static SortedMap<String, Command> generateSubCommands() { final SortedMap<String, Command> commands = new TreeMap<>(); final UseraddCommand useraddCommand = new UseraddCommand(); commands.put(useraddCommand.getName(), useraddCommand); final PasswdCommand passwdCommand = new PasswdCommand(); commands.put(passwdCommand.getName(), passwdCommand); return commands; }
private static SortedMap<String, Command> generateSubCommands() { final SortedMap<String, Command> commands = new TreeMap<>(); final GenerateRequestCommand generateRequestCommand = new GenerateRequestCommand(); commands.put(generateRequestCommand.getName(), generateRequestCommand); final GenerateSpecsCommand generateSpecsCommand = new GenerateSpecsCommand(); commands.put(generateSpecsCommand.getName(), generateSpecsCommand); return commands; }
@Override public void initialize(Bootstrap<?> bootstrap) { this.application = bootstrap.getApplication(); listServices(Bundle.class).forEach(bootstrap::addBundle); listServices(ConfiguredBundle.class).forEach(bootstrap::addBundle); listServices(Command.class).forEach(bootstrap::addCommand); }
/** * Runs a command for the {@link TestApplication} with the given yaml configuration. * * @param yamlConfig a <code>String</code> containing the yaml configuration * @return the TestApplication if successful * @throws Exception if the command failed */ public static TestApplication runDropwizardCommandUsingConfig(final String yamlConfig, final Function<TestApplication, Command> commandInstantiator) throws Exception { final TestApplication application = new TestApplication(); final Bootstrap<TestConfiguration> bootstrap = new Bootstrap<>(application); bootstrap.setConfigurationSourceProvider(new StringConfigurationSourceProvider()); final Command command = commandInstantiator.apply(application); command.run(bootstrap, new Namespace(Collections.singletonMap("file", yamlConfig))); return application; }
public static void main(String[] args) { System.out.println("This is simply a way of making sure things compile"); Command ec = new Command("Stuff", "Things"){ @Override public void configure(Subparser subparser){} @Override public void run(Bootstrap<?> bootstrap, Namespace namespace) throws Exception{} }; System.out.println("" + ec.getName()); }
/** * Register commands resolved with classpath scan. * * @param commands installed commands * @see ru.vyarus.dropwizard.guice.GuiceBundle.Builder#searchCommands() */ public void registerCommands(final List<Class<Command>> commands) { setScope(ClasspathScanner.class); for (Class<Command> cmd : commands) { register(ConfigItem.Command, cmd); } closeScope(); }
/** * Inject dependencies into all registered environment commands. (only field and setter injection could be used) * There is no need to process other commands, because only environment commands will run bundles and so will * start the injector. * * @param commands registered commands * @param injector guice injector object * @param tracker stats tracker */ public static void initCommands(final List<Command> commands, final Injector injector, final StatsTracker tracker) { final Stopwatch timer = tracker.timer(CommandTime); if (commands != null) { for (Command cmd : commands) { if (cmd instanceof EnvironmentCommand) { injector.injectMembers(cmd); } } } timer.stop(); }
protected HierarchicalCommand(String name, String description, SortedMap<String, Command> commands) { super(name, description); this.commands = commands; }
@Override public void configure(Subparser subparser) { for (Map.Entry<String, Command> command : commands.entrySet()) { addCommand(subparser, command.getValue()); } }
@Override protected void run(Bootstrap<ApplicationConfig> bootstrap, Namespace namespace, ApplicationConfig applicationConfig) throws Exception { final Command command = commands.get(namespace.getString(COMMAND_NAME_ATTR)); command.run(bootstrap, namespace); }
private static SortedMap<String, Command> generateSubCommands() { final SortedMap<String, Command> commands = new TreeMap<>(); final ValidateSpecCommand validateSpecCommand = new ValidateSpecCommand(); commands.put(validateSpecCommand.getName(), validateSpecCommand); return commands; }
/** * @return list of installed commands or empty list */ public List<Class<Command>> getCommands() { return commands; }
protected void bindCommand(Command command) { Preconditions.checkNotNull(command, "Command cannot be null."); buildBinders(); commandBinder.addBinding().toInstance(command); }
protected void bindCommand(Class<? extends Command> commandClass) { Preconditions.checkNotNull(commandClass, "Command Class cannot be null."); buildBinders(); commandClassBinder.addBinding().toInstance(commandClass); }
public ImmutableSet<Command> getCommands() { return getAndBuildInstances(Keys.Commands, Keys.CommandClasses); }
/** * @return types of all installed commands or empty list * @see ru.vyarus.dropwizard.guice.GuiceBundle.Builder#searchCommands() */ public List<Class<Command>> getCommands() { return context.getItems(ConfigItem.Command); }