esa-cli
Version:
A CLI for operating Alibaba Cloud ESA EdgeRoutine (Edge Functions).
155 lines (154 loc) • 6.45 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: (yargs) => {
var _a, _b;
return yargs
.option('access-key-id', {
alias: 'ak',
describe: (_a = t('login_option_access_key_id')) === null || _a === void 0 ? void 0 : _a.d('AccessKey ID'),
type: 'string'
})
.option('access-key-secret', {
alias: 'sk',
describe: (_b = t('login_option_access_key_secret')) === null || _b === void 0 ? void 0 : _b.d('AccessKey Secret'),
type: 'string'
});
},
handler: (argv) => __awaiter(void 0, void 0, void 0, function* () {
handleLogin(argv);
})
};
export default login;
export function handleLogin(argv) {
return __awaiter(this, void 0, void 0, function* () {
generateDefaultConfig();
// Prioritize command line parameters
const accessKeyId = argv === null || argv === void 0 ? void 0 : argv['access-key-id'];
const accessKeySecret = argv === null || argv === void 0 ? void 0 : argv['access-key-secret'];
if (accessKeyId && accessKeySecret) {
yield handleLoginWithAKSK(accessKeyId, accessKeySecret);
return;
}
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();
}
});
}
function handleLoginWithAKSK(accessKeyId, accessKeySecret) {
return __awaiter(this, void 0, void 0, function* () {
let apiConfig = getApiConfig();
apiConfig.auth = {
accessKeyId,
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.'));
}
});
}
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.'));
}
});
}