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