esa-cli
Version:
A CLI for operating Alibaba Cloud ESA EdgeRoutine (Edge Functions).
109 lines (108 loc) • 4.64 kB
JavaScript
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
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) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
import inquirer from 'inquirer';
import { getApiConfig, getCliConfig, updateCliConfigFile, generateDefaultConfig } from '../../utils/fileUtils/index.js';
import chalk from 'chalk';
import { ApiService } from '../../libs/apiService.js';
import t from '../../i18n/index.js';
import logger from '../../libs/logger.js';
const login = {
command: 'login',
describe: `🔑 ${t('login_describe').d('Login to the server')}`,
builder: {},
handler: (argv) => __awaiter(void 0, void 0, void 0, function* () {
handleLogin();
})
};
export default login;
export function handleLogin() {
return __awaiter(this, void 0, void 0, function* () {
generateDefaultConfig();
const cliConfig = getCliConfig();
if (!cliConfig)
return;
if (cliConfig &&
cliConfig.auth &&
cliConfig.auth.accessKeyId &&
cliConfig.auth.accessKeySecret) {
const service = yield ApiService.getInstance();
const loginStatus = yield service.checkLogin(false);
if (loginStatus.success) {
logger.warn(t('login_already').d('You are already logged in.'));
const action = yield inquirer.prompt([
{
type: 'list',
name: 'action',
message: t('login_existing_credentials_message').d('Existing credentials found. What do you want to do?'),
choices: [
t('login_existing_credentials_action_overwrite').d('Overwrite existing credentials'),
t('common_exit').d('Exit')
]
}
]);
if (action.action === t('common_exit').d('Exit')) {
return;
}
yield getUserInputAuthInfo();
}
else {
logger.error(t('pre_login_failed').d('The previously entered Access Key ID (AK) and Secret Access Key (SK) are incorrect. Please enter them again.'));
logger.log(`${t('login_logging').d('Logging in')}...`);
yield getUserInputAuthInfo();
}
}
else {
logger.log(`${t('login_logging').d('Logging in')}...`);
yield getUserInputAuthInfo();
}
});
}
export function getUserInputAuthInfo() {
return __awaiter(this, void 0, void 0, function* () {
const styledUrl = chalk.underline.blue('https://ram.console.aliyun.com/manage/ak');
logger.log(`🔑 ${chalk.underline(t('login_get_ak_sk').d(`Please go to the following link to get your account's AccessKey ID and AccessKey Secret`))}`);
logger.log(`👉 ${styledUrl}`);
const answers = yield inquirer.prompt([
{
type: 'input',
name: 'accessKeyId',
message: 'AccessKey ID:'
},
{
type: 'password',
name: 'accessKeySecret',
message: 'AccessKey Secret:',
mask: '*'
}
]);
let apiConfig = getApiConfig();
apiConfig.auth = {
accessKeyId: answers.accessKeyId,
accessKeySecret: answers.accessKeySecret
};
try {
yield updateCliConfigFile({
auth: apiConfig.auth
});
const service = yield ApiService.getInstance();
service.updateConfig(apiConfig);
const loginStatus = yield service.checkLogin();
if (loginStatus.success) {
logger.success(t('login_success').d('Login success!'));
}
else {
logger.error(loginStatus.message || 'Login failed');
}
}
catch (error) {
logger.error(t('login_failed').d('An error occurred while trying to log in.'));
}
});
}