use of com.bluenimble.platform.cli.command.CommandExecutionException in project serverless by bluenimble.
the class WGetCommand method execute.
@SuppressWarnings("unchecked")
@Override
public CommandResult execute(Tool tool, Map<String, CommandOption> options) throws CommandExecutionException {
String sUrl = (String) options.get("u").getArg(0);
CommandOption varOpt = options.get("v");
CommandOption fileOpt = options.get("f");
InputStream in = null;
Writer writer = null;
try {
if (varOpt != null) {
writer = new StringWriter();
} else if (fileOpt != null) {
writer = new FileWriter(new File((String) fileOpt.getArg(0)));
} else {
writer = new PrintWriter(System.out);
}
in = new URL(sUrl).openStream();
IOUtils.copy(in, writer);
if (varOpt != null) {
Map<String, Object> vars = (Map<String, Object>) tool.getContext(Tool.ROOT_CTX).get(ToolContext.VARS);
vars.put((String) varOpt.getArg(0), writer.toString());
}
} catch (Throwable th) {
throw new CommandExecutionException(th.getMessage(), th);
} finally {
IOUtils.closeQuietly(writer);
IOUtils.closeQuietly(in);
}
return new DefaultCommandResult(CommandResult.OK, "Command Executed with success");
}
use of com.bluenimble.platform.cli.command.CommandExecutionException in project serverless by bluenimble.
the class JsonCountHandler method execute.
@Override
public CommandResult execute(Tool tool, String... args) throws CommandExecutionException {
if (args == null || args.length < 1) {
throw new CommandExecutionException("json variable name required");
}
String var = args[0];
@SuppressWarnings("unchecked") Map<String, Object> vars = (Map<String, Object>) tool.getContext(Tool.ROOT_CTX).get(ToolContext.VARS);
Object o = vars.get(var);
if (o == null) {
throw new CommandExecutionException("variable '" + var + "' not found");
}
if (!(o instanceof JsonObject)) {
throw new CommandExecutionException("variable '" + var + "' isn't a valid json object");
}
JsonObject json = (JsonObject) o;
if (args.length < 2) {
return new DefaultCommandResult(CommandResult.OK, json.count());
}
String prop = args[1];
String[] path = Lang.split(prop, Lang.DOT);
Object child = Json.find(json, path);
if (child == null) {
throw new CommandExecutionException(Lang.join(path, Lang.DOT) + " not found");
}
if (child instanceof JsonObject) {
return new DefaultCommandResult(CommandResult.OK, ((JsonObject) child).count());
} else if (child instanceof JsonArray) {
return new DefaultCommandResult(CommandResult.OK, ((JsonArray) child).count());
}
return new DefaultCommandResult(CommandResult.OK, 0);
}
use of com.bluenimble.platform.cli.command.CommandExecutionException in project serverless by bluenimble.
the class JsonLoadHandler method execute.
@Override
public CommandResult execute(Tool tool, String... args) throws CommandExecutionException {
if (args == null || args.length < 1) {
throw new CommandExecutionException("json variable name required");
}
String var = args[0];
String sFile = null;
if (args.length > 1) {
sFile = args[1];
}
JsonObject json = null;
@SuppressWarnings("unchecked") Map<String, Object> vars = (Map<String, Object>) tool.getContext(Tool.ROOT_CTX).get(ToolContext.VARS);
if (sFile == null) {
Object vValue = vars.get(var);
if (vValue == null) {
throw new CommandExecutionException("variable '" + var + "' not found!");
}
try {
json = new JsonObject(String.valueOf(vValue));
vars.put(var, json);
} catch (Exception e) {
throw new CommandExecutionException(e.getMessage(), e);
}
return new DefaultCommandResult(CommandResult.OK, json);
}
File file = new File(sFile);
if (!file.exists()) {
throw new CommandExecutionException("file " + sFile + " not found!");
}
if (!file.isFile()) {
throw new CommandExecutionException("invalid file " + sFile + ". Are you sure is a file not a folder?");
}
try {
json = Json.load(file);
} catch (Exception e) {
throw new CommandExecutionException(e.getMessage(), e);
}
vars.put(var, json);
return new DefaultCommandResult(CommandResult.OK, json);
}
use of com.bluenimble.platform.cli.command.CommandExecutionException in project serverless by bluenimble.
the class JsonSetHandler method execute.
@Override
public CommandResult execute(Tool tool, String... args) throws CommandExecutionException {
if (args == null || args.length < 1) {
throw new CommandExecutionException("json variable name required");
}
if (args.length < 2) {
throw new CommandExecutionException("json property required");
}
String var = args[0];
@SuppressWarnings("unchecked") Map<String, Object> vars = (Map<String, Object>) tool.getContext(Tool.ROOT_CTX).get(ToolContext.VARS);
Object o = vars.get(var);
if (o == null) {
throw new CommandExecutionException("variable '" + var + "' not found");
}
if (!(o instanceof JsonObject)) {
throw new CommandExecutionException("variable '" + var + "' isn't a valid json object");
}
JsonObject json = (JsonObject) o;
String prop = args[1];
// it's a delete
int indexOfDot = prop.indexOf(Lang.DOT);
String value = args[2];
boolean isJson = false;
boolean isVar = false;
Object oValue = null;
if (value.startsWith("j\\")) {
value = value.substring(3);
isJson = true;
}
if (value.startsWith("v\\")) {
value = value.substring(3);
isVar = true;
}
if (Lang.isNullOrEmpty(value)) {
isJson = false;
}
oValue = value;
if (isJson) {
try {
oValue = JsonParser.parse(value);
} catch (Exception ex) {
throw new CommandExecutionException(ex.getMessage(), ex);
}
}
if (isVar) {
oValue = vars.get(value);
}
if (indexOfDot <= 0) {
json.set(prop, oValue);
} else {
String[] path = Lang.split(prop, Lang.DOT);
prop = path[path.length - 1];
path = Lang.moveRight(path, 1);
Object child = Json.find(json, path);
if (child == null) {
throw new CommandExecutionException(Lang.join(path, Lang.DOT) + " not found");
}
if (child instanceof JsonObject) {
((JsonObject) child).set(prop, oValue);
} else if (child instanceof JsonArray) {
boolean append = false;
int iProp = -1;
if (prop.equals(Lang.UNDERSCORE)) {
append = true;
} else {
try {
iProp = Integer.valueOf(prop);
} catch (Exception ex) {
// ignore
}
}
JsonArray array = (JsonArray) child;
if (append) {
array.add(oValue);
} else {
if (iProp > -1 && array.count() > iProp) {
((JsonArray) child).add(iProp, oValue);
}
}
}
}
return new DefaultCommandResult(CommandResult.OK, json);
}
use of com.bluenimble.platform.cli.command.CommandExecutionException in project serverless by bluenimble.
the class UnSetCommand 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();
}
final Map<String, Object> vars = (Map<String, Object>) tool.getContext(Tool.ROOT_CTX).get(ToolContext.VARS);
String varName = cmd.trim();
if (varName.equals(Tool.ParaPhraseVar)) {
throw new CommandExecutionException("can't remove paraphrase");
}
vars.remove(varName);
try {
tool.saveVariable(varName, varName.equals(Tool.ParaPhraseVar) ? tool.getParaphrase(false) : null);
} catch (Exception e) {
throw new CommandExecutionException(e.getMessage(), e);
}
tool.printer().content(cmd, "variable " + varName + " removed");
return null;
}
Aggregations