Search in sources :

Example 66 with ParsedCommandLine

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

the class ArchiveHandler method buildRequestWithoutHeaders.

/* (non-Javadoc)
     * @see org.jboss.as.cli.OperationCommand#buildRequest(org.jboss.as.cli.CommandContext)
     */
@Override
public ModelNode buildRequestWithoutHeaders(CommandContext ctx) throws CommandFormatException {
    ParsedCommandLine args = ctx.getParsedCommandLine();
    final String path = this.path.getValue(args, true);
    final File archive;
    archive = new File(path);
    if (!archive.exists()) {
        throw new OperationFormatException("Path " + archive.getAbsolutePath() + " doesn't exist.");
    }
    if (archive.isDirectory()) {
        throw new OperationFormatException(archive.getAbsolutePath() + " is a directory.");
    }
    File root;
    try {
        root = extractArchive(archive);
    } catch (IOException e) {
        throw new OperationFormatException("Unable to extract archive '" + archive.getAbsolutePath() + "' to temporary location");
    }
    ctx.setCurrentDir(root);
    String holdbackBatch = activateNewBatch(ctx);
    try {
        String script = this.script.getValue(args);
        if (script == null) {
            script = "deploy.scr";
        }
        File scriptFile = new File(ctx.getCurrentDir(), script);
        if (!scriptFile.exists()) {
            throw new CommandFormatException("ERROR: script " + script + "' not found.");
        }
        ctx.printLine("Processing script '" + script + "'.");
        try (BufferedReader reader = new BufferedReader(new FileReader(scriptFile))) {
            String line = reader.readLine();
            while (!ctx.isTerminated() && line != null) {
                ctx.handle(line);
                line = reader.readLine();
            }
        } catch (FileNotFoundException e) {
            throw new CommandFormatException("ERROR: script " + script + "' not found.");
        } catch (IOException e) {
            throw new CommandFormatException(e.getMessage());
        } catch (CommandLineException e) {
            throw new CommandFormatException(e.getMessage());
        }
        ModelNode composite = ctx.getBatchManager().getActiveBatch().toRequest();
        return composite;
    } finally {
        // reset current dir in context
        ctx.setCurrentDir(new File(""));
        discardBatch(ctx, holdbackBatch);
        recursiveDelete(root);
    }
}
Also used : OperationFormatException(org.jboss.as.cli.operation.OperationFormatException) CommandFormatException(org.jboss.as.cli.CommandFormatException) BufferedReader(java.io.BufferedReader) FileNotFoundException(java.io.FileNotFoundException) ParsedCommandLine(org.jboss.as.cli.operation.ParsedCommandLine) FileReader(java.io.FileReader) IOException(java.io.IOException) ModelNode(org.jboss.dmr.ModelNode) JarFile(java.util.jar.JarFile) File(java.io.File) CommandLineException(org.jboss.as.cli.CommandLineException)

Example 67 with ParsedCommandLine

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

the class DeployHandler method doHandle.

