Search in sources :

Example 26 with CommandExecutionException

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

the class PrefixedCommand method execute.

@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();
    }
    String subject = cmd;
    int indexOfSapce = cmd.indexOf(Lang.SPACE);
    if (indexOfSapce > 0) {
        subject = cmd.substring(0, indexOfSapce).trim();
        cmd = cmd.substring(indexOfSapce + 1);
    } else {
        cmd = null;
    }
    CommandHandler handler = handlers.get(subject);
    if (handler == null) {
        throw new CommandExecutionException("'" + getName() + " " + subject + "' not found");
    }
    // list of commands
    String[] commands = Lang.split(cmd, Lang.SEMICOLON);
    if (commands != null && commands.length > 1) {
        for (String c : commands) {
            c = c.trim();
            CommandResult result = handler.execute(tool, args(handler.getArgs(), c));
            try {
                if (result != null && result.getContent() != null) {
                    if (result.getType() == CommandResult.OK) {
                        tool.printer().content(null, String.valueOf(result.getContent()));
                    } else {
                        tool.printer().error(String.valueOf(result.getContent()));
                    }
                }
            } catch (Exception ex) {
            // Ignore
            }
        }
        return null;
    }
    return handler.execute(tool, args(handler.getArgs(), cmd));
}
Also used : CommandExecutionException(com.bluenimble.platform.cli.command.CommandExecutionException) CommandHandler(com.bluenimble.platform.cli.command.CommandHandler) CommandExecutionException(com.bluenimble.platform.cli.command.CommandExecutionException) CommandResult(com.bluenimble.platform.cli.command.CommandResult)

Example 27 with CommandExecutionException

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

the class PrefixedCommand method args.

protected String[] args(CommandHandler.Arg[] args, String cmd) throws CommandExecutionException {
    String[] sArgs = args(args == null ? 0 : args.length, cmd);
    if (args == null) {
        return sArgs;
    }
    boolean atLeastOneRequired = atLeastOneRequired(args);
    if (atLeastOneRequired && (sArgs == null || sArgs.length == 0)) {
        throw new CommandExecutionException("missing arguments").showUsage();
    }
    for (int i = 0; i < args.length; i++) {
        CommandHandler.Arg arg = args[i];
        if (arg.required() && (sArgs.length - 1) < i) {
            throw new CommandExecutionException("missing arguments").showUsage();
        }
    }
    return sArgs;
}
Also used : CommandExecutionException(com.bluenimble.platform.cli.command.CommandExecutionException) CommandHandler(com.bluenimble.platform.cli.command.CommandHandler)

Example 28 with CommandExecutionException

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

the class ScriptCommand method runCommands.

private CommandResult runCommands(String name, BufferedReader reader, Tool tool, boolean safeMode) throws CommandExecutionException {
    try {
        int res;
        String s;
        while ((s = reader.readLine()) != null) {
            if (Lang.isNullOrEmpty(s)) {
                continue;
            }
            if (s.trim().startsWith("#")) {
                continue;
            }
            res = tool.processCommand(s);
            if (res == Tool.FAILURE && safeMode) {
                return new DefaultCommandResult(CommandResult.KO, "'" + name + "' Script is stopped due to errors");
            }
        }
    } catch (IOException e) {
        throw new CommandExecutionException(e.getMessage(), e);
    } finally {
        try {
            reader.close();
        } catch (IOException ioex) {
        // IGNORE
        }
    }
    return null;
}
Also used : CommandExecutionException(com.bluenimble.platform.cli.command.CommandExecutionException) IOException(java.io.IOException)

Example 29 with CommandExecutionException

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

the class ScriptCommand method execute.

