Search in sources :

Example 1 with YamlStringPrinter

use of com.bluenimble.platform.json.printers.YamlStringPrinter 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)

Aggregations

CommandExecutionException (com.bluenimble.platform.cli.command.CommandExecutionException)1 DefaultCommandResult (com.bluenimble.platform.cli.command.impls.DefaultCommandResult)1 Templates (com.bluenimble.platform.icli.mgm.CliSpec.Templates)1 JsonObject (com.bluenimble.platform.json.JsonObject)1 YamlStringPrinter (com.bluenimble.platform.json.printers.YamlStringPrinter)1 File (java.io.File)1 Map (java.util.Map)1