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)");
}
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());
}
}
}
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());
}
}
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());
}
}
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;
}
Aggregations