Search in sources :

Example 1 with CliOption

use of org.springframework.shell.core.annotation.CliOption in project geode by apache.

the class Helper method getHelp.

/**
   * returns a short description and help string of the command if annotations is null or returns a
   * details description of the command with the syntax and parameter description
   */
HelpBlock getHelp(CliCommand cliCommand, Annotation[][] annotations, Class<?>[] parameterTypes) {
    String commandName = cliCommand.value()[0];
    boolean isAvailable = isAvailable(commandName);
    if (annotations == null && parameterTypes == null) {
        String available = isAvailable ? AVAILABLE : NOT_AVAILABLE;
        HelpBlock help = new HelpBlock(commandName + " (" + available + ")");
        help.addChild(new HelpBlock(cliCommand.help()));
        return help;
    }
    HelpBlock root = new HelpBlock();
    // First we will have the block for NAME of the command
    HelpBlock name = new HelpBlock(NAME_NAME);
    name.addChild(new HelpBlock(commandName));
    root.addChild(name);
    // add the availability flag
    HelpBlock availability = new HelpBlock(IS_AVAILABLE_NAME);
    availability.addChild(new HelpBlock(isAvailable + ""));
    root.addChild(availability);
    // Now add synonyms if any
    String[] allNames = cliCommand.value();
    if (allNames.length > 1) {
        HelpBlock synonyms = new HelpBlock(SYNONYMS_NAME);
        for (int i = 1; i < allNames.length; i++) {
            synonyms.addChild(new HelpBlock(allNames[i]));
        }
        root.addChild(synonyms);
    }
    // Now comes the turn to display synopsis if any
    if (StringUtils.isNotBlank(cliCommand.help())) {
        HelpBlock synopsis = new HelpBlock(SYNOPSIS_NAME);
        synopsis.addChild(new HelpBlock(cliCommand.help()));
        root.addChild(synopsis);
    }
    // Now display the syntax for the command
    HelpBlock syntaxBlock = new HelpBlock(SYNTAX_NAME);
    String syntax = getSyntaxString(commandName, annotations, parameterTypes);
    syntaxBlock.addChild(new HelpBlock(syntax));
    root.addChild(syntaxBlock);
    // Detailed description of all the Options
    if (annotations.length > 0) {
        HelpBlock options = new HelpBlock(OPTIONS_NAME);
        for (int i = 0; i < annotations.length; i++) {
            CliOption cliOption = getAnnotation(annotations[i], CliOption.class);
            HelpBlock optionNode = getOptionDetail(cliOption);
            options.addChild(optionNode);
        }
        root.addChild(options);
    }
    return root;
}
Also used : CliOption(org.springframework.shell.core.annotation.CliOption)

Example 2 with CliOption

use of org.springframework.shell.core.annotation.CliOption in project geode by apache.

the class LuceneIndexCommands method createIndex.

