Search in sources :

Example 16 with CommandExecutionException

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");
}
Also used : CommandOption(com.bluenimble.platform.cli.command.CommandOption) InputStream(java.io.InputStream) FileWriter(java.io.FileWriter) URL(java.net.URL) StringWriter(java.io.StringWriter) CommandExecutionException(com.bluenimble.platform.cli.command.CommandExecutionException) File(java.io.File) Map(java.util.Map) PrintWriter(java.io.PrintWriter) StringWriter(java.io.StringWriter) FileWriter(java.io.FileWriter) Writer(java.io.Writer) PrintWriter(java.io.PrintWriter)

Example 17 with CommandExecutionException

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);
}
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)

Example 18 with CommandExecutionException

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);
}
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) Map(java.util.Map) File(java.io.File) CommandExecutionException(com.bluenimble.platform.cli.command.CommandExecutionException)

Example 19 with CommandExecutionException

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

Example 20 with CommandExecutionException

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

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