@Override
protected void doHandle(CommandContext ctx) throws CommandLineException {
    ParsedCommandLine args = ctx.getParsedCommandLine();
    boolean l = this.l.isPresent(args);
    if (!args.hasProperties() || l) {
        try {
            ListCommand.listDeployments(ctx, l);
        } catch (CommandException ex) {
            throw new CommandLineException(ex.getLocalizedMessage());
        }
        return;
    }
    final boolean unmanaged = this.unmanaged.isPresent(args);
    final String path = this.path.getValue(args);
    final String name = this.name.getValue(args);
    final String rtName = this.rtName.getValue(args);
    final String url = this.url.getValue(args);
    final boolean force = this.force.isPresent(args);
    final boolean disabled = this.disabled.isPresent(args);
    final boolean enabled = this.enabled.isPresent(args);
    final String serverGroups = this.serverGroups.getValue(args);
    final boolean allServerGroups = this.allServerGroups.isPresent(args);
    final ModelNode headersNode = headers.toModelNode(ctx);
    if (headersNode != null && headersNode.getType() != ModelType.OBJECT) {
        throw new CommandFormatException("--headers option has wrong value '" + headersNode + "'");
    }
    if (path == null && url == null) {
        if (name == null) {
            throw new CommandLineException("Filesystem path, --url or --name is " + " required.");
        }
        if (name.equals(ALL)) {
            if (force || disabled) {
                throw new CommandLineException("force and disabled can't be used " + "when deploying all disabled deployments");
            }
            EnableAllCommand command = new EnableAllCommand(ctx);
            command.allServerGroups = allServerGroups;
            command.headers = headersNode;
            command.serverGroups = serverGroups;
            try {
                command.execute(ctx);
            } catch (CommandException ex) {
                throw new CommandLineException(ex.getLocalizedMessage());
            }
            return;
        } else {
            EnableCommand command = new EnableCommand(ctx);
            command.allServerGroups = allServerGroups;
            command.headers = headersNode;
            command.serverGroups = serverGroups;
            command.name = name;
            try {
                command.execute(ctx);
            } catch (CommandException ex) {
                throw new CommandLineException(ex.getLocalizedMessage());
            }
            return;
        }
    }
    if (path != null) {
        if (url != null) {
            throw new CommandLineException("Filesystem path and --url can't be used together.");
        }
        File f = new File(path);
        LegacyBridge c;
        if (DeployArchiveCommand.isCliArchive(f)) {
            DeployArchiveCommand command = new DeployArchiveCommand(ctx);
            command.file = f;
            command.script = this.script.getValue(args);
            c = command;
        } else {
            DeployFileCommand command = new DeployFileCommand(ctx, REPLACE_OPTION);
            command.allServerGroups = allServerGroups;
            command.disabled = disabled;
            command.enabled = enabled;
            command.file = f;
            command.replace = force;
            command.headers = headersNode;
            command.name = name;
            command.runtimeName = rtName;
            command.serverGroups = serverGroups;
            command.unmanaged = unmanaged;
            c = command;
        }
        try {
            c.execute(ctx);
        } catch (CommandException ex) {
            throw new CommandLineException(ex.getLocalizedMessage());
        }
        return;
    }
    if (url != null) {
        if (path != null) {
            throw new CommandLineException("Filesystem path and --url can't be " + "used together.");
        }
        DeployUrlCommand command = new DeployUrlCommand(ctx, REPLACE_OPTION);
        command.allServerGroups = allServerGroups;
        command.disabled = disabled;
        command.enabled = enabled;
        try {
            command.deploymentUrl = new URL(url);
        } catch (MalformedURLException ex) {
            throw new CommandLineException(ex);
        }
        command.replace = force;
        command.headers = headersNode;
        command.runtimeName = rtName;
        command.serverGroups = serverGroups;
        try {
            command.execute(ctx);
        } catch (CommandException ex) {
            throw new CommandLineException(ex.getLocalizedMessage());
        }
    }
}
Also used : LegacyBridge(org.jboss.as.cli.impl.aesh.cmd.LegacyBridge) MalformedURLException(java.net.MalformedURLException) CommandException(org.aesh.command.CommandException) CommandLineException(org.jboss.as.cli.CommandLineException) URL(java.net.URL) DeployUrlCommand(org.jboss.as.cli.impl.aesh.cmd.deployment.DeployUrlCommand) EnableAllCommand(org.jboss.as.cli.impl.aesh.cmd.deployment.EnableAllCommand) CommandFormatException(org.jboss.as.cli.CommandFormatException) EnableCommand(org.jboss.as.cli.impl.aesh.cmd.deployment.EnableCommand) DeployFileCommand(org.jboss.as.cli.impl.aesh.cmd.deployment.DeployFileCommand) ParsedCommandLine(org.jboss.as.cli.operation.ParsedCommandLine) DeployArchiveCommand(org.jboss.as.cli.impl.aesh.cmd.deployment.DeployArchiveCommand) ModelNode(org.jboss.dmr.ModelNode) File(java.io.File)

