Search in sources :

Example 11 with CommandExecutionException

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

the class AbstractTool method onException.

private void onException(Command command, Throwable th) throws IOException {
    th = Lang.getRootCause(th);
    StackTraceElement ste = th.getStackTrace()[0];
    printer().error(th.getMessage() == null ? (th.getClass().getSimpleName() + Lang.SPACE + "File: " + ste.getFileName().substring(0, ste.getFileName().lastIndexOf(Lang.DOT)) + ", Line: " + ste.getLineNumber()) : th.getMessage());
    if (th instanceof CommandExecutionException) {
        CommandExecutionException ceex = (CommandExecutionException) th;
        if (ceex.shouldShowUsage()) {
            printer().content("Command " + command.getName(), command.describe());
        }
    }
}
Also used : CommandExecutionException(com.bluenimble.platform.cli.command.CommandExecutionException)

Example 12 with CommandExecutionException

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

the class KeysCommand method execute.

@Override
public CommandResult execute(Tool tool, Map<String, CommandOption> options) throws CommandExecutionException {
    String cmd = (String) tool.currentContext().get(ToolContext.CommandLine);
    try {
        if ("reload".equals(cmd)) {
            BlueNimble.loadKeys(tool);
        }
        Map<String, Keys> secrets = BlueNimble.allKeys();
        if (secrets == null || secrets.isEmpty()) {
            tool.printer().info("No keys found!");
            return null;
        }
        Iterator<String> secIter = secrets.keySet().iterator();
        while (secIter.hasNext()) {
            String sname = secIter.next();
            Keys s = secrets.get(sname);
            boolean current = BlueNimble.keys() != null && sname.equals(BlueNimble.keys().alias());
            tool.printer().content(// __PS__YELLOW:(C) _|_keyAlias
            current ? PrintSpec.Start + Colors.Yellow + PrintSpec.TextSep + "(C) " + PrintSpec.Split + sname : sname, (Lang.isNullOrEmpty(s.name()) ? Lang.BLANK : "      Name | " + s.name() + Lang.ENDLN) + (Lang.isNullOrEmpty(s.domain()) ? Lang.BLANK : "  Endpoint | " + s.domain() + Lang.ENDLN) + (Lang.isNullOrEmpty(s.issuer()) ? Lang.BLANK : "    Issuer | " + s.issuer() + Lang.ENDLN) + (Lang.isNullOrEmpty(s.whenIssued()) ? Lang.BLANK : "    Issued | " + s.whenIssued() + Lang.ENDLN) + (Lang.isNullOrEmpty(s.expiresOn()) ? Lang.BLANK : "   Expires | " + s.expiresOn() + Lang.ENDLN) + (Lang.isNullOrEmpty(s.stamp()) ? Lang.BLANK : "     Stamp | " + s.stamp() + Lang.ENDLN) + "Access Key | " + s.accessKey());
        }
    } catch (Exception ex) {
        throw new CommandExecutionException(ex.getMessage(), ex);
    }
    return new DefaultCommandResult(CommandResult.OK, null);
}
Also used : DefaultCommandResult(com.bluenimble.platform.cli.command.impls.DefaultCommandResult) Keys(com.bluenimble.platform.icli.mgm.Keys) CommandExecutionException(com.bluenimble.platform.cli.command.CommandExecutionException) CommandExecutionException(com.bluenimble.platform.cli.command.CommandExecutionException)

Example 13 with CommandExecutionException

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

the class WorkspaceCommand method execute.

