}
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);
}
BufferedReader reader = null;
try {
reader = new BufferedReader(new FileReader(f));
String line = reader.readLine();
batchManager.activateNewBatch();
final Batch batch = batchManager.getActiveBatch();
while(line != null) {
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);
}
if(reader != null) {
try {
reader.close();
} catch (IOException e) {}
}
}
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.");
}
}