Example 68 with ParsedCommandLine

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

the class LsHandler method recognizeArguments.

@Override
protected void recognizeArguments(CommandContext ctx) throws CommandFormatException {
    super.recognizeArguments(ctx);
    final ParsedCommandLine parsedCmd = ctx.getParsedCommandLine();
    if (resolve.isPresent(parsedCmd)) {
        ModelNode op = new ModelNode();
        op.get("operation").set("read-operation-description");
        op.get("name").set("read-attribute");
        OperationRequestAddress address = getOperationRequestAddress(ctx);
        op = getAddressNode(ctx, address, op);
        ModelNode returnVal = new ModelNode();
        try {
            if (ctx.getModelControllerClient() != null) {
                returnVal = ctx.getModelControllerClient().execute(op);
            }
        } catch (IOException e) {
            throw new CommandFormatException("Failed to read resource: " + e.getLocalizedMessage(), e);
        }
        if (returnVal.hasDefined("outcome") && returnVal.get("outcome").asString().equals("success")) {
            ModelNode result = returnVal.get("result");
            if (result.hasDefined("request-properties")) {
                ModelNode properties = result.get("request-properties");
                if (!properties.hasDefined("resolve-expressions") && resolve.isPresent(parsedCmd)) {
                    throw new OperationFormatException("Resolve Expression argument not available at this location.");
                }
            }
        }
    }
}
Also used : OperationFormatException(org.jboss.as.cli.operation.OperationFormatException) CommandFormatException(org.jboss.as.cli.CommandFormatException) ParsedCommandLine(org.jboss.as.cli.operation.ParsedCommandLine) IOException(java.io.IOException) ModelNode(org.jboss.dmr.ModelNode) DefaultOperationRequestAddress(org.jboss.as.cli.operation.impl.DefaultOperationRequestAddress) OperationRequestAddress(org.jboss.as.cli.operation.OperationRequestAddress)

Example 69 with ParsedCommandLine

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

the class LsHandler method handleResponse.

