Search in sources :

Example 6 with DefaultCommandResult

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

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

use of com.bluenimble.platform.cli.command.impls.DefaultCommandResult 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)

Example 9 with DefaultCommandResult

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

the class JsonCountHandler method execute.

@Override
public CommandResult execute(Tool tool, String... args) throws CommandExecutionException {
    if (args == null || args.length < 1) {
        throw new CommandExecutionException("json variable name required");
    }
    String var = args[0];
    @SuppressWarnings("unchecked") Map<String, Object> vars = (Map<String, Object>) tool.getContext(Tool.ROOT_CTX).get(ToolContext.VARS);
    Object o = vars.get(var);
    if (o == null) {
        throw new CommandExecutionException("variable '" + var + "' not found");
    }
    if (!(o instanceof JsonObject)) {
        throw new CommandExecutionException("variable '" + var + "' isn't a valid json object");
    }
    JsonObject json = (JsonObject) o;
    if (args.length < 2) {
        return new DefaultCommandResult(CommandResult.OK, json.count());
    }
    String prop = args[1];
    String[] path = Lang.split(prop, Lang.DOT);
    Object child = Json.find(json, path);
    if (child == null) {
        throw new CommandExecutionException(Lang.join(path, Lang.DOT) + " not found");
    }
    if (child instanceof JsonObject) {
        return new DefaultCommandResult(CommandResult.OK, ((JsonObject) child).count());
    } else if (child instanceof JsonArray) {
        return new DefaultCommandResult(CommandResult.OK, ((JsonArray) child).count());
    }
    return new DefaultCommandResult(CommandResult.OK, 0);
}
Also used : JsonArray(com.bluenimble.platform.json.JsonArray) DefaultCommandResult(com.bluenimble.platform.cli.command.impls.DefaultCommandResult) JsonObject(com.bluenimble.platform.json.JsonObject) CommandExecutionException(com.bluenimble.platform.cli.command.CommandExecutionException) JsonObject(com.bluenimble.platform.json.JsonObject) Map(java.util.Map)

Example 10 with DefaultCommandResult

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

the class JsonLoadHandler method execute.

@Override
public CommandResult execute(Tool tool, String... args) throws CommandExecutionException {
    if (args == null || args.length < 1) {
        throw new CommandExecutionException("json variable name required");
    }
    String var = args[0];
    String sFile = null;
    if (args.length > 1) {
        sFile = args[1];
    }
    JsonObject json = null;
    @SuppressWarnings("unchecked") Map<String, Object> vars = (Map<String, Object>) tool.getContext(Tool.ROOT_CTX).get(ToolContext.VARS);
    if (sFile == null) {
        Object vValue = vars.get(var);
        if (vValue == null) {
            throw new CommandExecutionException("variable '" + var + "' not found!");
        }
        try {
            json = new JsonObject(String.valueOf(vValue));
            vars.put(var, json);
        } catch (Exception e) {
            throw new CommandExecutionException(e.getMessage(), e);
        }
        return new DefaultCommandResult(CommandResult.OK, json);
    }
    File file = new File(sFile);
    if (!file.exists()) {
        throw new CommandExecutionException("file " + sFile + " not found!");
    }
    if (!file.isFile()) {
        throw new CommandExecutionException("invalid file " + sFile + ". Are you sure is a file not a folder?");
    }
    try {
        json = Json.load(file);
    } catch (Exception e) {
        throw new CommandExecutionException(e.getMessage(), e);
    }
    vars.put(var, json);
    return new DefaultCommandResult(CommandResult.OK, json);
}
Also used : DefaultCommandResult(com.bluenimble.platform.cli.command.impls.DefaultCommandResult) JsonObject(com.bluenimble.platform.json.JsonObject) CommandExecutionException(com.bluenimble.platform.cli.command.CommandExecutionException) JsonObject(com.bluenimble.platform.json.JsonObject) Map(java.util.Map) File(java.io.File) CommandExecutionException(com.bluenimble.platform.cli.command.CommandExecutionException)

Aggregations

DefaultCommandResult (com.bluenimble.platform.cli.command.impls.DefaultCommandResult)19 CommandExecutionException (com.bluenimble.platform.cli.command.CommandExecutionException)18 JsonObject (com.bluenimble.platform.json.JsonObject)13 Map (java.util.Map)12 File (java.io.File)10 JsonArray (com.bluenimble.platform.json.JsonArray)4 IOException (java.io.IOException)4 HashMap (java.util.HashMap)3 Keys (com.bluenimble.platform.icli.mgm.Keys)2 JsonException (com.bluenimble.platform.json.JsonException)2 FileInputStream (java.io.FileInputStream)2 InputStream (java.io.InputStream)2 OutputStream (java.io.OutputStream)2 StreamPointer (com.bluenimble.platform.cli.command.parser.converters.StreamPointer)1 YamlObject (com.bluenimble.platform.cli.impls.YamlObject)1 HttpEndpoint (com.bluenimble.platform.http.HttpEndpoint)1 HttpHeader (com.bluenimble.platform.http.HttpHeader)1 HttpRequest (com.bluenimble.platform.http.request.HttpRequest)1 HttpResponse (com.bluenimble.platform.http.response.HttpResponse)1 CliSpec (com.bluenimble.platform.icli.mgm.CliSpec)1