@Override
public CommandResult execute(Tool tool, Map<String, CommandOption> options) throws CommandExecutionException {
    CommandOption f = options.get("f");
    CommandOption v = options.get("v");
    if (f == null && v == null) {
        throw new CommandExecutionException("One of the options 'v' or 'f' must be set");
    }
    String name = null;
    BufferedReader reader = null;
    if (f != null) {
        name = (String) f.getArg(0);
        File file = new File(name);
        if (!file.exists()) {
            throw new CommandExecutionException("File " + name + " not found");
        }
        if (!file.isFile()) {
            throw new CommandExecutionException(name + " is not a valid file");
        }
        try {
            reader = new BufferedReader(new InputStreamReader(new FileInputStream(file)));
        } catch (FileNotFoundException e) {
            throw new CommandExecutionException(e.getMessage(), e);
        }
    } else {
        name = (String) v.getArg(0);
        Object value = tool.currentContext().get(name.trim().toLowerCase());
        if (value == null) {
            return null;
        }
        StringBuilder script = new StringBuilder("");
        if (value instanceof String[]) {
            String[] commands = (String[]) value;
            for (String c : commands) {
                if (c != null) {
                    script.append(c).append("\n");
                }
            }
        } else if (Iterable.class.isAssignableFrom(value.getClass())) {
            Iterable<?> commands = (Iterable<?>) value;
            for (Object c : commands) {
                if (c != null) {
                    script.append(c).append("\n");
                }
            }
        } else {
            script.append(value);
        }
        String s = script.toString().trim();
        script.setLength(0);
        if (s.isEmpty()) {
            return null;
        }
        reader = new BufferedReader(new StringReader(s));
    }
    /*
		if (reader == null) {
			return new DefaultCommandResult (CommandResult.OK, "WARN: Empty Script");
		}*/
    CommandOption sm = options.get("sm");
    if (sm != null) {
        tool.writeln("Running script [" + name + "] using safe mode");
    }
    long start = System.currentTimeMillis();
    try {
        CommandResult result = runCommands(name, reader, tool, sm != null);
        if (result != null) {
            return result;
        }
    } finally {
        try {
            reader.close();
        } catch (IOException ioex) {
        // IGNORE
        }
    }
    long end = System.currentTimeMillis();
    return new DefaultCommandResult(CommandResult.OK, "\n'" + name + "' executed with success. Time ( " + ((end - start) / 1000) + " seconds)");
}
Also used : CommandOption(com.bluenimble.platform.cli.command.CommandOption) InputStreamReader(java.io.InputStreamReader) FileNotFoundException(java.io.FileNotFoundException) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) CommandResult(com.bluenimble.platform.cli.command.CommandResult) BufferedReader(java.io.BufferedReader) StringReader(java.io.StringReader) CommandExecutionException(com.bluenimble.platform.cli.command.CommandExecutionException) File(java.io.File)

Example 30 with CommandExecutionException

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

the class SetCommand 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();
    }
    String[] nameValue = Lang.split(cmd, Lang.SPACE, true);
    String varName = nameValue[0];
    Object value = Lang.BLANK;
    if (nameValue.length > 1) {
        value = nameValue[1];
    }
    if (value == null) {
        value = Lang.BLANK;
    }
    final Map<String, Object> vars = (Map<String, Object>) tool.getContext(Tool.ROOT_CTX).get(ToolContext.VARS);
    try {
        if (varName.equals(Tool.ParaPhraseVar)) {
            tool.setParaphrase((String) value, true);
            tool.printer().content("Security", "Paraphase Updated");
        }
        if (value instanceof YamlObject) {
            value = ((YamlObject) value).toJson();
        }
        value = varName.equals(Tool.ParaPhraseVar) ? tool.getParaphrase(false) : value;
        vars.put(varName, value);
        tool.saveVariable(varName, value);
    } catch (Exception e) {
        throw new CommandExecutionException(e.getMessage(), e);
    }
    tool.printer().content(varName, String.valueOf(vars.get(varName)));
    return null;
}
Also used : CommandExecutionException(com.bluenimble.platform.cli.command.CommandExecutionException) YamlObject(com.bluenimble.platform.cli.impls.YamlObject) YamlObject(com.bluenimble.platform.cli.impls.YamlObject) 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