@Override
public CommandResult execute(Tool tool, Map<String, CommandOption> options) throws CommandExecutionException {
    String workFolder = (String) tool.currentContext().get(ToolContext.CommandLine);
    try {
        if (Lang.isNullOrEmpty(workFolder)) {
            if (BlueNimble.Workspace == null) {
                tool.printer().warning("No workspace found!\nUse command 'ws [Your Workspace Foler Path]'.\nExample: ws /applications/bluenimble");
            } else {
                tool.printer().content("Workspace", BlueNimble.Workspace.getAbsolutePath());
                String currentApi = Json.getString(BlueNimble.Config, CliSpec.Config.CurrentApi);
                JsonObject apis = Json.getObject(BlueNimble.Config, CliSpec.Config.Apis);
                if (apis != null && !apis.isEmpty()) {
                    StringBuilder sApis = new StringBuilder();
                    Iterator<String> keys = apis.keys();
                    while (keys.hasNext()) {
                        String api = keys.next();
                        String prefix = "    ";
                        if (api.equals(currentApi)) {
                            prefix = "(C) ";
                        }
                        sApis.append(prefix + api).append(Lang.ENDLN);
                    }
                    tool.printer().content("Apis", sApis.toString());
                    sApis.setLength(0);
                } else {
                    tool.printer().content("Apis", "No apis found in this workspace");
                }
            }
            return null;
        }
    } catch (Exception ex) {
        throw new CommandExecutionException(ex.getMessage(), ex);
    }
    File workspace = new File(workFolder);
    if (!workspace.exists() || !workspace.isDirectory()) {
        throw new CommandExecutionException(workFolder + " is not a valid folder");
    }
    try {
        BlueNimble.workspace(workspace);
    } catch (Exception ex) {
        throw new CommandExecutionException(ex.getMessage(), ex);
    }
    return new DefaultCommandResult(CommandResult.OK, "workspace sat to '" + workspace.getAbsolutePath() + "'");
}
Also used : DefaultCommandResult(com.bluenimble.platform.cli.command.impls.DefaultCommandResult) JsonObject(com.bluenimble.platform.json.JsonObject) CommandExecutionException(com.bluenimble.platform.cli.command.CommandExecutionException) File(java.io.File) CommandExecutionException(com.bluenimble.platform.cli.command.CommandExecutionException)

Example 14 with CommandExecutionException

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

the class CreateApiHandler method execute.

@SuppressWarnings("unchecked")
@Override
public CommandResult execute(Tool tool, String... args) throws CommandExecutionException {
    if (args == null || args.length < 1) {
        throw new CommandExecutionException("api namespace required. ex. create api myapi");
    }
    String namespace = args[0];
    if (!Pattern.matches("^[a-zA-Z0-9_-]*$", namespace)) {
        throw new CommandExecutionException("api namespace must contain only numbers, letters, '_' and '-'");
    }
    String sApiFolder = namespace;
    if (args.length > 1) {
        sApiFolder = args[1];
    }
    File apiFolder = new File(BlueNimble.Workspace, sApiFolder);
    if (apiFolder.exists()) {
        try {
            tool.printer().info(sApiFolder + " already exists");
            String exists = Json.getString(Json.getObject(BlueNimble.Config, CliSpec.Config.Apis), namespace);
            if (!Lang.isNullOrEmpty(exists)) {
                return null;
            }
            tool.printer().info("binding to the current workspace");
            String ns = BlueNimble.loadApi(apiFolder);
            if (ns == null) {
                tool.printer().warning("unable to bind current api. Please check if the api.json exists and the namespace is there.");
            }
            Json.getObject(BlueNimble.Config, CliSpec.Config.Apis).set(ns, sApiFolder);
            BlueNimble.Config.set(CliSpec.Config.CurrentApi, sApiFolder);
        } catch (Exception ex) {
            throw new CommandExecutionException(ex.getMessage(), ex);
        }
    }
    Map<String, Object> vars = (Map<String, Object>) tool.getContext(Tool.ROOT_CTX).get(ToolContext.VARS);
    String template = (String) vars.get(BlueNimble.DefaultVars.TemplateApi);
    if (Lang.isNullOrEmpty(template)) {
        template = DefaultTemplate;
    }
    File fTemplate = new File(new File(BlueNimble.Home, Templates.class.getSimpleName().toLowerCase() + Lang.SLASH + Templates.Apis), template);
    if (!fTemplate.exists()) {
        throw new CommandExecutionException("api template '" + template + "' not installed");
    }
    apiFolder.mkdir();
    try {
        FileUtils.copy(fTemplate, apiFolder, false);
    } catch (Exception ex) {
        throw new CommandExecutionException(ex.getMessage(), ex);
    }
    String specLang = (String) vars.get(BlueNimble.DefaultVars.SpecLanguage);
    if (Lang.isNullOrEmpty(specLang)) {
        specLang = BlueNimble.SpecLangs.Json;
    }
    CodeGenUtils.writeAll(apiFolder, (JsonObject) new JsonObject().set(Namespace, namespace), specLang);
    BlueNimble.Config.set(CliSpec.Config.CurrentApi, namespace);
    JsonObject oApis = Json.getObject(BlueNimble.Config, CliSpec.Config.Apis);
    if (oApis == null) {
        oApis = new JsonObject();
        BlueNimble.Config.set(CliSpec.Config.Apis, oApis);
    }
    oApis.set(namespace, sApiFolder);
    boolean secure = false;
    String sSecure = (String) vars.get(BlueNimble.DefaultVars.ApiSecurityEnabled);
    if (Lang.isNullOrEmpty(sSecure)) {
        secure = false;
    } else {
        secure = Lang.TrueValues.contains(sSecure.trim().toLowerCase());
    }
    try {
        JsonObject apiSpec = SpecUtils.read(apiFolder);
        JsonObject codeGen = Json.getObject(apiSpec, "_codegen_");
        if (codeGen != null) {
            secure = Json.getBoolean(codeGen, "secure", true);
            apiSpec.remove("_codegen_");
            SpecUtils.write(apiFolder, apiSpec);
        }
        BlueNimble.saveConfig();
        tool.printer().content("Api '" + namespace + "' created! path: $ws/ " + sApiFolder, BlueNimble.SpecLangs.Json.equals(specLang) ? apiSpec.toString(2) : new YamlStringPrinter(2).print(apiSpec).toString());
    } catch (Exception e) {
        throw new CommandExecutionException(e.getMessage(), e);
    }
    if (secure) {
        SecureApiHandler.execute(tool, new String[] { namespace, "token+signature", Lang.STAR });
    }
    return new DefaultCommandResult(CommandResult.OK, null);
}
Also used : DefaultCommandResult(com.bluenimble.platform.cli.command.impls.DefaultCommandResult) Templates(com.bluenimble.platform.icli.mgm.CliSpec.Templates) JsonObject(com.bluenimble.platform.json.JsonObject) CommandExecutionException(com.bluenimble.platform.cli.command.CommandExecutionException) JsonObject(com.bluenimble.platform.json.JsonObject) YamlStringPrinter(com.bluenimble.platform.json.printers.YamlStringPrinter) File(java.io.File) Map(java.util.Map) CommandExecutionException(com.bluenimble.platform.cli.command.CommandExecutionException)

