use of com.bluenimble.platform.cli.ToolContext in project serverless by bluenimble.
the class AbstractTool method onCommand.
protected String onCommand(String commandName, String params) {
ToolContext toolContext = currentContext();
toolContext.put(ToolContext.CommandName, commandName);
if (params == null) {
toolContext.remove(ToolContext.CommandLine);
return params;
}
params = params.trim();
toolContext.put(ToolContext.CommandLine, params);
return params;
}
use of com.bluenimble.platform.cli.ToolContext in project serverless by bluenimble.
the class BlueNimble method loadCommands.
private void loadCommands(File commands) {
if (!commands.exists() || !commands.isDirectory()) {
commands.mkdir();
return;
}
File[] folders = commands.listFiles(new FileFilter() {
@Override
public boolean accept(File f) {
return f.isDirectory();
}
});
for (File fld : folders) {
String ctx = fld.getName();
if (GlobalContext.equals(ctx)) {
ctx = Tool.ROOT_CTX;
} else {
ToolContext toolContext = getContext(ctx);
if (toolContext == null) {
addContext(new ToolContextImpl(ctx));
}
}
File[] files = fld.listFiles(new FileFilter() {
@Override
public boolean accept(File f) {
return f.isFile() && f.getName().endsWith(".json");
}
});
String name = null;
for (File f : files) {
name = f.getName().substring(0, f.getName().indexOf(JsonExt));
try {
addCommand(new RemoteCommand(ctx, name, Json.load(f)));
} catch (Exception e) {
System.out.println("ERROR: Can't load command " + name + ". Cause: " + e.getMessage());
}
}
}
}
use of com.bluenimble.platform.cli.ToolContext in project serverless by bluenimble.
the class BlueNimble method loadScripts.
private void loadScripts(File scripts) {
if (!scripts.exists() || !scripts.isDirectory()) {
scripts.mkdir();
return;
}
File[] folders = scripts.listFiles(new FileFilter() {
@Override
public boolean accept(File f) {
return f.isDirectory();
}
});
for (File fld : folders) {
String ctx = fld.getName();
if (GlobalContext.equals(ctx)) {
ctx = Tool.ROOT_CTX;
} else {
ToolContext toolContext = getContext(ctx);
if (toolContext == null) {
addContext(new ToolContextImpl(ctx));
}
}
File[] files = fld.listFiles(new FileFilter() {
@Override
public boolean accept(File f) {
return f.isFile() && (f.getName().endsWith(MacroExt) || f.getName().endsWith(ScriptExt));
}
});
String name = null;
for (File f : files) {
name = f.getName().substring(0, f.getName().lastIndexOf(Lang.DOT));
try {
Command c = null;
if (f.getName().endsWith(MacroExt)) {
c = new MacroSourceCommand(ctx, name, f);
} else if (f.getName().endsWith(ScriptExt)) {
c = new ScriptSourceCommand(ctx, name, f);
}
if (c != null) {
addCommand(c);
}
} catch (Exception e) {
System.out.println("ERROR: Can't load command " + name + ". Cause: " + e.getMessage());
}
}
}
}
use of com.bluenimble.platform.cli.ToolContext in project serverless by bluenimble.
the class ChangeContextCommand method execute.
@Override
public CommandResult execute(Tool tool, Map<String, CommandOption> options) throws CommandExecutionException {
CommandOption co = options.get(CommandOption.CMD_LINE);
if (co == null || co.getArg(0) == null) {
return new DefaultCommandResult(CommandResult.KO, "Specify a valid context name");
}
String contextName = (String) co.getArg(0);
contextName = contextName.toLowerCase();
ToolContext ctx = tool.getContext(contextName);
if (ctx == null) {
return new DefaultCommandResult(CommandResult.KO, "Context [" + contextName + "] not registered with this tool");
}
tool.changeContext(ctx);
return new DefaultCommandResult(CommandResult.OK, "Context changed to [" + contextName + "]");
}
Aggregations