UNPKG

@facets-cloud/facetsctl

Version:
74 lines (73 loc) 2.6 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const core_1 = require("@oclif/core"); const configuration_1 = require("../utils/configuration"); const FacetsAPI = require("../services/facets-api"); class Login extends core_1.Command { constructor() { super(...arguments); this.showOriginalError = false; } async run() { const { flags } = await this.parse(Login); const username = flags.username || ""; const token = flags["access-token"] || ""; const cpUrl = flags["cp-url"] || ""; this.showOriginalError = flags.debug === "true"; core_1.CliUx.ux.action.start("Logging in"); try { // verify user credentials const result = await FacetsAPI.fetchUserDetails(cpUrl, username, token); if (result.status === 200) { // save config file and exit const config = { ControlPlaneURL: cpUrl, Username: username, AccessToken: token, }; (0, configuration_1.writeConfigWithPermissions)(config); core_1.CliUx.ux.action.stop(); // shows 'starting a process... done' } } catch (error) { // Log actual error in logs core_1.CliUx.ux.action.stop("Failed"); if (this.showOriginalError) { throw error; } if ((error === null || error === void 0 ? void 0 : error.code) === "ENOTFOUND") { error.message = "Incorrect URL of Control Plane!"; } if ((error === null || error === void 0 ? void 0 : error.code) === "ERR_BAD_REQUEST") { error.message = "Incorrect username or token!"; } throw error; } } } exports.default = Login; Login.description = "Login to Facets control plane"; Login.examples = ["<%= config.bin %> <%= command.id %>"]; Login.flags = { username: core_1.Flags.string({ char: "u", description: "username of the user", required: true, }), "access-token": core_1.Flags.string({ char: "t", description: "Personal token generated for the user", required: true, }), "cp-url": core_1.Flags.string({ char: "c", description: "URL of the Control plane", required: true, }), debug: core_1.Flags.string({ description: "See original error message thrown", required: false, default: "false", }), }; Login.args = [];