Search in sources :

Example 6 with BatchedCommand

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

the class BatchHandler 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 {
    final BatchManager batchManager = ctx.getBatchManager();
    final boolean list = l.isPresent(ctx.getParsedCommandLine());
    final String path = file.getValue(ctx.getParsedCommandLine());
    final String name = this.name.getValue(ctx.getParsedCommandLine());
    if (list) {
        if (path != null || name != null) {
            throw new CommandFormatException("-l is exclusive, neither --file nor name can appear with -l.");
        }
        final Set<String> heldbackNames = batchManager.getHeldbackNames();
        if (!heldbackNames.isEmpty()) {
            List<String> names = new ArrayList<String>(heldbackNames.size());
            for (String heldbackName : heldbackNames) {
                names.add(heldbackName == null ? "<unnamed>" : heldbackName);
            }
            Collections.sort(names);
            for (String heldbackName : names) {
                ctx.printLine(heldbackName);
            }
        }
        return;
    }
    if (batchManager.isBatchActive()) {
        throw new CommandLineException("Can't start a new batch while in batch mode.");
    }
    if (path != null) {
        if (name != null) {
            throw new CommandFormatException("Either --file or name argument can be specified at a time.");
        }
        final File f = new File(path);
        if (!f.exists()) {
            throw new CommandLineException("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();
            final Batch batch = batchManager.getActiveBatch();
            while (line != null) {
                line = line.trim();
                if (!line.isEmpty() && line.charAt(0) != '#') {
                    batch.add(ctx.toBatchedCommand(line));
                }
                line = reader.readLine();
            }
        } catch (IOException e) {
            batchManager.discardActiveBatch();
            throw new CommandLineException("Failed to read file " + f.getAbsolutePath(), e);
        } catch (CommandFormatException e) {
            batchManager.discardActiveBatch();
            throw new CommandLineException("Failed to create batch from " + f.getAbsolutePath(), e);
        } finally {
            if (baseDir != null) {
                ctx.setCurrentDir(currentDir);
            }
        }
        return;
    }
    boolean activated;
    if (batchManager.isHeldback(name)) {
        activated = batchManager.activateHeldbackBatch(name);
        if (activated) {
            final String msg = name == null ? "Re-activated batch" : "Re-activated batch '" + name + "'";
            ctx.printLine(msg);
            List<BatchedCommand> batch = batchManager.getActiveBatch().getCommands();
            if (!batch.isEmpty()) {
                for (int i = 0; i < batch.size(); ++i) {
                    BatchedCommand cmd = batch.get(i);
                    ctx.printLine("#" + (i + 1) + ' ' + cmd.getCommand());
                }
            }
        }
    } else if (name != null) {
        throw new CommandLineException("'" + name + "' not found among the held back batches.");
    } else {
        activated = batchManager.activateNewBatch();
    }
    if (!activated) {
        // that's more like illegal state
        throw new CommandLineException("Failed to activate batch.");
    }
}
Also used : ArrayList(java.util.ArrayList) IOException(java.io.IOException) CommandLineException(org.jboss.as.cli.CommandLineException) Batch(org.jboss.as.cli.batch.Batch) CommandFormatException(org.jboss.as.cli.CommandFormatException) BufferedReader(java.io.BufferedReader) FileReader(java.io.FileReader) BatchManager(org.jboss.as.cli.batch.BatchManager) File(java.io.File) BatchedCommand(org.jboss.as.cli.batch.BatchedCommand)

Example 7 with BatchedCommand

use of org.jboss.as.cli.batch.BatchedCommand 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 8 with BatchedCommand

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

the class DefaultBatch method toRequest.

@Override
public ModelNode toRequest() {
    final ModelNode composite = new ModelNode();
    composite.get(Util.OPERATION).set(Util.COMPOSITE);
    composite.get(Util.ADDRESS).setEmptyList();
    final ModelNode steps = composite.get(Util.STEPS);
    for (BatchedCommand cmd : commands) {
        CommandContext ctx = cmd.getCommandContext();
        ModelNode request = cmd.getRequest();
        if (ctx.getConfig().isValidateOperationRequests()) {
            try {
                ctx.set(Scope.REQUEST, Util.DESCRIPTION_RESPONSE, cmd.getDescriptionResponse());
                ModelNode opDescOutcome = Util.validateRequest(ctx, request);
                if (opDescOutcome != null) {
                    // operation has params that might need to be replaced
                    Util.replaceFilePathsWithBytes(request, opDescOutcome);
                }
            } catch (CommandFormatException ex) {
                throw new RuntimeException(ex);
            } finally {
                ctx.remove(CommandContext.Scope.REQUEST, Util.DESCRIPTION_RESPONSE);
            }
        }
        steps.add(request);
    }
    return composite;
}
Also used : CommandContext(org.jboss.as.cli.CommandContext) CommandFormatException(org.jboss.as.cli.CommandFormatException) ModelNode(org.jboss.dmr.ModelNode) BatchedCommand(org.jboss.as.cli.batch.BatchedCommand)

Example 9 with BatchedCommand

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

the class DefaultBatch method move.

@Override
public void move(int currentIndex, int newIndex) {
    ensureRange(currentIndex);
    ensureRange(newIndex);
    if (currentIndex == newIndex) {
        return;
    }
    BatchedCommand cmd = commands.get(currentIndex);
    int step = newIndex > currentIndex ? 1 : -1;
    for (int i = currentIndex; i != newIndex; i += step) {
        commands.set(i, commands.get(i + step));
    }
    commands.set(newIndex, cmd);
}
Also used : BatchedCommand(org.jboss.as.cli.batch.BatchedCommand)

Aggregations

BatchedCommand (org.jboss.as.cli.batch.BatchedCommand)9 CommandFormatException (org.jboss.as.cli.CommandFormatException)8 Batch (org.jboss.as.cli.batch.Batch)7 CommandLineException (org.jboss.as.cli.CommandLineException)5 IOException (java.io.IOException)4 BatchManager (org.jboss.as.cli.batch.BatchManager)4 ModelNode (org.jboss.dmr.ModelNode)4 File (java.io.File)3 BufferedReader (java.io.BufferedReader)2 FileReader (java.io.FileReader)2 DefaultBatchedCommand (org.jboss.as.cli.batch.impl.DefaultBatchedCommand)2 OperationResponse (org.jboss.as.controller.client.OperationResponse)2 AccessDeniedException (java.nio.file.AccessDeniedException)1 ArrayList (java.util.ArrayList)1 CommandNotFoundException (org.aesh.command.CommandNotFoundException)1 CommandLineParserException (org.aesh.command.parser.CommandLineParserException)1 OptionValidatorException (org.aesh.command.validator.OptionValidatorException)1 CommandContext (org.jboss.as.cli.CommandContext)1 CommandHandler (org.jboss.as.cli.CommandHandler)1 OperationCommand (org.jboss.as.cli.OperationCommand)1