Search in sources :

Example 31 with CommandExecutionException

use of com.bluenimble.platform.cli.command.CommandExecutionException in project serverless by bluenimble.

the class ApiCommand method execute.

@Override
public CommandResult execute(Tool tool, Map<String, CommandOption> options) throws CommandExecutionException {
    JsonObject config = BlueNimble.Config;
    String apiNs = (String) tool.currentContext().get(ToolContext.CommandLine);
    if (Lang.isNullOrEmpty(apiNs)) {
        String currentApi = Json.getString(config, CliSpec.Config.CurrentApi);
        try {
            if (Lang.isNullOrEmpty(currentApi)) {
                tool.printer().info("use ' api YourApi ' or create a new one using ' create api YourApi '");
            } else {
                tool.printer().content("Current Api", "namespace: " + currentApi + "\npath: $ws/ " + Json.getString(Json.getObject(config, CliSpec.Config.Apis), currentApi));
            }
            return null;
        } catch (Exception ex) {
            throw new CommandExecutionException(ex.getMessage(), ex);
        }
    }
    apiNs = apiNs.trim();
    String apiPath = Json.getString(Json.getObject(config, CliSpec.Config.Apis), apiNs);
    if (Lang.isNullOrEmpty(apiPath)) {
        tool.printer().info("api '" + apiNs + "' not found in workspace");
        return null;
    }
    config.set(CliSpec.Config.CurrentApi, apiNs);
    try {
        BlueNimble.saveConfig();
        tool.printer().content("Current Api", "namespace: " + apiNs + "\npath: $ws/ " + Json.getString(Json.getObject(config, CliSpec.Config.Apis), apiNs));
    } catch (Exception ex) {
        throw new CommandExecutionException(ex.getMessage(), ex);
    }
    return new DefaultCommandResult(CommandResult.OK, null);
}
Also used : DefaultCommandResult(com.bluenimble.platform.cli.command.impls.DefaultCommandResult) JsonObject(com.bluenimble.platform.json.JsonObject) CommandExecutionException(com.bluenimble.platform.cli.command.CommandExecutionException) CommandExecutionException(com.bluenimble.platform.cli.command.CommandExecutionException)

Example 32 with CommandExecutionException

use of com.bluenimble.platform.cli.command.CommandExecutionException 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 33 with CommandExecutionException

use of com.bluenimble.platform.cli.command.CommandExecutionException 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 34 with CommandExecutionException

use of com.bluenimble.platform.cli.command.CommandExecutionException 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 35 with CommandExecutionException

use of com.bluenimble.platform.cli.command.CommandExecutionException 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

CommandExecutionException (com.bluenimble.platform.cli.command.CommandExecutionException)35 Map (java.util.Map)20 DefaultCommandResult (com.bluenimble.platform.cli.command.impls.DefaultCommandResult)19 JsonObject (com.bluenimble.platform.json.JsonObject)16 File (java.io.File)16 IOException (java.io.IOException)8 FileInputStream (java.io.FileInputStream)7 InputStream (java.io.InputStream)7 OutputStream (java.io.OutputStream)5 HashMap (java.util.HashMap)5 CommandResult (com.bluenimble.platform.cli.command.CommandResult)4 JsonArray (com.bluenimble.platform.json.JsonArray)4 ByteArrayInputStream (java.io.ByteArrayInputStream)4 FileOutputStream (java.io.FileOutputStream)4 CommandOption (com.bluenimble.platform.cli.command.CommandOption)3 Keys (com.bluenimble.platform.icli.mgm.Keys)3 InputStreamReader (java.io.InputStreamReader)3 CommandHandler (com.bluenimble.platform.cli.command.CommandHandler)2 YamlObject (com.bluenimble.platform.cli.impls.YamlObject)2 Templates (com.bluenimble.platform.icli.mgm.CliSpec.Templates)2