Search in sources :

Example 6 with CommandArgument

use of org.jboss.as.cli.CommandArgument in project wildfly-core by wildfly.

the class GenericTypeOperationHandler method getHandler.

private OperationCommand getHandler(CommandContext ctx, String op) throws CommandLineException {
    if (op == null) {
        if (writePropHandler == null) {
            writePropHandler = new WritePropertyHandler();
            Iterator<AttributeDescription> props = getNodeProperties(ctx);
            while (props.hasNext()) {
                final AttributeDescription prop = props.next();
                if (prop.isWriteAllowed()) {
                    CommandLineCompleter valueCompleter = null;
                    ArgumentValueConverter valueConverter = null;
                    if (propConverters != null) {
                        valueConverter = propConverters.get(prop.getName());
                    }
                    if (valueCompleters != null) {
                        valueCompleter = valueCompleters.get(prop.getName());
                    }
                    if (valueConverter == null) {
                        valueConverter = ArgumentValueConverter.DEFAULT;
                        final ModelType propType = prop.getType();
                        if (propType != null) {
                            if (ModelType.BOOLEAN == propType) {
                                if (valueCompleter == null) {
                                    valueCompleter = SimpleTabCompleter.BOOLEAN;
                                }
                            } else if (ModelType.STRING == propType) {
                                valueConverter = ArgumentValueConverter.NON_OBJECT;
                            } else if (prop.getName().endsWith("properties")) {
                                // TODO this is bad but can't rely on proper descriptions
                                valueConverter = ArgumentValueConverter.PROPERTIES;
                            } else if (ModelType.LIST == propType) {
                                if (asType(prop.getProperty(Util.VALUE_TYPE)) == ModelType.PROPERTY) {
                                    valueConverter = ArgumentValueConverter.PROPERTIES;
                                } else {
                                    valueConverter = ArgumentValueConverter.LIST;
                                }
                            }
                        }
                    }
                    final CommandArgument arg = new ArgumentWithValue(GenericTypeOperationHandler.this, valueCompleter, valueConverter, "--" + prop.getName());
                    writePropHandler.addArgument(arg);
                }
            }
        }
        return writePropHandler;
    } else {
        if (customHandlers != null && customHandlers.containsKey(op)) {
            final OperationCommand opHandler = customHandlers.get(op);
            if (opHandler != null) {
                return opHandler;
            }
        }
        if (opHandlers != null) {
            OperationCommand opHandler = opHandlers.get(op);
            if (opHandler != null) {
                return opHandler;
            }
        }
        final ModelNode descr = getOperationDescription(ctx, op);
        if (opHandlers == null) {
            opHandlers = new HashMap<String, OperationCommand>();
        }
        final OpHandler opHandler = new OpHandler(op);
        opHandlers.put(op, opHandler);
        opHandler.addArgument(this.headers);
        if (descr != null && descr.has(Util.REQUEST_PROPERTIES)) {
            final List<Property> propList = descr.get(Util.REQUEST_PROPERTIES).asPropertyList();
            for (Property prop : propList) {
                final ModelNode propDescr = prop.getValue();
                CommandLineCompleter valueCompleter = null;
                ArgumentValueConverter valueConverter = null;
                if (propConverters != null) {
                    valueConverter = propConverters.get(prop.getName());
                }
                if (valueCompleters != null) {
                    valueCompleter = valueCompleters.get(prop.getName());
                }
                if (valueConverter == null) {
                    valueConverter = ArgumentValueConverter.DEFAULT;
                    if (propDescr.has(Util.TYPE)) {
                        final ModelType type = propDescr.get(Util.TYPE).asType();
                        if (ModelType.BOOLEAN == type) {
                            if (valueCompleter == null) {
                                valueCompleter = SimpleTabCompleter.BOOLEAN;
                            }
                        } else if (ModelType.STRING == type) {
                            valueConverter = ArgumentValueConverter.NON_OBJECT;
                        } else if (prop.getName().endsWith("properties")) {
                            // TODO this is bad but can't rely on proper descriptions
                            valueConverter = ArgumentValueConverter.PROPERTIES;
                        } else if (ModelType.LIST == type) {
                            if (propDescr.hasDefined(Util.VALUE_TYPE) && asType(propDescr.get(Util.VALUE_TYPE)) == ModelType.PROPERTY) {
                                valueConverter = ArgumentValueConverter.PROPERTIES;
                            } else {
                                valueConverter = ArgumentValueConverter.LIST;
                            }
                        }
                    }
                }
                final CommandArgument arg = new ArgumentWithValue(GenericTypeOperationHandler.this, valueCompleter, valueConverter, "--" + prop.getName());
                opHandler.addArgument(arg);
            }
        }
        return opHandler;
    }
}
Also used : CommandArgument(org.jboss.as.cli.CommandArgument) CommandLineCompleter(org.jboss.as.cli.CommandLineCompleter) OperationCommand(org.jboss.as.cli.OperationCommand) ArgumentValueConverter(org.jboss.as.cli.ArgumentValueConverter) ArgumentWithValue(org.jboss.as.cli.impl.ArgumentWithValue) ModelType(org.jboss.dmr.ModelType) ModelNode(org.jboss.dmr.ModelNode) Property(org.jboss.dmr.Property)

