@facets-cloud/facetsctlv3
Version:
129 lines (128 loc) • 4.93 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
const core_1 = require("@oclif/core");
const FacetsAPI = __importStar(require("../services/facets-api"));
const config_service_1 = require("../services/config-service");
class Login extends core_1.Command {
static description = 'Log in to the Facets control plane';
static examples = [
`$ facetsctl login --username my-user --token my-token --facets-url https://facets-control-plane.example.com
Logged in successfully
`
];
static flags = {
username: core_1.Flags.string({
char: 'u',
description: 'Username',
required: true,
}),
token: core_1.Flags.string({
char: 't',
description: 'Personal token',
required: true,
}),
'facets-url': core_1.Flags.string({
char: 'f',
description: 'URL of the Control plane',
required: true,
}),
};
async run() {
const { flags } = await this.parse(Login);
const username = flags.username;
const token = flags.token;
let cpUrl = flags['facets-url'];
if (!cpUrl.startsWith('http://') && !cpUrl.startsWith('https://')) {
cpUrl = `https://${cpUrl}`;
}
// Remove trailing slash if present
if (cpUrl.endsWith('/')) {
cpUrl = cpUrl.slice(0, -1);
}
if (!username || !token || !cpUrl) {
this.error('Username, token, and control plane URL are required.');
}
// const configFilePath = ConfigService.findConfigFile();
//
// if (configFilePath) {
// this.log('Configuration file already exists. No need to log in again.');
// const config = ConfigService.readConfig(configFilePath);
// this.log('Control Plane URL:', config.ControlPlaneURL);
// return;
// }
try {
const urlObj = new URL(cpUrl);
cpUrl = `https://${urlObj.hostname}`;
}
catch (error) {
this.error('Invalid URL format. Please provide a valid URL in the format: "https://<cp-host-name>".');
}
core_1.ux.action.start('Initiating login process');
try {
// Step 1: Verify user credentials
core_1.ux.action.status = 'Verifying user credentials...';
const result = await FacetsAPI.fetchUserDetails(cpUrl, username, token);
if (result.status === 200) {
// Step 2: Save config file and exit
core_1.ux.action.status = 'Saving configuration...';
const config = {
ControlPlaneURL: cpUrl,
Username: username,
AccessToken: token,
};
config_service_1.ConfigService.saveConfig(config);
core_1.ux.action.stop('Login successful!');
}
else {
this.handleError(result);
}
}
catch (error) {
core_1.ux.action.stop('Login failed');
this.handleError(error);
}
}
handleError(error) {
this.logErrorDetails(error);
if (error?.code === 'ENOTFOUND') {
this.error('The URL of the Control Plane is incorrect. Please check and try again.');
}
else if (error?.code === 'ERR_BAD_REQUEST') {
this.error('The username or token provided is incorrect. Please verify your credentials.');
}
else {
this.error('An unexpected error occurred. Please try again.');
}
}
logErrorDetails(error) {
this.log(`Error details: ${error.message}`);
// Log actual error details for debugging purposes
if (error.stack) {
this.log(error.stack);
}
}
}
exports.default = Login;