@devmn/cloud-cli
Version:
CLI tool for Intelligo Cloud.
207 lines • 9.28 kB
JavaScript
;
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
const StorageHelper_1 = require("./StorageHelper");
const StdOutUtil_1 = require("./StdOutUtil");
const Constants_1 = require("./Constants");
const ValidationsHandler_1 = require("./ValidationsHandler");
const CliApiManager_1 = require("../api/CliApiManager");
class CliHelper {
static get() {
if (!CliHelper.instance) {
CliHelper.instance = new CliHelper();
}
return CliHelper.instance;
}
getAppsAsOptions(apps) {
return [
{
name: Constants_1.default.CANCEL_STRING,
value: '',
short: ''
},
...apps.map(app => ({
name: `${app.appName}`,
value: `${app.appName}`,
short: `${app.appName}`
}))
];
}
getMachinesAsOptions() {
return [
{
name: Constants_1.default.CANCEL_STRING,
value: '',
short: ''
},
...StorageHelper_1.default.get()
.getMachines()
.map(machine => ({
name: `${StdOutUtil_1.default.getColoredMachine(machine)}`,
value: `${machine.name}`,
short: `${machine.name}`
}))
];
}
getApiMethodsAsOptions() {
return [
{
name: Constants_1.default.CANCEL_STRING,
value: '',
short: ''
},
...Constants_1.default.API_METHODS.map(method => ({
name: `${method}`,
value: `${method}`,
short: `${method}`
}))
];
}
getApiMethodsDescription() {
return Constants_1.default.API_METHODS.reduce((acc, method) => (acc ? `${acc}, ` : '') + `"${method}"`, '');
}
loginMachine(machine, password) {
return __awaiter(this, void 0, void 0, function* () {
try {
const tokenToIgnore = yield CliApiManager_1.default.get(machine).getAuthToken(password);
StdOutUtil_1.default.printGreenMessage(`Logged in successfully.`);
StdOutUtil_1.default.printMessage(`Authorization token is now saved as ${StdOutUtil_1.default.getColoredMachine(machine)}.\n`);
}
catch (error) {
const errorMessage = error.message ? error.message : error;
StdOutUtil_1.default.printError(`Something bad happened: cannot save ${StdOutUtil_1.default.getColoredMachine(machine)}.\n${errorMessage}\n`);
}
});
}
logoutMachine(machineName) {
const removedMachine = StorageHelper_1.default.get().removeMachine(machineName);
StdOutUtil_1.default.printMessage(`You are now logged out from ${StdOutUtil_1.default.getColoredMachine(removedMachine)}.\n`);
}
findDefaultCaptainName() {
let currentSuffix = 1;
const machines = StorageHelper_1.default.get()
.getMachines()
.map(machine => machine.name);
while (machines.includes(this.getCaptainFullName(currentSuffix))) {
currentSuffix++;
}
return this.getCaptainFullName(currentSuffix);
}
getCaptainFullName(suffix) {
return `captain-${suffix < 10 ? '0' : ''}${suffix}`;
}
ensureAuthentication(url, password, machineName) {
return __awaiter(this, void 0, void 0, function* () {
if (url) {
// Auth to url
const machine = { baseUrl: url, name: '', authToken: '' };
if (machineName) {
// With machine name: also store credentials
let err = ValidationsHandler_1.getErrorForDomain(url);
if (err !== true) {
// Error for domain: can't store credentials
StdOutUtil_1.default.printWarning(`\nCan't store store login credentials: ${err ||
'error!'}\n`);
}
else {
err = ValidationsHandler_1.getErrorForMachineName(machineName);
if (err !== true) {
// Error for machine name: can't store credentials
StdOutUtil_1.default.printWarning(`\nCan't store store login credentials: ${err ||
'error!'}\n`);
}
else {
machine.name = machineName;
}
}
}
if (password) {
// If password provided
yield CliApiManager_1.default.get(machine).getAuthToken(password); // Do auth
}
return machine;
}
else if (machineName) {
// Auth to stored machine name
const machine = StorageHelper_1.default.get().findMachine(machineName); // Get stored machine
if (!machine) {
throw new Error(`Can't find stored machine "${machineName}"`);
} // No stored machine: throw
try {
yield CliApiManager_1.default.get(machine).getAllApps(); // Get data with stored token
}
catch (e) {
// Error getting data: token expired
StdOutUtil_1.default.printWarning(`Your auth token for ${StdOutUtil_1.default.getColoredMachine(machine)} is not valid anymore, try to login again...`);
machine.authToken = ''; // Remove expired token
if (password) {
// If password provided
yield CliApiManager_1.default.get(machine).getAuthToken(password); // Do auth
}
}
return machine;
}
throw new Error('Too few arguments, no url or machine name');
});
}
getEnsureAuthenticationOption(appToken, url, password, name, done) {
let machine;
return {
name: 'ensureAuthenticationPlaceholder',
message: 'CapRover machine password',
type: 'password',
hide: true,
when: () => __awaiter(this, void 0, void 0, function* () {
StdOutUtil_1.default.printMessage('Ensuring authentication...');
const getVal = (value) => {
return value && value instanceof Function ? value() : value;
};
const urlExtracted = getVal(url);
const passwordExtracted = getVal(password);
const nameExtracted = getVal(name);
if (!!appToken) {
machine = {
baseUrl: urlExtracted || '',
authToken: '',
appToken,
name: nameExtracted || ''
};
return false;
}
try {
machine = yield CliHelper.get().ensureAuthentication(urlExtracted, passwordExtracted, nameExtracted);
return !machine.authToken;
}
catch (e) {
StdOutUtil_1.default.printError(`\nSomething bad happened during authentication to ${urlExtracted
? StdOutUtil_1.default.getColoredMachineUrl(urlExtracted)
: StdOutUtil_1.default.getColoredMachineName(nameExtracted || '')}.\n${e.message || e}`, true);
}
return true;
}),
validate: (passwordToValidate) => __awaiter(this, void 0, void 0, function* () {
const err = ValidationsHandler_1.getErrorForPassword(passwordToValidate);
if (err !== true) {
return err;
}
try {
yield CliApiManager_1.default.get(machine).getAuthToken(passwordToValidate); // Do auth
}
catch (e) {
StdOutUtil_1.default.printError(`\nSomething bad happened during authentication to ${StdOutUtil_1.default.getColoredMachineUrl(machine.baseUrl)}.\n${e.message || e}`, true);
}
return true;
}),
preProcessParam: () => __awaiter(this, void 0, void 0, function* () { return done && (yield done(machine)); })
};
}
}
exports.default = CliHelper;
//# sourceMappingURL=CliHelper.js.map