Example 7 with CommandArgument

use of org.jboss.as.cli.CommandArgument in project wildfly-core by wildfly.

the class GenericTypeOperationHandler method getArguments.

@Override
public Collection<CommandArgument> getArguments(CommandContext ctx) {
    final ParsedCommandLine args = ctx.getParsedCommandLine();
    List<CommandArgument> arguments = new ArrayList<>();
    try {
        if (isChildNode) {
            arguments.addAll(staticArgs.values());
        } else {
            if (!name.isValueComplete(args)) {
                return staticArgs.values();
            }
        }
    } catch (CommandFormatException e) {
        return Collections.emptyList();
    }
    final String op = operation.getValue(args);
    OperationCommand handler;
    try {
        handler = getHandler(ctx, op);
    } catch (CommandLineException e) {
        // Return the static arguments in case of child node.
        return arguments;
    }
    arguments.addAll(handler.getArguments(ctx));
    return arguments;
}
Also used : OperationCommand(org.jboss.as.cli.OperationCommand) CommandFormatException(org.jboss.as.cli.CommandFormatException) CommandArgument(org.jboss.as.cli.CommandArgument) ArrayList(java.util.ArrayList) ParsedCommandLine(org.jboss.as.cli.operation.ParsedCommandLine) CommandLineException(org.jboss.as.cli.CommandLineException)

Example 8 with CommandArgument

use of org.jboss.as.cli.CommandArgument in project wildfly-core by wildfly.

the class MockOperationCandidatesProvider method getProperties.

