@cto.ai/ops-rc
Version:
š» CTO.ai Ops - The CLI built for Teams š
135 lines (134 loc) ⢠5.57 kB
JavaScript
"use strict";
/**
* @author: JP Lew (jp@cto.ai)
* @date: Tuesday, 30th April 2019 12:07:49 pm
* @lastModifiedBy: JP Lew (jp@cto.ai)
* @lastModifiedTime: Friday, 20th September 2019 1:48:16 pm
* @copyright (c) 2019 CTO.ai
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.signinPrompts = void 0;
const tslib_1 = require("tslib");
const cloneDeep_1 = tslib_1.__importDefault(require("lodash/cloneDeep"));
const base_1 = tslib_1.__importStar(require("../../base"));
const asyncPipe_1 = require("../../utils/asyncPipe");
const CustomErrors_1 = require("../../errors/CustomErrors");
exports.signinPrompts = {
user: {
type: 'input',
name: 'user',
message: 'Username or email: ',
},
password: {
type: 'password',
name: 'password',
message: 'Password: ',
mask: '*',
},
};
class AccountSignin extends base_1.default {
constructor() {
super(...arguments);
this.logMessages = () => {
this.log('');
this.log(`š» ${this.ux.colors.multiBlue('CTO.ai Ops')} - ${this.ux.colors.actionBlue('The CLI built for Teams')} š`);
this.log('');
this.log(`š ${this.ux.colors.white('Welcome to the')} ${this.ux.colors.callOutCyan('Ops CLI beta')}! \n`);
};
this.keycloakSignInFlow = async () => {
this.ux.spinner.start('Authenticating using Single Sign On');
const tokens = await this.services.keycloakService
.keycloakSignInFlow()
.catch(() => {
throw new CustomErrors_1.SSOError();
});
this.ux.spinner.stop('Finished');
return tokens;
};
this.createConfigFile = async (tokens) => {
this.log('');
this.ux.spinner.start(`${this.ux.colors.white('Authenticating')}`);
return this.initConfig(tokens);
};
this.showWelcomeMessage = (config) => {
this.ux.spinner.stop(`${this.ux.colors.green('Done!')}`);
this.log(`\nš ${this.ux.colors.white('Welcome back')} ${this.ux.colors.italic.dim(config.user.username)}!`);
this.log(`\nš Type ${this.ux.colors.italic.dim('ops search')} to find ops or ${this.ux.colors.italic.dim('ops init')} to create your own! \n`);
return cloneDeep_1.default(config);
};
this.sendAnalytics = (config) => {
try {
this.services.analytics.track('Ops CLI Signin', {
username: config.user.username,
}, config);
}
catch (err) {
this.debug('%O', err);
throw new CustomErrors_1.AnalyticsError(err);
}
return cloneDeep_1.default(config);
};
this.determineQuestions = (prompts, flags) => () => {
const removeIfPassedToFlags = ([key, _question]) => !Object.entries(flags)
.map(([flagKey]) => flagKey)
.includes(key);
const questions = Object.entries(prompts)
.filter(removeIfPassedToFlags)
.map(([_key, question]) => question);
return questions;
};
this.getRefreshToken = async (credentials) => {
const tokens = await this.services.keycloakService.getTokenFromPasswordGrant(credentials);
return tokens;
};
this.askQuestions = async (questions) => {
if (!questions.length) {
return {};
}
this.log(`${this.ux.colors.white('Please login to get started.')}\n`);
return this.ux.prompt(questions);
};
this.determineUserCredentials = (flags) => (answers) => (Object.assign(Object.assign({}, flags), answers));
this.browserSigninPipeline = asyncPipe_1.asyncPipe(this.logMessages, this.keycloakSignInFlow, this.createConfigFile, this.showWelcomeMessage, this.sendAnalytics);
this.cliSigninPipeline = (flags) => asyncPipe_1.asyncPipe(this.logMessages, this.determineQuestions(exports.signinPrompts, flags), this.askQuestions, this.determineUserCredentials(flags), this.getRefreshToken, this.createConfigFile, this.showWelcomeMessage, this.sendAnalytics);
}
async run() {
try {
const { flags } = this.parse(AccountSignin);
await this.services.keycloakService.init();
/*
* If -u, -p, or -i flags are passed, sign-in via the CLI as opposed to
* the browser.
*
* If -i AND either -u or -p are passed, it doesn't matter because the CLI
* will prompt for all missing flags.
*/
if (flags.interactive || flags.user || flags.password) {
return await this.cliSigninPipeline(flags)();
}
return await this.browserSigninPipeline();
}
catch (err) {
this.ux.spinner.stop('Failed');
this.debug('%O', err);
this.config.runHook('error', { err });
}
}
}
exports.default = AccountSignin;
AccountSignin.description = 'Log in to your account.';
AccountSignin.flags = {
help: base_1.flags.help({ char: 'h' }),
interactive: base_1.flags.boolean({
char: 'i',
description: 'Interactive Mode',
}),
user: base_1.flags.string({
char: 'u',
description: 'Username or email',
}),
password: base_1.flags.string({
char: 'p',
description: 'Password',
}),
};