Example 15 with CommandExecutionException

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

the class CreateServiceHandler method execute.

@Override
public CommandResult execute(Tool tool, String... args) throws CommandExecutionException {
    if (args == null || args.length < 2) {
        throw new CommandExecutionException("service verb and model required. Ex. 'create service get user' or 'create service * offer'");
    }
    String currentApi = Json.getString(BlueNimble.Config, CliSpec.Config.CurrentApi);
    if (Lang.isNullOrEmpty(currentApi)) {
        throw new CommandExecutionException("target api not set. Set target using the 'api' command. Ex. api myapi");
    }
    String apiPath = Json.getString(Json.getObject(BlueNimble.Config, CliSpec.Config.Apis), currentApi);
    if (Lang.isNullOrEmpty(apiPath)) {
        throw new CommandExecutionException("api path not found for '" + currentApi + "'");
    }
    File apiFolder = new File(BlueNimble.Workspace, apiPath);
    if (!apiFolder.exists() || !apiFolder.isDirectory()) {
        throw new CommandExecutionException("invalid api folder '" + apiPath + "'");
    }
    String verb = args[0];
    String model = args[1];
    File resourcesFolder = new File(apiFolder, "resources");
    File servicesFolder = new File(resourcesFolder, "services");
    File functionsFolder = new File(resourcesFolder, "functions");
    CodeGenUtils.writeService((AbstractTool) tool, verb, model, servicesFolder, functionsFolder);
    return new DefaultCommandResult(CommandResult.OK, null);
}
Also used : DefaultCommandResult(com.bluenimble.platform.cli.command.impls.DefaultCommandResult) CommandExecutionException(com.bluenimble.platform.cli.command.CommandExecutionException) File(java.io.File)

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