@Override
public List<CommandArgument> getProperties(CommandContext ctx, String operationName, OperationRequestAddress address) {
    MockOperation operation = root.getOperation(operationName);
    if (operation == null) {
        return Collections.emptyList();
    }
    final List<MockOperationProperty> properties = operation.getProperties();
    final List<CommandArgument> result = new ArrayList<CommandArgument>(properties.size());
    for (final MockOperationProperty property : properties) {
        String name = property.getName();
        result.add(new CommandArgument() {

            @Override
            public String getFullName() {
                return name;
            }

            @Override
            public String getShortName() {
                return null;
            }

            @Override
            public int getIndex() {
                return -1;
            }

            @Override
            public boolean isPresent(ParsedCommandLine args) throws CommandFormatException {
                return args.hasProperty(name);
            }

            @Override
            public boolean canAppearNext(CommandContext ctx) throws CommandFormatException {
                ParsedCommandLine args = ctx.getParsedCommandLine();
                if (isPresent(args)) {
                    return !isValueComplete(args);
                }
                return true;
            }

            @Override
            public String getValue(ParsedCommandLine args) throws CommandFormatException {
                return args.getPropertyValue(name);
            }

            @Override
            public String getValue(ParsedCommandLine args, boolean required) throws CommandFormatException {
                if (!isPresent(args)) {
                    throw new CommandFormatException("Property '" + name + "' is missing required value.");
                }
                return args.getPropertyValue(name);
            }

            @Override
            public boolean isValueComplete(ParsedCommandLine args) throws CommandFormatException {
                if (!isPresent(args)) {
                    return false;
                }
                if (name.equals(args.getLastParsedPropertyName()) && !args.isLastPropertyNegated()) {
                    return false;
                }
                return true;
            }

            @Override
            public boolean isValueRequired() {
                return property.isValueRequired();
            }

            @Override
            public CommandLineCompleter getValueCompleter() {
                return property.getPossibleValues() != null ? new SimpleTabCompleter(property.getPossibleValues()) : null;
            }
        });
    }
    return result;
}
Also used : SimpleTabCompleter(org.jboss.as.cli.handlers.SimpleTabCompleter) CommandContext(org.jboss.as.cli.CommandContext) CommandArgument(org.jboss.as.cli.CommandArgument) ArrayList(java.util.ArrayList) CommandLineCompleter(org.jboss.as.cli.CommandLineCompleter) CommandFormatException(org.jboss.as.cli.CommandFormatException) ParsedCommandLine(org.jboss.as.cli.operation.ParsedCommandLine)

Example 9 with CommandArgument

use of org.jboss.as.cli.CommandArgument in project wildfly-core by wildfly.

the class DefaultOperationCandidatesProviderTestCase method testGetPropertiesFromPropList.

@Test
public void testGetPropertiesFromPropList() throws Exception {
    List<Property> propList = new ArrayList<>();
    propList.add(new Property("blocking", ModelNode.fromString(list_content)));
    propList.add(new Property("blocking-param", ModelNode.fromString(list_content)));
    propList.add(new Property("start-mode", ModelNode.fromString(list_content)));
    MockCommandContext ctx = new MockCommandContext();
    String operationName = "operationName";
    ctx.parseCommandLine(":" + operationName + "(!blocking", false);
    DefaultOperationRequestAddress address = new DefaultOperationRequestAddress();
    DefaultOperationCandidatesProvider candidatesProvider = new DefaultOperationCandidatesProvider();
    List<CommandArgument> candidates = candidatesProvider.getPropertiesFromPropList(propList, ctx, operationName, address);
    assertEquals(propList.size(), candidates.size());
    for (CommandArgument candidate : candidates) {
        if (candidate.getFullName().equals("blocking"))
            assertFalse("Property blocking can't appear next, since it's completely specified" + " on the commandline as !blocking", candidate.canAppearNext(ctx));
        else
            assertTrue(candidate.canAppearNext(ctx));
    }
}
Also used : CommandArgument(org.jboss.as.cli.CommandArgument) ArrayList(java.util.ArrayList) MockCommandContext(org.jboss.as.cli.completion.mock.MockCommandContext) Property(org.jboss.dmr.Property) Test(org.junit.Test)

Example 10 with CommandArgument

use of org.jboss.as.cli.CommandArgument in project wildfly-core by wildfly.

the class LsHandler method getDynamicOptions.

