UNPKG

@veecode-platform/safira-cli

Version:

Generate a microservice project from your spec.

162 lines (161 loc) 7.88 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const tslib_1 = require("tslib"); const core_1 = require("@oclif/core"); const inquirer_1 = tslib_1.__importDefault(require("inquirer")); const git_providers_1 = require("../../git/git-providers"); const credentials_1 = require("../../vkpr/credentials/credentials"); const color_1 = tslib_1.__importDefault(require("@oclif/color")); const cd_provider_enum_1 = require("../../ci-cd/cd-provider-enum"); const install_1 = tslib_1.__importDefault(require("../argocd/install")); const git_commands_1 = require("../../git/git-commands"); const file_system_utils_1 = require("../../utils/file-system-utils"); const git_utils_1 = require("../../git/git-utils"); const setup_1 = require("../../ci-cd/setup"); const safira_utils_1 = require("../../safira-project/safira-utils"); const docker_registry_1 = require("../../docker/docker-registry"); const set_1 = tslib_1.__importDefault(require("../docker/credential/set")); const constants_1 = require("../../utils/commands/constants"); const cache_manager_1 = require("../../cache/cache-manager"); class ProjectCicdConfigure extends core_1.Command { async run() { const currentPath = file_system_utils_1.FileSystemUtils.getCurrentPath(); if (!safira_utils_1.SafiraUtils.isSafiraProject()) { this.log(color_1.default.bold(color_1.default.yellowBright("You need to run this command in a Safira project"))); return; } const { args, flags } = await this.parse(ProjectCicdConfigure); const gitProviderList = Object.keys(git_providers_1.GitProviderEnum); const cdProviderList = Object.keys(cd_provider_enum_1.CdProviderEnum); const dockerRegistryList = Object.keys(docker_registry_1.DockerRegistryEnum); this.log("This command will help you configure your CI/CD pipeline for your project using some GIT provider and ArgoCD"); this.log("This project must be versioned into a git repository"); const cicdProvider = await inquirer_1.default.prompt([ { when: () => gitProviderList.length > 1, type: "list", name: "ci-provider", message: "Which service to use as CI?", choices: gitProviderList, }, { when: () => cdProviderList.length > 1, type: "list", name: "cd-provider", message: "Which service to use as CD(Code Delivery)?", choices: cdProviderList, }, { type: "list", name: "setup-cd", message: answers => `Do you want to setup ${(answers["cd-provider"] || cdProviderList[0]).toString().toUpperCase()}?`, choices: constants_1.yesNoList, }, ]); const cdProvider = cdProviderList.length === 1 ? cdProviderList[0] : cicdProvider["cd-provider"]; const gitProvider = gitProviderList.length === 1 ? gitProviderList[0] : cicdProvider["ci-provider"]; const dockerCredExists = credentials_1.Credentials.instance.exists(credentials_1.CredentialsKey.dockerRegistry); if (!dockerCredExists) await this._askByDockerCredentials(); if (cicdProvider["setup-cd"] === "yes") { await this._askByCdProvider(cdProvider); } const gitCredExists = credentials_1.Credentials.instance.exists(credentials_1.CredentialsKey[gitProvider]); if (!gitCredExists) await git_utils_1.GitUtils.askByGitCredentials(gitProvider); const iac = cache_manager_1.CacheManager.instance.getCache(cache_manager_1.CacheName.iac); const origin = await git_commands_1.GitCommand.instance.getRemoteOriginUrl(); const repo = await git_utils_1.GitUtils.getRepositoryData(origin); const repoName = `${repo.owner}/${repo.name}`; const answers = await inquirer_1.default.prompt([ { type: "input", name: "branch-name", message: "Type the branch name to configure the CI/CD pipeline", default: "develop", }, { type: "input", name: "kubernetes-namespace", message: answer => `Type the kubernetes namespace to deploy the application from branch ${answer["branch-name"]}`, default: (answer) => answer["branch-name"], }, { when: iac.length > 0, type: "list", name: "iac-server", message: "Select the repository to store Argo Artifacts?", choices: [...iac, { name: "input other repository", value: "other path" }], }, { when: answers => iac.length === 0 || answers["iac-server"] === "other path", type: "input", name: "iac-server-single", message: "Enter with the repository to store Argo Artifacts:", default: "owner/repository", validate: (value) => { if (value.length > 1) return true; return "Please enter a valid repository"; }, }, { type: "list", name: "question-argo-subfolder", message: "Select the subfolder to store Argo Artifacts?", default: "yes", choices: constants_1.yesNoList, }, { when: answer => answer["question-argo-subfolder"] === "yes", type: "input", name: "argo-subfolder", message: "Path", validate: (value) => { if (value.length > 1) return true; return "Please enter a valid path"; }, }, ]); if (answers["iac-server-single"]) cache_manager_1.CacheManager.instance.addItem(cache_manager_1.CacheName.iac, answers["iac-server-single"]); console.log(answers); const params = { "project-path": currentPath, "project-name": repoName, "project-folder": git_utils_1.GitUtils.validatePath(answers["iac-server"] || answers["iac-server-single"]) || "./", "project-subfolder": answers["argo-subfolder"] ? git_utils_1.GitUtils.validatePath(answers["argo-subfolder"]) : "./", "branch-name": answers["branch-name"], "kubernetes-namespace": answers["kubernetes-namespace"], "ci-provider": gitProvider, "cd-provider": cdProvider, "docker-registry": docker_registry_1.DockerRegistryEnum.dockerRegistry, }; setup_1.CiCdSetup.instance.setup(params); } async _askByDockerCredentials() { this.log(color_1.default.bold(color_1.default.yellowBright("You need to configure your credentials for docker Registry"))); await set_1.default.run([]); } async _askByCdProvider(provider) { this.log(color_1.default.bold(color_1.default.yellowBright(`Setup ${provider} in your kubernetes cluster`))); switch (provider) { case cd_provider_enum_1.CdProviderEnum.argocd: await install_1.default.run([]); break; default: throw new Error("CD Provider not implemented"); } } } exports.default = ProjectCicdConfigure; ProjectCicdConfigure.hidden = true; ProjectCicdConfigure.description = "Help you configure your CI/CD pipeline for your project"; ProjectCicdConfigure.examples = [ "<%= config.bin %> <%= command.id %>", ]; ProjectCicdConfigure.flags = { help: core_1.Flags.help({}), }; ProjectCicdConfigure.args = [];