@Override
protected void handleResponse(CommandContext ctx, ModelNode outcome, boolean composite) throws CommandLineException {
    final ParsedCommandLine parsedCmd = ctx.getParsedCommandLine();
    if (!composite) {
        final List<ModelNode> nodeList = outcome.get(Util.RESULT).asList();
        if (!nodeList.isEmpty()) {
            final List<String> list = new ArrayList<String>(nodeList.size());
            for (ModelNode node : nodeList) {
                list.add(node.asString());
            }
            printList(ctx, list, l.isPresent(parsedCmd));
        }
        return;
    }
    List<String> additionalProps = Collections.emptyList();
    if (l.isPresent(parsedCmd)) {
        final Set<String> argNames = parsedCmd.getPropertyNames();
        final boolean resolvePresent = resolve.isPresent(parsedCmd);
        final int recognizedArgs = 1 + (resolvePresent ? 1 : 0);
        if (argNames.size() > recognizedArgs) {
            additionalProps = new ArrayList<String>(argNames.size() - recognizedArgs);
            int i = 0;
            for (String arg : argNames) {
                if (arg.equals(l.getFullName()) || resolvePresent && arg.equals(resolve.getFullName())) {
                    continue;
                }
                final String prop;
                if (arg.length() > 1 && arg.charAt(0) == '-') {
                    if (arg.charAt(1) == '-') {
                        prop = arg.substring(2);
                    } else {
                        prop = arg.substring(1);
                    }
                } else {
                    prop = arg;
                }
                additionalProps.add(prop);
            }
        }
    }
    ModelNode resultNode = outcome.get(Util.RESULT);
    ModelNode attrDescriptions = null;
    ModelNode childDescriptions = null;
    if (resultNode.hasDefined(Util.STEP_3)) {
        final ModelNode stepOutcome = resultNode.get(Util.STEP_3);
        if (Util.isSuccess(stepOutcome)) {
            if (stepOutcome.hasDefined(Util.RESULT)) {
                final ModelNode descrResult = stepOutcome.get(Util.RESULT);
                if (descrResult.hasDefined(Util.ATTRIBUTES)) {
                    attrDescriptions = descrResult.get(Util.ATTRIBUTES);
                }
                if (descrResult.hasDefined(Util.CHILDREN)) {
                    childDescriptions = descrResult.get(Util.CHILDREN);
                }
            } else {
                throw new CommandFormatException("Result is not available for read-resource-description request: " + outcome);
            }
        } else {
            throw new CommandFormatException("Failed to get resource description: " + outcome);
        }
    }
    List<String> names = null;
    List<String> typeNames = null;
    if (resultNode.hasDefined(Util.STEP_1)) {
        ModelNode typesOutcome = resultNode.get(Util.STEP_1);
        if (Util.isSuccess(typesOutcome)) {
            if (typesOutcome.hasDefined(Util.RESULT)) {
                final ModelNode resourceResult = typesOutcome.get(Util.RESULT);
                final List<ModelNode> types = resourceResult.asList();
                if (!types.isEmpty()) {
                    typeNames = new ArrayList<String>();
                    for (ModelNode type : types) {
                        typeNames.add(type.asString());
                    }
                    if (childDescriptions == null && attrDescriptions == null) {
                        names = typeNames;
                    }
                }
            } else {
                throw new CommandFormatException("Result is not available for read-children-types request: " + outcome);
            }
        } else {
            throw new CommandFormatException("Failed to fetch type names: " + outcome);
        }
    } else {
        throw new CommandFormatException("The result for children type names is not available: " + outcome);
    }
    if (resultNode.hasDefined(Util.STEP_2)) {
        ModelNode resourceOutcome = resultNode.get(Util.STEP_2);
        if (Util.isSuccess(resourceOutcome)) {
            if (resourceOutcome.hasDefined(Util.RESULT)) {
                final ModelNode resourceResult = resourceOutcome.get(Util.RESULT);
                final List<Property> props = resourceResult.asPropertyList();
                if (!props.isEmpty()) {
                    final SimpleTable attrTable;
                    if (attrDescriptions == null) {
                        attrTable = null;
                    } else {
                        if (!additionalProps.isEmpty()) {
                            String[] headers = new String[3 + additionalProps.size()];
                            headers[0] = "ATTRIBUTE";
                            headers[1] = "VALUE";
                            headers[2] = "TYPE";
                            int i = 3;
                            for (String additional : additionalProps) {
                                headers[i++] = additional.toUpperCase(Locale.ENGLISH);
                            }
                            attrTable = new SimpleTable(headers, ctx.getTerminalWidth());
                        } else {
                            attrTable = new SimpleTable(new String[] { "ATTRIBUTE", "VALUE", "TYPE" }, ctx.getTerminalWidth());
                        }
                    }
                    SimpleTable childrenTable = childDescriptions == null ? null : new SimpleTable(new String[] { "CHILD", "MIN-OCCURS", "MAX-OCCURS" }, ctx.getTerminalWidth());
                    if (typeNames == null && attrTable == null && childrenTable == null) {
                        typeNames = new ArrayList<String>();
                        names = typeNames;
                    }
                    for (Property prop : props) {
                        final StringBuilder buf = new StringBuilder();
                        if (typeNames == null || !typeNames.contains(prop.getName())) {
                            if (attrDescriptions == null) {
                                buf.append(prop.getName());
                                buf.append('=');
                                buf.append(prop.getValue().asString());
                                // TODO the value should be formatted nicer but the current
                                // formatter uses new lines for complex value which doesn't work here
                                // final ModelNode value = prop.getValue();
                                // ModelNodeFormatter.Factory.forType(value.getType()).format(buf, 0, value);
                                typeNames.add(buf.toString());
                                buf.setLength(0);
                            } else {
                                final String[] line = new String[attrTable.columnsTotal()];
                                line[0] = prop.getName();
                                line[1] = prop.getValue().asString();
                                if (attrDescriptions.hasDefined(prop.getName())) {
                                    final ModelNode attrDescr = attrDescriptions.get(prop.getName());
                                    line[2] = getAsString(attrDescr, Util.TYPE);
                                    if (!additionalProps.isEmpty()) {
                                        int i = 3;
                                        for (String additional : additionalProps) {
                                            line[i++] = getAsString(attrDescr, additional);
                                        }
                                    }
                                } else {
                                    for (int i = 2; i < line.length; ++i) {
                                        line[i] = "n/a";
                                    }
                                }
                                attrTable.addLine(line);
                            }
                        } else if (childDescriptions != null) {
                            if (childDescriptions.hasDefined(prop.getName())) {
                                final ModelNode childDescr = childDescriptions.get(prop.getName());
                                final Integer maxOccurs = getAsInteger(childDescr, Util.MAX_OCCURS);
                                childrenTable.addLine(new String[] { prop.getName(), getAsString(childDescr, Util.MIN_OCCURS), maxOccurs == null ? "n/a" : (maxOccurs == Integer.MAX_VALUE ? "unbounded" : maxOccurs.toString()) });
                            } else {
                                childrenTable.addLine(new String[] { prop.getName(), "n/a", "n/a" });
                            }
                        }
                    }
                    StringBuilder buf = null;
                    if (attrTable != null && !attrTable.isEmpty()) {
                        buf = new StringBuilder();
                        attrTable.append(buf, true);
                    }
                    if (childrenTable != null && !childrenTable.isEmpty()) {
                        if (buf == null) {
                            buf = new StringBuilder();
                        } else {
                            buf.append("\n\n");
                        }
                        childrenTable.append(buf, true);
                    }
                    if (buf != null) {
                        ctx.printLine(buf.toString());
                    }
                }
            } else {
                throw new CommandFormatException("Result is not available for read-resource request: " + outcome);
            }
        } else {
            throw new CommandFormatException("Failed to fetch attributes: " + outcome);
        }
    } else {
        throw new CommandFormatException("The result for attributes is not available: " + outcome);
    }
    if (names != null) {
        printList(ctx, names, l.isPresent(parsedCmd));
    }
}
Also used : ArrayList(java.util.ArrayList) SimpleTable(org.jboss.as.cli.util.SimpleTable) CommandFormatException(org.jboss.as.cli.CommandFormatException) ParsedCommandLine(org.jboss.as.cli.operation.ParsedCommandLine) ModelNode(org.jboss.dmr.ModelNode) Property(org.jboss.dmr.Property)

