Search in sources :

Example 16 with ParsedCommandLine

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

the class AbstractDistributionCommand method createPatchOperationTarget.

PatchOperationTarget createPatchOperationTarget(CommandContext ctx) throws CommandException {
    final PatchOperationTarget target;
    final ParsedCommandLine args = ctx.getParsedCommandLine();
    if (ctx.getModelControllerClient() != null) {
        if (distribution != null) {
            throw new CommandException("--distribution is not allowed when connected to the controller.");
        }
        if (modulePath != null) {
            throw new CommandException("--module-path is not allowed when connected to the controller.");
        }
        if (bundlePath != null) {
            throw new CommandException("--bundle-path is not allowed when connected to the controller.");
        }
        if (ctx.isDomainMode()) {
            target = PatchOperationTarget.createHost(host, ctx.getModelControllerClient());
        } else {
            target = PatchOperationTarget.createStandalone(ctx.getModelControllerClient());
        }
    } else {
        final File root = getJBossHome();
        final List<File> modules = getFSArgument(modulePath, args, root, "modules");
        final List<File> bundles = getFSArgument(bundlePath, args, root, "bundles");
        try {
            target = PatchOperationTarget.createLocal(root, modules, bundles);
        } catch (Exception e) {
            throw new CommandException("Unable to apply patch to local JBOSS_HOME=" + root, e);
        }
    }
    return target;
}
Also used : ParsedCommandLine(org.jboss.as.cli.operation.ParsedCommandLine) CommandException(org.aesh.command.CommandException) File(java.io.File) PatchOperationTarget(org.jboss.as.patching.tool.PatchOperationTarget) OptionValidatorException(org.aesh.command.validator.OptionValidatorException) CommandException(org.aesh.command.CommandException)

Example 17 with ParsedCommandLine

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

the class PatchHandler method doHandle.

@Override
protected void doHandle(CommandContext ctx) throws CommandLineException {
    final ParsedCommandLine parsedLine = ctx.getParsedCommandLine();
    if (host.isPresent(parsedLine) && !ctx.isDomainMode()) {
        throw new CommandFormatException("The --host option is not available in the current context. Connection to the controller might be unavailable or not running in domain mode.");
    }
    final String action = this.action.getValue(parsedLine);
    if (INSPECT.equals(action)) {
        doInspect(ctx);
        return;
    }
    final PatchOperationTarget target = createPatchOperationTarget(ctx);
    final PatchOperationBuilder builder = createPatchOperationBuilder(parsedLine);
    final ModelNode response;
    try {
        response = builder.execute(target);
    } catch (Exception e) {
        throw new CommandLineException(action + " failed", e);
    }
    if (!Util.isSuccess(response)) {
        final ModelNode fd = response.get(ModelDescriptionConstants.FAILURE_DESCRIPTION);
        if (!fd.isDefined()) {
            throw new CommandLineException("Failed to apply patch: " + response.asString());
        }
        if (fd.has(Constants.CONFLICTS)) {
            final StringBuilder buf = new StringBuilder();
            buf.append(fd.get(Constants.MESSAGE).asString()).append(": ");
            final ModelNode conflicts = fd.get(Constants.CONFLICTS);
            String title = "";
            if (conflicts.has(Constants.BUNDLES)) {
                formatConflictsList(buf, conflicts, "", Constants.BUNDLES);
                title = ", ";
            }
            if (conflicts.has(Constants.MODULES)) {
                formatConflictsList(buf, conflicts, title, Constants.MODULES);
                title = ", ";
            }
            if (conflicts.has(Constants.MISC)) {
                formatConflictsList(buf, conflicts, title, Constants.MISC);
            }
            buf.append(lineSeparator).append("Use the --override-all, --override=[] or --preserve=[] arguments in order to resolve the conflict.");
            throw new CommandLineException(buf.toString());
        } else {
            throw new CommandLineException(Util.getFailureDescription(response));
        }
    }
    if (INFO.equals(action)) {
        if (patchId.getValue(parsedLine) != null) {
            final ModelNode result = response.get(ModelDescriptionConstants.RESULT);
            if (!result.isDefined()) {
                return;
            }
            SimpleTable table = new SimpleTable(2, ctx.getTerminalWidth());
            table.addLine(new String[] { "Patch ID:", result.get(Constants.PATCH_ID).asString() });
            table.addLine(new String[] { "Type:", result.get(Constants.TYPE).asString() });
            table.addLine(new String[] { "Identity name:", result.get(Constants.IDENTITY_NAME).asString() });
            table.addLine(new String[] { "Identity version:", result.get(Constants.IDENTITY_VERSION).asString() });
            table.addLine(new String[] { "Description:", result.get(Constants.DESCRIPTION).asString() });
            if (result.hasDefined(Constants.LINK)) {
                table.addLine(new String[] { "Link:", result.get(Constants.LINK).asString() });
            }
            ctx.printLine(table.toString(false));
            final ModelNode elements = result.get(Constants.ELEMENTS);
            if (elements.isDefined()) {
                ctx.printLine("");
                ctx.printLine("ELEMENTS");
                for (ModelNode e : elements.asList()) {
                    table = new SimpleTable(2, ctx.getTerminalWidth());
                    table.addLine(new String[] { "Patch ID:", e.get(Constants.PATCH_ID).asString() });
                    table.addLine(new String[] { "Name:", e.get(Constants.NAME).asString() });
                    table.addLine(new String[] { "Type:", e.get(Constants.TYPE).asString() });
                    table.addLine(new String[] { "Description:", e.get(Constants.DESCRIPTION).asString() });
                    ctx.printLine("");
                    ctx.printLine(table.toString(false));
                }
            }
        } else if (jsonOutput.isPresent(parsedLine)) {
            ctx.printLine(response.toJSONString(false));
        } else if (streams.isPresent(parsedLine)) {
            final List<ModelNode> list = response.get(ModelDescriptionConstants.RESULT).asList();
            if (list.size() == 1) {
                ctx.printLine(list.get(0).asString());
            } else {
                final List<String> streams = new ArrayList<String>(list.size());
                for (ModelNode stream : list) {
                    streams.add(stream.asString());
                }
                ctx.printColumns(streams);
            }
        } else {
            final ModelNode result = response.get(ModelDescriptionConstants.RESULT);
            if (!result.isDefined()) {
                return;
            }
            SimpleTable table = new SimpleTable(2, ctx.getTerminalWidth());
            table.addLine(new String[] { "Version:", result.get(Constants.VERSION).asString() });
            addPatchesInfo(result, table);
            ctx.printLine(table.toString(false));
            if (verbose.isPresent(parsedLine)) {
                printLayerPatches(ctx, result, Constants.ADD_ON);
                printLayerPatches(ctx, result, Constants.LAYER);
            }
        }
    } else {
        ctx.printLine(response.toJSONString(false));
    }
}
Also used : SimpleTable(org.jboss.as.cli.util.SimpleTable) ArrayList(java.util.ArrayList) PatchOperationBuilder(org.jboss.as.patching.tool.PatchOperationBuilder) XMLStreamException(javax.xml.stream.XMLStreamException) ZipException(java.util.zip.ZipException) CommandFormatException(org.jboss.as.cli.CommandFormatException) PatchingException(org.jboss.as.patching.PatchingException) IOException(java.io.IOException) CommandLineException(org.jboss.as.cli.CommandLineException) CommandLineException(org.jboss.as.cli.CommandLineException) CommandFormatException(org.jboss.as.cli.CommandFormatException) ParsedCommandLine(org.jboss.as.cli.operation.ParsedCommandLine) ModelNode(org.jboss.dmr.ModelNode) PatchOperationTarget(org.jboss.as.patching.tool.PatchOperationTarget)