@CliCommand(value = LuceneCliStrings.LUCENE_CREATE_INDEX, help = LuceneCliStrings.LUCENE_CREATE_INDEX__HELP)
@CliMetaData(relatedTopic = { CliStrings.TOPIC_GEODE_REGION, CliStrings.TOPIC_GEODE_DATA })
public // TODO : Add optionContext for indexName
Result createIndex(@CliOption(key = LuceneCliStrings.LUCENE__INDEX_NAME, mandatory = true, help = LuceneCliStrings.LUCENE_CREATE_INDEX__NAME__HELP) final String indexName, @CliOption(key = LuceneCliStrings.LUCENE__REGION_PATH, mandatory = true, optionContext = ConverterHint.REGION_PATH, help = LuceneCliStrings.LUCENE_CREATE_INDEX__REGION_HELP) final String regionPath, @CliOption(key = LuceneCliStrings.LUCENE_CREATE_INDEX__FIELD, mandatory = true, help = LuceneCliStrings.LUCENE_CREATE_INDEX__FIELD_HELP) final String[] fields, @CliOption(key = LuceneCliStrings.LUCENE_CREATE_INDEX__ANALYZER, help = LuceneCliStrings.LUCENE_CREATE_INDEX__ANALYZER_HELP) final String[] analyzers) {
    Result result;
    XmlEntity xmlEntity = null;
    this.securityService.authorizeRegionManage(regionPath);
    try {
        final InternalCache cache = getCache();
        // trim fields for any leading trailing spaces.
        String[] trimmedFields = Arrays.stream(fields).map(field -> field.trim()).toArray(size -> new String[size]);
        LuceneIndexInfo indexInfo = new LuceneIndexInfo(indexName, regionPath, trimmedFields, analyzers);
        final ResultCollector<?, ?> rc = this.executeFunctionOnAllMembers(createIndexFunction, indexInfo);
        final List<CliFunctionResult> funcResults = (List<CliFunctionResult>) rc.getResult();
        final TabularResultData tabularResult = ResultBuilder.createTabularResultData();
        for (final CliFunctionResult cliFunctionResult : funcResults) {
            tabularResult.accumulate("Member", cliFunctionResult.getMemberIdOrName());
            if (cliFunctionResult.isSuccessful()) {
                tabularResult.accumulate("Status", "Successfully created lucene index");
            // if (xmlEntity == null) {
            // xmlEntity = cliFunctionResult.getXmlEntity();
            // }
            } else {
                tabularResult.accumulate("Status", "Failed: " + cliFunctionResult.getMessage());
            }
        }
        result = ResultBuilder.buildResult(tabularResult);
    } catch (IllegalArgumentException iae) {
        LogWrapper.getInstance().info(iae.getMessage());
        result = ResultBuilder.createUserErrorResult(iae.getMessage());
    } catch (CommandResultException crex) {
        result = crex.getResult();
    } catch (Exception e) {
        result = ResultBuilder.createGemFireErrorResult(e.getMessage());
    }
    return result;
}
Also used : InternalCache(org.apache.geode.internal.cache.InternalCache) StringUtils(org.apache.commons.lang.StringUtils) CliAvailabilityIndicator(org.springframework.shell.core.annotation.CliAvailabilityIndicator) Arrays(java.util.Arrays) FunctionInvocationTargetException(org.apache.geode.cache.execute.FunctionInvocationTargetException) Execution(org.apache.geode.cache.execute.Execution) CliMetaData(org.apache.geode.management.cli.CliMetaData) Function(org.apache.geode.cache.execute.Function) CliOption(org.springframework.shell.core.annotation.CliOption) ArrayList(java.util.ArrayList) LuceneListIndexFunction(org.apache.geode.cache.lucene.internal.cli.functions.LuceneListIndexFunction) HashSet(java.util.HashSet) TabularResultData(org.apache.geode.management.internal.cli.result.TabularResultData) LuceneDescribeIndexFunction(org.apache.geode.cache.lucene.internal.cli.functions.LuceneDescribeIndexFunction) CliStrings(org.apache.geode.management.internal.cli.i18n.CliStrings) Region(org.apache.geode.cache.Region) LuceneSearchIndexFunction(org.apache.geode.cache.lucene.internal.cli.functions.LuceneSearchIndexFunction) SystemFailure(org.apache.geode.SystemFailure) IntegratedSecurityService(org.apache.geode.internal.security.IntegratedSecurityService) Result(org.apache.geode.management.cli.Result) DistributedMember(org.apache.geode.distributed.DistributedMember) XmlEntity(org.apache.geode.management.internal.configuration.domain.XmlEntity) CliFunctionResult(org.apache.geode.management.internal.cli.functions.CliFunctionResult) LinkedHashSet(java.util.LinkedHashSet) CliUtil(org.apache.geode.management.internal.cli.CliUtil) Gfsh(org.apache.geode.management.internal.cli.shell.Gfsh) CliCommand(org.springframework.shell.core.annotation.CliCommand) CommandResult(org.apache.geode.management.internal.cli.result.CommandResult) GfshCommand(org.apache.geode.management.internal.cli.commands.GfshCommand) CommandResultException(org.apache.geode.management.internal.cli.result.CommandResultException) SecurityService(org.apache.geode.internal.security.SecurityService) Set(java.util.Set) ResultCollector(org.apache.geode.cache.execute.ResultCollector) Collectors(java.util.stream.Collectors) AbstractExecution(org.apache.geode.internal.cache.execute.AbstractExecution) List(java.util.List) LogWrapper(org.apache.geode.management.internal.cli.LogWrapper) ResourceOperation(org.apache.geode.management.internal.security.ResourceOperation) LuceneCreateIndexFunction(org.apache.geode.cache.lucene.internal.cli.functions.LuceneCreateIndexFunction) LuceneDestroyIndexFunction(org.apache.geode.cache.lucene.internal.cli.functions.LuceneDestroyIndexFunction) Resource(org.apache.geode.security.ResourcePermission.Resource) ConverterHint(org.apache.geode.management.cli.ConverterHint) Operation(org.apache.geode.security.ResourcePermission.Operation) ResultBuilder(org.apache.geode.management.internal.cli.result.ResultBuilder) TabularResultData(org.apache.geode.management.internal.cli.result.TabularResultData) InternalCache(org.apache.geode.internal.cache.InternalCache) FunctionInvocationTargetException(org.apache.geode.cache.execute.FunctionInvocationTargetException) CommandResultException(org.apache.geode.management.internal.cli.result.CommandResultException) Result(org.apache.geode.management.cli.Result) CliFunctionResult(org.apache.geode.management.internal.cli.functions.CliFunctionResult) CommandResult(org.apache.geode.management.internal.cli.result.CommandResult) XmlEntity(org.apache.geode.management.internal.configuration.domain.XmlEntity) CliFunctionResult(org.apache.geode.management.internal.cli.functions.CliFunctionResult) CommandResultException(org.apache.geode.management.internal.cli.result.CommandResultException) ArrayList(java.util.ArrayList) List(java.util.List) CliCommand(org.springframework.shell.core.annotation.CliCommand) CliMetaData(org.apache.geode.management.cli.CliMetaData)

