UNPKG

@facets-cloud/facetsctl

Version:
111 lines (110 loc) 4.96 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const core_1 = require("@oclif/core"); const Listr = require("listr"); const configuration_1 = require("../utils/configuration"); const FacetsAPI = require("../services/facets-api"); const errors_1 = require("@oclif/core/lib/errors"); class Refresh extends core_1.Command { async run() { const { flags } = await this.parse(Refresh); let envId = flags["environment-id"] || ""; const blueprint = flags["blueprint-name"] || ""; const envName = flags["environment-name"] || ""; if (!envId && !blueprint && !envName) { throw new errors_1.CLIError("Either environment id or blueprint and environment name are required!", { exit: 2 }); } const tasks = new Listr([ { title: "Checking user credentials", task: (ctx) => { try { const config = (0, configuration_1.readConfig)(); ctx.config = config; } catch (error) { if (!error.message) { error.message = "Error while fetching user credentials! Make sure you are logged in before pushing"; } throw new errors_1.CLIError(error, { exit: 2 }); } }, }, { title: "Validating input", task: async (ctx) => { try { ctx.envId = envId; if (!ctx.envId) { if (!blueprint || !envName) { throw new errors_1.CLIError("Both blueprint name and environment name are required if environment id is not provided!", { exit: 2 }); } const clustersRes = await FacetsAPI.getAllClustersByStackName(ctx.config.ControlPlaneURL, ctx.config.Username, ctx.config.AccessToken, blueprint); const clusters = clustersRes.data || []; if (Array.isArray(clusters)) { const cluster = clusters.find((c) => c.name === envName); if (!cluster) throw new errors_1.CLIError("Environment not found!", { exit: 2 }); ctx.envId = cluster.id; } else { throw new errors_1.CLIError("Invalid response from server: Please verify your input and login details.", { exit: 2, }); } } } catch (error) { if (!error.message) { error.message = "Error while validating input or fetching environment ID!"; } throw new errors_1.CLIError(error, { exit: 2 }); } }, }, { title: "Refreshing k8s credentials", task: async (ctx) => { try { await FacetsAPI.refreshCredentials(ctx.config.ControlPlaneURL, ctx.config.Username, ctx.config.AccessToken, ctx.envId); } catch (error) { if (!error.message) { error.message = "Error while refreshing token!"; } throw new errors_1.CLIError(error, { exit: 2 }); } }, }, ]); try { await tasks.run(); } catch (error) { throw error; } } } exports.default = Refresh; Refresh.description = "Refresh your k8s credentials for an environment. Credentials can be refreshed using either an environment id or a combination of blueprint name and environment name."; Refresh.examples = ["<%= config.bin %> <%= command.id %>"]; Refresh.flags = { "environment-id": core_1.Flags.string({ char: "e", description: "Environment id for credentials needs to be refreshed", exclusive: ["environment-name", "blueprint-name"], }), "environment-name": core_1.Flags.string({ description: "Environment Name for credentials needs to be refreshed", dependsOn: ["blueprint-name"], exclusive: ["environment-id"], }), "blueprint-name": core_1.Flags.string({ description: "Blueprint name using which environment was launched", dependsOn: ["environment-name"], exclusive: ["environment-id"], }), }; Refresh.args = [];