private Map<String, CommandArgument> getDynamicOptions(CommandContext ctx) throws CommandFormatException {
    if (ctx.getModelControllerClient() == null) {
        return Collections.emptyMap();
    }
    final OperationRequestAddress address = getOperationRequestAddress(ctx);
    if (address.endsOnType()) {
        return Collections.emptyMap();
    }
    final ModelNode req = new ModelNode();
    if (address.isEmpty()) {
        req.get(Util.ADDRESS).setEmptyList();
    } else {
        final ModelNode addrNode = req.get(Util.ADDRESS);
        for (OperationRequestAddress.Node node : address) {
            addrNode.add(node.getType(), node.getName());
        }
    }
    req.get(Util.OPERATION).set(Util.READ_RESOURCE_DESCRIPTION);
    Map<String, CommandArgument> options = Collections.emptyMap();
    try {
        final ModelNode response = ctx.getModelControllerClient().execute(req);
        if (Util.isSuccess(response)) {
            if (response.hasDefined(Util.RESULT)) {
                final ModelNode result = response.get(Util.RESULT);
                if (result.hasDefined(Util.ATTRIBUTES)) {
                    options = new TreeMap<>();
                    ModelNode attributes = result.get(Util.ATTRIBUTES);
                    for (String key : attributes.keys()) {
                        ModelNode attribute = attributes.get(key);
                        for (String k : attribute.keys()) {
                            ArgumentWithoutValue wv = new ArgumentWithoutValue(new CommandHandlerWithArguments() {

                                @Override
                                public boolean isAvailable(CommandContext ctx) {
                                    return LsHandler.this.isAvailable(ctx);
                                }

                                @Override
                                public boolean isBatchMode(CommandContext ctx) {
                                    return LsHandler.this.isBatchMode(ctx);
                                }

                                @Override
                                public void handle(CommandContext ctx) throws CommandLineException {
                                    LsHandler.this.handle(ctx);
                                }

                                @Override
                                public void addArgument(CommandArgument arg) {
                                // Noop.
                                }
                            }, "--" + k);
                            wv.addRequiredPreceding(l);
                            options.put("--" + k, wv);
                        }
                    }
                }
            }
        }
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
    return options;
}
Also used : Node(org.jboss.as.cli.operation.OperationRequestAddress.Node) CommandContext(org.jboss.as.cli.CommandContext) CommandArgument(org.jboss.as.cli.CommandArgument) CommandLineException(org.jboss.as.cli.CommandLineException) OperationFormatException(org.jboss.as.cli.operation.OperationFormatException) IOException(java.io.IOException) CommandLineException(org.jboss.as.cli.CommandLineException) CommandFormatException(org.jboss.as.cli.CommandFormatException) ModelNode(org.jboss.dmr.ModelNode) ArgumentWithoutValue(org.jboss.as.cli.impl.ArgumentWithoutValue) DefaultOperationRequestAddress(org.jboss.as.cli.operation.impl.DefaultOperationRequestAddress) OperationRequestAddress(org.jboss.as.cli.operation.OperationRequestAddress)

Aggregations

CommandArgument (org.jboss.as.cli.CommandArgument)13 CommandFormatException (org.jboss.as.cli.CommandFormatException)8 CommandLineCompleter (org.jboss.as.cli.CommandLineCompleter)6 ArrayList (java.util.ArrayList)4 ParsedCommandLine (org.jboss.as.cli.operation.ParsedCommandLine)4 ModelNode (org.jboss.dmr.ModelNode)4 Property (org.jboss.dmr.Property)4 CommandContext (org.jboss.as.cli.CommandContext)3 CommandLineException (org.jboss.as.cli.CommandLineException)2 CommandLineFormat (org.jboss.as.cli.CommandLineFormat)2 OperationCommand (org.jboss.as.cli.OperationCommand)2 ArgumentWithoutValue (org.jboss.as.cli.impl.ArgumentWithoutValue)2 OperationFormatException (org.jboss.as.cli.operation.OperationFormatException)2 IOException (java.io.IOException)1 ArgumentValueConverter (org.jboss.as.cli.ArgumentValueConverter)1 MockCommandContext (org.jboss.as.cli.completion.mock.MockCommandContext)1 SimpleTabCompleter (org.jboss.as.cli.handlers.SimpleTabCompleter)1 ArgumentWithValue (org.jboss.as.cli.impl.ArgumentWithValue)1 OperationRequestAddress (org.jboss.as.cli.operation.OperationRequestAddress)1 Node (org.jboss.as.cli.operation.OperationRequestAddress.Node)1