Example 3 with CliOption

use of org.springframework.shell.core.annotation.CliOption in project geode by apache.

the class Helper method getSyntaxString.

String getSyntaxString(String commandName, Annotation[][] annotations, Class[] parameterTypes) {
    StringBuffer buffer = new StringBuffer();
    buffer.append(commandName);
    for (int i = 0; i < annotations.length; i++) {
        CliOption cliOption = getAnnotation(annotations[i], CliOption.class);
        String optionString = getOptionString(cliOption, parameterTypes[i]);
        if (cliOption.mandatory()) {
            buffer.append(" ").append(optionString);
        } else {
            buffer.append(" [").append(optionString).append("]");
        }
    }
    return buffer.toString();
}
Also used : CliOption(org.springframework.shell.core.annotation.CliOption)

Aggregations

CliOption (org.springframework.shell.core.annotation.CliOption)3 ArrayList (java.util.ArrayList)1 Arrays (java.util.Arrays)1 HashSet (java.util.HashSet)1 LinkedHashSet (java.util.LinkedHashSet)1 List (java.util.List)1 Set (java.util.Set)1 Collectors (java.util.stream.Collectors)1 StringUtils (org.apache.commons.lang.StringUtils)1 SystemFailure (org.apache.geode.SystemFailure)1 Region (org.apache.geode.cache.Region)1 Execution (org.apache.geode.cache.execute.Execution)1 Function (org.apache.geode.cache.execute.Function)1 FunctionInvocationTargetException (org.apache.geode.cache.execute.FunctionInvocationTargetException)1 ResultCollector (org.apache.geode.cache.execute.ResultCollector)1 LuceneCreateIndexFunction (org.apache.geode.cache.lucene.internal.cli.functions.LuceneCreateIndexFunction)1 LuceneDescribeIndexFunction (org.apache.geode.cache.lucene.internal.cli.functions.LuceneDescribeIndexFunction)1 LuceneDestroyIndexFunction (org.apache.geode.cache.lucene.internal.cli.functions.LuceneDestroyIndexFunction)1 LuceneListIndexFunction (org.apache.geode.cache.lucene.internal.cli.functions.LuceneListIndexFunction)1 LuceneSearchIndexFunction (org.apache.geode.cache.lucene.internal.cli.functions.LuceneSearchIndexFunction)1