Search in sources :

Example 6 with CommandOption

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

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

the class NoOptionsParser method main.

public static void main(String[] args) throws CommandParsingError {
    String cmdLine = "  -s   -f   argF -a rg2  -mult arg1 -";
    AbstractCommand command = new AbstractCommand("abc", "desc") {

        private static final long serialVersionUID = 0L;

        @Override
        public CommandResult execute(Tool tool, Map<String, CommandOption> options) throws CommandExecutionException {
            return null;
        }

        @Override
        public Map<String, CommandOption> getOptions() {
            return null;
        }
    };
    CommandParser wrapper = new CommandParserImpl();
    Map<String, CommandOption> res = wrapper.parse(command, cmdLine);
    if (res != null) {
        Iterator<CommandOption> optionsIter = res.values().iterator();
        while (optionsIter.hasNext()) {
            System.out.print(optionsIter.next());
        }
    }
}
Also used : CommandOption(com.bluenimble.platform.cli.command.CommandOption) AbstractCommand(com.bluenimble.platform.cli.command.impls.AbstractCommand) CommandParserImpl(com.bluenimble.platform.cli.command.parser.impls.CommandParserImpl) Map(java.util.Map) Tool(com.bluenimble.platform.cli.Tool) CommandParser(com.bluenimble.platform.cli.command.parser.CommandParser)

Example 8 with CommandOption

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

the class SimpleParser method main.

public static void main(String[] args) throws CommandParsingError {
    String cmdLine = "-k teta -p beta";
    AbstractCommand command = new AbstractCommand("use", "desc", "k+,p+") {

        private static final long serialVersionUID = 0L;

        @Override
        public CommandResult execute(Tool tool, Map<String, CommandOption> options) throws CommandExecutionException {
            return null;
        }
    };
    CommandParser parser = new CommandParserImpl();
    Iterator<CommandOption> optionsIter = parser.parse(command, cmdLine).values().iterator();
    while (optionsIter.hasNext()) {
        System.out.println(optionsIter.next());
    }
}
Also used : CommandOption(com.bluenimble.platform.cli.command.CommandOption) AbstractCommand(com.bluenimble.platform.cli.command.impls.AbstractCommand) CommandParserImpl(com.bluenimble.platform.cli.command.parser.impls.CommandParserImpl) Map(java.util.Map) Tool(com.bluenimble.platform.cli.Tool) CommandParser(com.bluenimble.platform.cli.command.parser.CommandParser)

Example 9 with CommandOption

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

the class TypedOptionParser method main.

public static void main(String[] args) throws CommandParsingError {
    String cmdLine = "  -s   -f   argF -a rg2  -multi arg1 -";
    AbstractCommand command = new AbstractCommand("abc", "desc", "s,f+,!multi+") {

        private static final long serialVersionUID = 0L;

        @Override
        public CommandResult execute(Tool tool, Map<String, CommandOption> options) throws CommandExecutionException {
            return null;
        }
    };
    command.getOptions().get("f").setCast(CommandOptionCast.Integer);
    CommandParser parser = new CommandParserImpl();
    Iterator<CommandOption> optionsIter = parser.parse(command, cmdLine).values().iterator();
    while (optionsIter.hasNext()) {
        System.out.println(optionsIter.next());
    }
}
Also used : CommandOption(com.bluenimble.platform.cli.command.CommandOption) AbstractCommand(com.bluenimble.platform.cli.command.impls.AbstractCommand) CommandParserImpl(com.bluenimble.platform.cli.command.parser.impls.CommandParserImpl) Map(java.util.Map) Tool(com.bluenimble.platform.cli.Tool) CommandParser(com.bluenimble.platform.cli.command.parser.CommandParser)

Example 10 with CommandOption

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

the class CommandParserImpl method parse.

@Override
public Map<String, CommandOption> parse(Command command, String cmdLine) throws CommandParsingError {
    Map<String, CommandOption> options = command.getOptions();
    if (cmdLine == null || cmdLine.trim().isEmpty()) {
        if (CommandUtils.hasRequiredOptions(options)) {
            throw new CommandParsingError("Missing required options.\t\n" + command.describe());
        }
        return null;
    }
    cmdLine = cmdLine.trim();
    CommandOption cmdLineOpt = new CommandOptionImpl(CommandOption.CMD_LINE);
    cmdLineOpt.addArg(cmdLine.trim());
    Map<String, CommandOption> result = new HashMap<String, CommandOption>();
    result.put(CommandOption.CMD_LINE, cmdLineOpt);
    if (options == null || options.isEmpty()) {
        return result;
    }
    Iterator<CommandOption> optionsIter = options.values().iterator();
    CommandOption option;
    while (optionsIter.hasNext()) {
        option = optionsIter.next();
        int indexOfOpt = cmdLine.indexOf(Lang.DASH + option.name());
        if (indexOfOpt < 0) {
            if (option.isRequired()) {
                throw new CommandParsingError("missing option: '" + option.label() + "'");
            }
            continue;
        }
        // fix with space before dash
        if (indexOfOpt > 0 && cmdLine.charAt(indexOfOpt - 1) != ' ') {
            continue;
        }
        CommandOption opt = null;
        result.put(option.name(), opt = option.clone());
        readOption(cmdLine, opt, indexOfOpt, options);
    }
    return result;
}
Also used : CommandParsingError(com.bluenimble.platform.cli.command.parser.CommandParsingError) CommandOption(com.bluenimble.platform.cli.command.CommandOption) HashMap(java.util.HashMap) CommandOptionImpl(com.bluenimble.platform.cli.command.impls.CommandOptionImpl)

Aggregations

CommandOption (com.bluenimble.platform.cli.command.CommandOption)13 Map (java.util.Map)6 Tool (com.bluenimble.platform.cli.Tool)4 AbstractCommand (com.bluenimble.platform.cli.command.impls.AbstractCommand)4 CommandParser (com.bluenimble.platform.cli.command.parser.CommandParser)4 CommandParserImpl (com.bluenimble.platform.cli.command.parser.impls.CommandParserImpl)4 CommandExecutionException (com.bluenimble.platform.cli.command.CommandExecutionException)3 File (java.io.File)3 Command (com.bluenimble.platform.cli.command.Command)2 CommandResult (com.bluenimble.platform.cli.command.CommandResult)2 IOException (java.io.IOException)2 InputStream (java.io.InputStream)2 HashMap (java.util.HashMap)2 InstallI18nException (com.bluenimble.platform.cli.InstallI18nException)1 ToolContext (com.bluenimble.platform.cli.ToolContext)1 ToolStartupException (com.bluenimble.platform.cli.ToolStartupException)1 CommandHandler (com.bluenimble.platform.cli.command.CommandHandler)1 CommandOptionImpl (com.bluenimble.platform.cli.command.impls.CommandOptionImpl)1 CommandParsingError (com.bluenimble.platform.cli.command.parser.CommandParsingError)1 FriendlyJsonEmitter (com.bluenimble.platform.cli.printing.impls.FriendlyJsonEmitter)1