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;
}
}
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;
}
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;
}
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));
}
}
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;
}
Aggregations