use of com.bluenimble.platform.cli.command.CommandExecutionException in project serverless by bluenimble.
the class PrefixedCommand method execute.
@Override
public CommandResult execute(Tool tool, Map<String, CommandOption> options) throws CommandExecutionException {
String cmd = (String) tool.currentContext().get(ToolContext.CommandLine);
if (Lang.isNullOrEmpty(cmd)) {
throw new CommandExecutionException("Command '" + name + "' missing arguments").showUsage();
}
String subject = cmd;
int indexOfSapce = cmd.indexOf(Lang.SPACE);
if (indexOfSapce > 0) {
subject = cmd.substring(0, indexOfSapce).trim();
cmd = cmd.substring(indexOfSapce + 1);
} else {
cmd = null;
}
CommandHandler handler = handlers.get(subject);
if (handler == null) {
throw new CommandExecutionException("'" + getName() + " " + subject + "' not found");
}
// list of commands
String[] commands = Lang.split(cmd, Lang.SEMICOLON);
if (commands != null && commands.length > 1) {
for (String c : commands) {
c = c.trim();
CommandResult result = handler.execute(tool, args(handler.getArgs(), c));
try {
if (result != null && result.getContent() != null) {
if (result.getType() == CommandResult.OK) {
tool.printer().content(null, String.valueOf(result.getContent()));
} else {
tool.printer().error(String.valueOf(result.getContent()));
}
}
} catch (Exception ex) {
// Ignore
}
}
return null;
}
return handler.execute(tool, args(handler.getArgs(), cmd));
}
use of com.bluenimble.platform.cli.command.CommandExecutionException in project serverless by bluenimble.
the class PrefixedCommand method args.
protected String[] args(CommandHandler.Arg[] args, String cmd) throws CommandExecutionException {
String[] sArgs = args(args == null ? 0 : args.length, cmd);
if (args == null) {
return sArgs;
}
boolean atLeastOneRequired = atLeastOneRequired(args);
if (atLeastOneRequired && (sArgs == null || sArgs.length == 0)) {
throw new CommandExecutionException("missing arguments").showUsage();
}
for (int i = 0; i < args.length; i++) {
CommandHandler.Arg arg = args[i];
if (arg.required() && (sArgs.length - 1) < i) {
throw new CommandExecutionException("missing arguments").showUsage();
}
}
return sArgs;
}
use of com.bluenimble.platform.cli.command.CommandExecutionException in project serverless by bluenimble.
the class ScriptCommand method runCommands.
private CommandResult runCommands(String name, BufferedReader reader, Tool tool, boolean safeMode) throws CommandExecutionException {
try {
int res;
String s;
while ((s = reader.readLine()) != null) {
if (Lang.isNullOrEmpty(s)) {
continue;
}
if (s.trim().startsWith("#")) {
continue;
}
res = tool.processCommand(s);
if (res == Tool.FAILURE && safeMode) {
return new DefaultCommandResult(CommandResult.KO, "'" + name + "' Script is stopped due to errors");
}
}
} catch (IOException e) {
throw new CommandExecutionException(e.getMessage(), e);
} finally {
try {
reader.close();
} catch (IOException ioex) {
// IGNORE
}
}
return null;
}
use of com.bluenimble.platform.cli.command.CommandExecutionException in project serverless by bluenimble.
the class ScriptCommand method execute.
@Override
public CommandResult execute(Tool tool, Map<String, CommandOption> options) throws CommandExecutionException {
CommandOption f = options.get("f");
CommandOption v = options.get("v");
if (f == null && v == null) {
throw new CommandExecutionException("One of the options 'v' or 'f' must be set");
}
String name = null;
BufferedReader reader = null;
if (f != null) {
name = (String) f.getArg(0);
File file = new File(name);
if (!file.exists()) {
throw new CommandExecutionException("File " + name + " not found");
}
if (!file.isFile()) {
throw new CommandExecutionException(name + " is not a valid file");
}
try {
reader = new BufferedReader(new InputStreamReader(new FileInputStream(file)));
} catch (FileNotFoundException e) {
throw new CommandExecutionException(e.getMessage(), e);
}
} else {
name = (String) v.getArg(0);
Object value = tool.currentContext().get(name.trim().toLowerCase());
if (value == null) {
return null;
}
StringBuilder script = new StringBuilder("");
if (value instanceof String[]) {
String[] commands = (String[]) value;
for (String c : commands) {
if (c != null) {
script.append(c).append("\n");
}
}
} else if (Iterable.class.isAssignableFrom(value.getClass())) {
Iterable<?> commands = (Iterable<?>) value;
for (Object c : commands) {
if (c != null) {
script.append(c).append("\n");
}
}
} else {
script.append(value);
}
String s = script.toString().trim();
script.setLength(0);
if (s.isEmpty()) {
return null;
}
reader = new BufferedReader(new StringReader(s));
}
/*
if (reader == null) {
return new DefaultCommandResult (CommandResult.OK, "WARN: Empty Script");
}*/
CommandOption sm = options.get("sm");
if (sm != null) {
tool.writeln("Running script [" + name + "] using safe mode");
}
long start = System.currentTimeMillis();
try {
CommandResult result = runCommands(name, reader, tool, sm != null);
if (result != null) {
return result;
}
} finally {
try {
reader.close();
} catch (IOException ioex) {
// IGNORE
}
}
long end = System.currentTimeMillis();
return new DefaultCommandResult(CommandResult.OK, "\n'" + name + "' executed with success. Time ( " + ((end - start) / 1000) + " seconds)");
}
use of com.bluenimble.platform.cli.command.CommandExecutionException in project serverless by bluenimble.
the class SetCommand method execute.
@SuppressWarnings("unchecked")
@Override
public CommandResult execute(Tool tool, Map<String, CommandOption> options) throws CommandExecutionException {
String cmd = (String) tool.currentContext().get(ToolContext.CommandLine);
if (Lang.isNullOrEmpty(cmd)) {
throw new CommandExecutionException("command '" + name + "' missing arguments").showUsage();
}
String[] nameValue = Lang.split(cmd, Lang.SPACE, true);
String varName = nameValue[0];
Object value = Lang.BLANK;
if (nameValue.length > 1) {
value = nameValue[1];
}
if (value == null) {
value = Lang.BLANK;
}
final Map<String, Object> vars = (Map<String, Object>) tool.getContext(Tool.ROOT_CTX).get(ToolContext.VARS);
try {
if (varName.equals(Tool.ParaPhraseVar)) {
tool.setParaphrase((String) value, true);
tool.printer().content("Security", "Paraphase Updated");
}
if (value instanceof YamlObject) {
value = ((YamlObject) value).toJson();
}
value = varName.equals(Tool.ParaPhraseVar) ? tool.getParaphrase(false) : value;
vars.put(varName, value);
tool.saveVariable(varName, value);
} catch (Exception e) {
throw new CommandExecutionException(e.getMessage(), e);
}
tool.printer().content(varName, String.valueOf(vars.get(varName)));
return null;
}
Aggregations