Example 18 with ParsedCommandLine

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

the class VariablesTestCase method testEscapingVariableName.

@Test
public void testEscapingVariableName() throws Exception {
    final ParsedCommandLine parsed = parse("set v=\\$" + OP_VAR_NAME);
    assertEquals("set", parsed.getOperationName());
    final List<String> props = parsed.getOtherProperties();
    assertEquals(1, props.size());
    assertEquals("v=\\$" + OP_VAR_NAME, props.get(0));
}
Also used : ParsedCommandLine(org.jboss.as.cli.operation.ParsedCommandLine) Test(org.junit.Test)

Example 19 with ParsedCommandLine

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

the class VariablesTestCase method testCommandArgumentNameAndValue.

@Test
public void testCommandArgumentNameAndValue() throws Exception {
    final ParsedCommandLine parsed = parse("command-name --$" + OP_PROP_VAR_NAME + "=$" + OP_PROP_VAR_NAME);
    assertEquals("command-name", parsed.getOperationName());
    // variables unlike system properties are always resolved
    assertEquals(parsed.getPropertyValue("--" + OP_PROP_VAR_VALUE), OP_PROP_VAR_VALUE);
}
Also used : ParsedCommandLine(org.jboss.as.cli.operation.ParsedCommandLine) Test(org.junit.Test)

Example 20 with ParsedCommandLine

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

the class VariablesTestCase method testNodeType.

@Test
public void testNodeType() throws Exception {
    final ParsedCommandLine parsed = parse("/$" + NODE_TYPE_VAR_NAME + "=test:op");
    final OperationRequestAddress address = parsed.getAddress();
    assertNotNull(address);
    assertEquals(NODE_TYPE_VAR_VALUE, address.getNodeType());
    assertEquals("test", address.getNodeName());
}
Also used : ParsedCommandLine(org.jboss.as.cli.operation.ParsedCommandLine) OperationRequestAddress(org.jboss.as.cli.operation.OperationRequestAddress) Test(org.junit.Test)

Aggregations

ParsedCommandLine (org.jboss.as.cli.operation.ParsedCommandLine)95 Test (org.junit.Test)42 CommandFormatException (org.jboss.as.cli.CommandFormatException)38 ModelNode (org.jboss.dmr.ModelNode)26 CommandLineException (org.jboss.as.cli.CommandLineException)14 OperationRequestAddress (org.jboss.as.cli.operation.OperationRequestAddress)14 File (java.io.File)12 IOException (java.io.IOException)10 ArrayList (java.util.ArrayList)9 DefaultOperationRequestAddress (org.jboss.as.cli.operation.impl.DefaultOperationRequestAddress)8 ModelControllerClient (org.jboss.as.controller.client.ModelControllerClient)8 CommandArgument (org.jboss.as.cli.CommandArgument)4 CommandContext (org.jboss.as.cli.CommandContext)4 OperationFormatException (org.jboss.as.cli.operation.OperationFormatException)4 ZipException (java.util.zip.ZipException)3 XMLStreamException (javax.xml.stream.XMLStreamException)3 CommandException (org.aesh.command.CommandException)3 BatchManager (org.jboss.as.cli.batch.BatchManager)3 CommandLineParser (org.jboss.as.cli.operation.CommandLineParser)3 DefaultCallbackHandler (org.jboss.as.cli.operation.impl.DefaultCallbackHandler)3