Search in sources :

Example 16 with BatchManager

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

the class BatchRunHandler method buildRequestWOValidation.

@Override
protected ModelNode buildRequestWOValidation(CommandContext ctx) throws CommandFormatException {
    final String path = file.getValue(ctx.getParsedCommandLine());
    final ModelNode headersNode = headers.isPresent(ctx.getParsedCommandLine()) ? headers.toModelNode(ctx) : null;
    final BatchManager batchManager = ctx.getBatchManager();
    if (batchManager.isBatchActive()) {
        if (path != null) {
            throw new CommandFormatException("--file is not allowed in the batch mode.");
        }
        final Batch batch = batchManager.getActiveBatch();
        List<BatchedCommand> currentBatch = batch.getCommands();
        if (currentBatch.isEmpty()) {
            batchManager.discardActiveBatch();
            throw new CommandFormatException("The batch is empty.");
        }
        final ModelNode request = batch.toRequest();
        if (headersNode != null) {
            request.get(Util.OPERATION_HEADERS).set(headersNode);
        }
        return request;
    }
    if (path != null) {
        final File f = new File(path);
        if (!f.exists()) {
            throw new CommandFormatException("File " + f.getAbsolutePath() + " does not exist.");
        }
        final File currentDir = ctx.getCurrentDir();
        final File baseDir = f.getParentFile();
        if (baseDir != null) {
            ctx.setCurrentDir(baseDir);
        }
        try (BufferedReader reader = new BufferedReader(new FileReader(f))) {
            String line = reader.readLine();
            batchManager.activateNewBatch();
            while (line != null) {
                ctx.handle(line.trim());
                line = reader.readLine();
            }
            final ModelNode request = batchManager.getActiveBatch().toRequest();
            if (headersNode != null) {
                request.get(Util.OPERATION_HEADERS).set(headersNode);
            }
            return request;
        } catch (IOException e) {
            throw new CommandFormatException("Failed to read file " + f.getAbsolutePath(), e);
        } catch (CommandLineException e) {
            throw new CommandFormatException("Failed to create batch from " + f.getAbsolutePath(), e);
        } finally {
            if (baseDir != null) {
                ctx.setCurrentDir(currentDir);
            }
        }
    }
    throw new CommandFormatException("Without arguments the command can be executed only in the batch mode.");
}
Also used : Batch(org.jboss.as.cli.batch.Batch) CommandFormatException(org.jboss.as.cli.CommandFormatException) BufferedReader(java.io.BufferedReader) FileReader(java.io.FileReader) IOException(java.io.IOException) ModelNode(org.jboss.dmr.ModelNode) BatchManager(org.jboss.as.cli.batch.BatchManager) File(java.io.File) CommandLineException(org.jboss.as.cli.CommandLineException) BatchedCommand(org.jboss.as.cli.batch.BatchedCommand)

Example 17 with BatchManager

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

the class TryCatchFinallyControlFlow method executeBlock.

private void executeBlock(CommandContext ctx, List<String> block, String blockName) throws CommandLineException {
    if (block != null && !block.isEmpty()) {
        final BatchManager batchManager = ctx.getBatchManager();
        // this is to discard a batch started by the block in case the block fails
        // as the cli remains in the batch mode in case run-batch resulted in an error
        final boolean discardActivatedBatch = !batchManager.isBatchActive();
        try {
            for (String l : block) {
                ctx.handle(l);
            }
        } finally {
            if (discardActivatedBatch && batchManager.isBatchActive()) {
                batchManager.discardActiveBatch();
            }
        }
    }
}
Also used : BatchManager(org.jboss.as.cli.batch.BatchManager)

Example 18 with BatchManager

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

the class IfHandler method doHandle.

/* (non-Javadoc)
     * @see org.jboss.as.cli.handlers.CommandHandlerWithHelp#doHandle(org.jboss.as.cli.CommandContext)
     */
@Override
protected void doHandle(CommandContext ctx) throws CommandLineException {
    String argsStr = ctx.getArgumentsString();
    if (argsStr == null) {
        throw new CommandFormatException("The command is missing arguments.");
    }
    final BatchManager batchManager = ctx.getBatchManager();
    if (batchManager.isBatchActive()) {
        throw new CommandFormatException("if is not allowed while in batch mode.");
    }
    final ParsedCommandLine args = ctx.getParsedCommandLine();
    final String conditionStr = this.condition.getOriginalValue(args, true);
    int i = argsStr.indexOf(conditionStr);
    if (i < 0) {
        throw new CommandFormatException("Failed to locate '" + conditionStr + "' in '" + argsStr + "'");
    }
    i = argsStr.indexOf("of", i + conditionStr.length());
    if (i < 0) {
        throw new CommandFormatException("Failed to locate 'of' in '" + argsStr + "'");
    }
    final String requestStr = argsStr.substring(i + 2);
    ctx.registerRedirection(new IfElseControlFlow(ctx, condition.resolveOperation(args), requestStr));
}
Also used : CommandFormatException(org.jboss.as.cli.CommandFormatException) ParsedCommandLine(org.jboss.as.cli.operation.ParsedCommandLine) BatchManager(org.jboss.as.cli.batch.BatchManager)

Aggregations

BatchManager (org.jboss.as.cli.batch.BatchManager)18 CommandFormatException (org.jboss.as.cli.CommandFormatException)10 Batch (org.jboss.as.cli.batch.Batch)7 BatchedCommand (org.jboss.as.cli.batch.BatchedCommand)4 ParsedCommandLine (org.jboss.as.cli.operation.ParsedCommandLine)3 BufferedReader (java.io.BufferedReader)2 File (java.io.File)2 FileReader (java.io.FileReader)2 IOException (java.io.IOException)2 Attachments (org.jboss.as.cli.Attachments)2 CommandLineException (org.jboss.as.cli.CommandLineException)2 ArrayList (java.util.ArrayList)1 ModelNode (org.jboss.dmr.ModelNode)1