use of com.bluenimble.platform.cli.command.impls.DefaultCommandResult in project serverless by bluenimble.
the class JsonCreateHandler method execute.
@Override
public CommandResult execute(Tool tool, String... args) throws CommandExecutionException {
if (args == null || args.length < 1) {
throw new CommandExecutionException("json variable name require");
}
String var = args[0];
@SuppressWarnings("unchecked") Map<String, Object> vars = (Map<String, Object>) tool.getContext(Tool.ROOT_CTX).get(ToolContext.VARS);
JsonObject json = null;
if (args.length > 1) {
try {
json = new JsonObject(args[1]);
} catch (JsonException e) {
throw new CommandExecutionException(e.getMessage(), e);
}
} else {
json = new JsonObject();
}
vars.put(var, json);
return new DefaultCommandResult(CommandResult.OK, json);
}
use of com.bluenimble.platform.cli.command.impls.DefaultCommandResult in project serverless by bluenimble.
the class JsonDeleteHandler 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);
String prop = args[1];
int indexOfDot = prop.indexOf(Lang.DOT);
Object o = vars.get(var);
if (o == null) {
throw new CommandExecutionException("variable '" + var + "' not found");
}
if (!(o instanceof JsonObject) && !(o instanceof JsonArray)) {
throw new CommandExecutionException("variable '" + var + "' isn't a valid json object or array");
}
if ((o instanceof JsonArray) && indexOfDot > 0) {
throw new CommandExecutionException("property '" + prop + "' should be a valid integer since the json variable is an array");
}
if (indexOfDot <= 0) {
if (o instanceof JsonObject) {
((JsonObject) o).remove(prop);
} else {
((JsonArray) o).remove(Integer.parseInt(prop));
}
return new DefaultCommandResult(CommandResult.OK, o);
}
JsonObject json = (JsonObject) o;
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).remove(prop);
return new DefaultCommandResult(CommandResult.OK, json);
} else if (child instanceof JsonArray) {
int iProp = -1;
try {
iProp = Integer.valueOf(prop);
} catch (Exception ex) {
// ignore
}
JsonArray array = (JsonArray) child;
if (iProp > -1 && array.count() > iProp) {
((JsonArray) child).remove(iProp);
return new DefaultCommandResult(CommandResult.OK, json);
}
}
return new DefaultCommandResult(CommandResult.OK, json);
}
use of com.bluenimble.platform.cli.command.impls.DefaultCommandResult in project serverless by bluenimble.
the class JsonJoinHandler 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 sep = Lang.SPACE;
if (args.length > 1) {
sep = args[1];
}
String var = args[0];
String prop = null;
int indexOfSlash = var.indexOf(Lang.SLASH);
if (indexOfSlash > 0) {
prop = var.substring(indexOfSlash + 1);
var = var.substring(0, indexOfSlash);
}
@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;
Object a = json.find(prop, Lang.SLASH);
if (!(a instanceof JsonArray)) {
throw new CommandExecutionException("property '" + prop + "' isn't a valid array");
}
JsonArray array = (JsonArray) a;
try {
return new DefaultCommandResult(CommandResult.OK, join(array, sep));
} catch (JsonException e) {
throw new CommandExecutionException(e.getMessage(), e);
}
}
use of com.bluenimble.platform.cli.command.impls.DefaultCommandResult in project serverless by bluenimble.
the class JsonSaveHandler 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("file path 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 sFile = args[1];
File file = new File(sFile);
boolean overwrite = true;
if (args.length > 2) {
overwrite = !("-check".equals(args[2]));
}
if (file.exists()) {
if (file.isDirectory()) {
throw new CommandExecutionException("file '" + sFile + "' is a folder!");
}
if (!overwrite) {
throw new CommandExecutionException("file '" + sFile + "' already exist");
}
}
try {
Json.store(json, file);
} catch (IOException e) {
throw new CommandExecutionException(e.getMessage(), e);
}
return new DefaultCommandResult(CommandResult.OK, "'" + var + "' saved to " + file.getAbsolutePath());
}
Aggregations