Fixed the short-format issue in list-kubernetes-host CLI command
Project: http://git-wip-us.apache.org/repos/asf/stratos/repo Commit: http://git-wip-us.apache.org/repos/asf/stratos/commit/6a6a962d Tree: http://git-wip-us.apache.org/repos/asf/stratos/tree/6a6a962d Diff: http://git-wip-us.apache.org/repos/asf/stratos/diff/6a6a962d Branch: refs/heads/master-deployment-policy-fix-merge Commit: 6a6a962d169d1e24ca542da4e97a769bfeadbf9e Parents: 49780e8 Author: Dinithi <[email protected]> Authored: Thu Mar 12 10:10:08 2015 +0530 Committer: Imesh Gunaratne <[email protected]> Committed: Sat Mar 14 11:11:29 2015 +0530 ---------------------------------------------------------------------- .../commands/ListKubernetesHostsCommand.java | 88 +++++++++++++++----- 1 file changed, 69 insertions(+), 19 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/stratos/blob/6a6a962d/components/org.apache.stratos.cli/src/main/java/org/apache/stratos/cli/commands/ListKubernetesHostsCommand.java ---------------------------------------------------------------------- diff --git a/components/org.apache.stratos.cli/src/main/java/org/apache/stratos/cli/commands/ListKubernetesHostsCommand.java b/components/org.apache.stratos.cli/src/main/java/org/apache/stratos/cli/commands/ListKubernetesHostsCommand.java index dcb21d8..b240cfa 100644 --- a/components/org.apache.stratos.cli/src/main/java/org/apache/stratos/cli/commands/ListKubernetesHostsCommand.java +++ b/components/org.apache.stratos.cli/src/main/java/org/apache/stratos/cli/commands/ListKubernetesHostsCommand.java @@ -19,8 +19,7 @@ package org.apache.stratos.cli.commands; -import org.apache.commons.cli.Option; -import org.apache.commons.cli.Options; +import org.apache.commons.cli.*; import org.apache.stratos.cli.Command; import org.apache.stratos.cli.RestCommandLineService; import org.apache.stratos.cli.StratosCommandContext; @@ -29,12 +28,28 @@ import org.apache.stratos.cli.utils.CliConstants; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import static org.apache.stratos.cli.utils.CliUtils.mergeOptionArrays; + public class ListKubernetesHostsCommand implements Command<StratosCommandContext> { private static final Logger logger = LoggerFactory.getLogger(ListKubernetesHostsCommand.class); - public ListKubernetesHostsCommand() { - } + private final Options options; + + public ListKubernetesHostsCommand(){ + options = constructOptions(); + } + + private Options constructOptions() { + final Options options = new Options(); + + Option clusterId = new Option(CliConstants.CLUSTER_ID_OPTION, CliConstants.CLUSTER_ID_LONG_OPTION, true, + "Cluster id"); + clusterId.setArgName("cluster id"); + options.addOption(clusterId); + + return options; + } public String getName() { return "list-kubernetes-hosts"; @@ -45,24 +60,59 @@ public class ListKubernetesHostsCommand implements Command<StratosCommandContext } public String getArgumentSyntax() { - return "[cluster-id]"; + return null; } - public int execute(StratosCommandContext context, String[] args, Option[] already_parsed_opts) throws CommandException { - if (logger.isDebugEnabled()) { - logger.debug("Executing command: ", getName()); - } - if ((args == null) || (args.length == 0)) { + public int execute(StratosCommandContext context, String[] args, Option[] already_parsed_opts) throws CommandException { + if (logger.isDebugEnabled()) { + logger.debug("Executing {} command...", getName()); + } + + if (args != null && args.length > 0) { + String clusterId= null; + + final CommandLineParser parser = new GnuParser(); + CommandLine commandLine; + + try { + commandLine = parser.parse(options, args); + //merge newly discovered options with previously discovered ones. + Options opts = mergeOptionArrays(already_parsed_opts, commandLine.getOptions()); + + if (logger.isDebugEnabled()) { + logger.debug("List kubernetes hosts of a cluster"); + } + + if (opts.hasOption(CliConstants.CLUSTER_ID_OPTION)) { + if (logger.isTraceEnabled()) { + logger.trace("Cluster id option is passed"); + } + clusterId = opts.getOption(CliConstants.CLUSTER_ID_OPTION).getValue(); + } + + if (clusterId == null) { + System.out.println("usage: " + getName() + "usage: " + getName() + " [-c <cluster-id>]"); + return CliConstants.COMMAND_FAILED; + } + + RestCommandLineService.getInstance().listKubernetesHosts(clusterId); + return CliConstants.COMMAND_SUCCESSFULL; + + } catch (ParseException e) { + if (logger.isErrorEnabled()) { + logger.error("Error parsing arguments", e); + } + System.out.println(e.getMessage()); + return CliConstants.COMMAND_FAILED; + } + + } else { context.getStratosApplication().printUsage(getName()); return CliConstants.COMMAND_FAILED; - } else { - String clusterId = args[0]; - RestCommandLineService.getInstance().listKubernetesHosts(clusterId); - return CliConstants.COMMAND_SUCCESSFULL; - } - } + } + } - public Options getOptions() { - return null; - } + public Options getOptions() { + return options; + } }
