Search in sources :

Example 16 with DefaultCommandResult

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);
}
Also used : JsonException(com.bluenimble.platform.json.JsonException) DefaultCommandResult(com.bluenimble.platform.cli.command.impls.DefaultCommandResult) JsonObject(com.bluenimble.platform.json.JsonObject) CommandExecutionException(com.bluenimble.platform.cli.command.CommandExecutionException) JsonObject(com.bluenimble.platform.json.JsonObject) Map(java.util.Map)

Example 17 with DefaultCommandResult

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);
}
Also used : JsonArray(com.bluenimble.platform.json.JsonArray) DefaultCommandResult(com.bluenimble.platform.cli.command.impls.DefaultCommandResult) JsonObject(com.bluenimble.platform.json.JsonObject) CommandExecutionException(com.bluenimble.platform.cli.command.CommandExecutionException) JsonObject(com.bluenimble.platform.json.JsonObject) Map(java.util.Map) CommandExecutionException(com.bluenimble.platform.cli.command.CommandExecutionException)

Example 18 with DefaultCommandResult

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);
    }
}
Also used : JsonArray(com.bluenimble.platform.json.JsonArray) JsonException(com.bluenimble.platform.json.JsonException) DefaultCommandResult(com.bluenimble.platform.cli.command.impls.DefaultCommandResult) JsonObject(com.bluenimble.platform.json.JsonObject) CommandExecutionException(com.bluenimble.platform.cli.command.CommandExecutionException) JsonObject(com.bluenimble.platform.json.JsonObject) Map(java.util.Map)

Example 19 with DefaultCommandResult

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());
}
Also used : DefaultCommandResult(com.bluenimble.platform.cli.command.impls.DefaultCommandResult) JsonObject(com.bluenimble.platform.json.JsonObject) CommandExecutionException(com.bluenimble.platform.cli.command.CommandExecutionException) JsonObject(com.bluenimble.platform.json.JsonObject) IOException(java.io.IOException) Map(java.util.Map) File(java.io.File)

Aggregations

DefaultCommandResult (com.bluenimble.platform.cli.command.impls.DefaultCommandResult)19 CommandExecutionException (com.bluenimble.platform.cli.command.CommandExecutionException)18 JsonObject (com.bluenimble.platform.json.JsonObject)13 Map (java.util.Map)12 File (java.io.File)10 JsonArray (com.bluenimble.platform.json.JsonArray)4 IOException (java.io.IOException)4 HashMap (java.util.HashMap)3 Keys (com.bluenimble.platform.icli.mgm.Keys)2 JsonException (com.bluenimble.platform.json.JsonException)2 FileInputStream (java.io.FileInputStream)2 InputStream (java.io.InputStream)2 OutputStream (java.io.OutputStream)2 StreamPointer (com.bluenimble.platform.cli.command.parser.converters.StreamPointer)1 YamlObject (com.bluenimble.platform.cli.impls.YamlObject)1 HttpEndpoint (com.bluenimble.platform.http.HttpEndpoint)1 HttpHeader (com.bluenimble.platform.http.HttpHeader)1 HttpRequest (com.bluenimble.platform.http.request.HttpRequest)1 HttpResponse (com.bluenimble.platform.http.response.HttpResponse)1 CliSpec (com.bluenimble.platform.icli.mgm.CliSpec)1