Example 70 with ParsedCommandLine

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

the class EchoVariableHandler method doHandle.

/* (non-Javadoc)
     * @see org.jboss.as.cli.handlers.CommandHandlerWithHelp#handle(org.jboss.as.cli.CommandContext, java.lang.String)
     */
@Override
protected void doHandle(CommandContext ctx) throws CommandFormatException {
    final ParsedCommandLine line = ctx.getParsedCommandLine();
    String result = line.getSubstitutedLine().trim().substring(line.getOperationName().length()).trim();
    if (ctx.isResolveParameterValues()) {
        result = CLIExpressionResolver.resolve(result);
    }
    // apply escape rules
    int i = result.indexOf('\\');
    if (i >= 0) {
        final StringBuilder buf = new StringBuilder(result.length() - 1);
        buf.append(result.substring(0, i));
        boolean escaped = true;
        while (++i < result.length()) {
            if (escaped) {
                buf.append(result.charAt(i));
                escaped = false;
            } else {
                final char c = result.charAt(i);
                if (c == '\\') {
                    escaped = true;
                } else {
                    buf.append(c);
                }
            }
        }
        result = buf.toString();
    }
    ctx.printLine(result);
}
Also used : ParsedCommandLine(org.jboss.as.cli.operation.ParsedCommandLine)

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