token cache
This commit is contained in:
@@ -6,11 +6,40 @@ import inquirer from 'inquirer';
|
||||
import { runApp } from '../app-run.ts';
|
||||
import { chalk } from '@/module/chalk.ts';
|
||||
import { loginInCommand } from '@/module/login/login-by-web.ts';
|
||||
export const saveToken = async (token: string) => {
|
||||
const baseURL = getBaseURL();
|
||||
const res = await runApp({ path: 'config', key: 'saveToken', payload: { baseURL, token } });
|
||||
if (res.code !== 200) {
|
||||
console.log('Set token failed', res.message || '');
|
||||
}
|
||||
};
|
||||
export const switchToken = async (baseURL: string) => {
|
||||
const res = await runApp({ path: 'config', key: 'switchToken', payload: { baseURL } });
|
||||
if (res.code !== 200 && res.code !== 404) {
|
||||
console.log('switch token failed', res.message || '');
|
||||
}
|
||||
return res;
|
||||
};
|
||||
export const deleteToken = async (baseURL: string) => {
|
||||
const res = await runApp({ path: 'config', key: 'deleteToken', payload: { baseURL } });
|
||||
if (res.code !== 200) {
|
||||
console.log('delete token failed', res.message || '');
|
||||
}
|
||||
return res;
|
||||
};
|
||||
export const getTokenList = async () => {
|
||||
const res = await runApp({ path: 'config', key: 'getTokenList' });
|
||||
if (res.code !== 200) {
|
||||
console.log('get token list failed', res.message || '');
|
||||
}
|
||||
return res;
|
||||
};
|
||||
// 定义login命令,支持 `-u` 和 `-p` 参数来输入用户名和密码
|
||||
const loginCommand = new Command('login')
|
||||
.description('Login to the application')
|
||||
.option('-u, --username <username>', 'Specify username')
|
||||
.option('-p, --password <password>', 'Specify password')
|
||||
.option('-f, --force', 'Force login')
|
||||
.option('-w, --web', 'Login on the web')
|
||||
.action(async (options) => {
|
||||
const config = getConfig();
|
||||
@@ -54,6 +83,7 @@ const loginCommand = new Command('login')
|
||||
if (res.code === 200) {
|
||||
const { token } = res.data;
|
||||
writeConfig({ ...config, token });
|
||||
saveToken(token);
|
||||
console.log('welcome', username);
|
||||
} else {
|
||||
console.log('登录失败', res.message || '');
|
||||
@@ -100,6 +130,7 @@ const switchOrgCommand = new Command('switch').argument('<username>', 'Switch to
|
||||
const token = res.data.token;
|
||||
writeConfig({ ...config, token });
|
||||
console.log(`Switch ${username} Success`);
|
||||
saveToken(token);
|
||||
await showMe();
|
||||
} else {
|
||||
console.log(`Switch ${username} Failed`, res.message || '');
|
||||
|
||||
@@ -1,12 +1,44 @@
|
||||
import { program as app, Command } from '@/program.ts';
|
||||
import { getConfig, query, writeConfig } from '@/module/index.ts';
|
||||
import inquirer from 'inquirer';
|
||||
import util from 'util';
|
||||
|
||||
import { saveToken, switchToken, deleteToken, getTokenList } from './login.ts';
|
||||
const token = new Command('token').description('show token').action(async () => {
|
||||
const config = getConfig();
|
||||
console.log('token', config.token);
|
||||
});
|
||||
const tokenList = new Command('list')
|
||||
.description('show token list')
|
||||
.option('-r --remove <number>', 'remove token by number')
|
||||
.action(async (opts) => {
|
||||
const res = await getTokenList();
|
||||
if (res.code !== 200) {
|
||||
console.error('get token list failed', res.message || '');
|
||||
return;
|
||||
}
|
||||
console.log(util.inspect(res.data.value, { colors: true, depth: 4 }));
|
||||
|
||||
const list = res.data.value || [];
|
||||
if (opts.remove) {
|
||||
const index = Number(opts.remove) - 1;
|
||||
if (index < 0 || index >= list.length) {
|
||||
console.log('index out of range');
|
||||
return;
|
||||
}
|
||||
const removeBase = list.splice(index, 1);
|
||||
const baseURL = removeBase[0];
|
||||
if (baseURL.baseURL) {
|
||||
const res = await deleteToken(baseURL?.baseURL);
|
||||
if (res.code !== 200) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
console.log('delete token success', 'delete', baseURL);
|
||||
return;
|
||||
}
|
||||
});
|
||||
token.addCommand(tokenList);
|
||||
app.addCommand(token);
|
||||
|
||||
const baseURL = new Command('baseURL')
|
||||
@@ -20,7 +52,7 @@ const baseURL = new Command('baseURL')
|
||||
.option('-c, --clear', 'clear baseURL')
|
||||
.action(async (opts) => {
|
||||
const config = getConfig();
|
||||
let list = config.baseURLList || [];
|
||||
let list = (config.baseURLList as Array<string>) || [];
|
||||
const quineList = (list: string[]) => {
|
||||
const newList = new Set(list);
|
||||
return Array.from(newList);
|
||||
@@ -49,20 +81,23 @@ const baseURL = new Command('baseURL')
|
||||
console.log('index out of range');
|
||||
return;
|
||||
}
|
||||
list.splice(index, 1);
|
||||
const removeBase = list.splice(index, 1);
|
||||
list = quineList(list);
|
||||
showList(list);
|
||||
writeConfig({ ...config, baseURLList: list });
|
||||
removeBase[0] && deleteToken(removeBase[0]);
|
||||
return;
|
||||
}
|
||||
if (opts.set) {
|
||||
const isNumber = !isNaN(Number(opts.set));
|
||||
let baseURL = '';
|
||||
if (isNumber) {
|
||||
const index = Number(opts.set) - 1;
|
||||
if (index < 0 || index >= list.length) {
|
||||
console.log('index out of range');
|
||||
return;
|
||||
}
|
||||
baseURL = list[index];
|
||||
writeConfig({ ...config, baseURL: list[index] });
|
||||
console.log('set baseURL success:', list[index]);
|
||||
} else {
|
||||
@@ -72,9 +107,11 @@ const baseURL = new Command('baseURL')
|
||||
console.log('invalid baseURL:', opts.set);
|
||||
return;
|
||||
}
|
||||
baseURL = opts.set;
|
||||
writeConfig({ ...config, baseURL: opts.set });
|
||||
console.log('set baseURL success:', opts.set);
|
||||
}
|
||||
baseURL && switchToken(baseURL);
|
||||
return;
|
||||
}
|
||||
if (opts.list) {
|
||||
@@ -100,6 +137,7 @@ const setBaseURL = new Command('set').description('set baseURL').action(async ()
|
||||
]);
|
||||
const baseURL = answers.baseURL;
|
||||
writeConfig({ ...config, baseURL });
|
||||
baseURL && switchToken(baseURL);
|
||||
});
|
||||
|
||||
baseURL.addCommand(setBaseURL);
|
||||
|
||||
@@ -435,7 +435,7 @@ const servicesCommand = new Command('services')
|
||||
);
|
||||
});
|
||||
} else {
|
||||
console.log(chalk.red(res.message || '获取列表失败'));
|
||||
console.log('error', chalk.red(res.message || '获取列表失败'));
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -1,49 +1,102 @@
|
||||
import { program as app, Command } from '@/program.ts';
|
||||
import { program, Command } from '@/program.ts';
|
||||
import inquirer from 'inquirer';
|
||||
import { query } from '../module/index.ts';
|
||||
import chalk from 'chalk';
|
||||
import util from 'util';
|
||||
import { runApp } from '@/app-run.ts';
|
||||
|
||||
const router = new Command('router').description('router get');
|
||||
program.addCommand(router);
|
||||
// web 开发模块
|
||||
const command = new Command('router')
|
||||
.description('router get')
|
||||
.option('-p, --path <path>', '退出进程')
|
||||
.option('-k, --key <key>', '启动进程')
|
||||
const command = new Command('service')
|
||||
.description('router services get')
|
||||
.option('-p, --path <path>', '第一路径 path')
|
||||
.option('-k, --key <key>', '第二路径 key')
|
||||
.action(async (options) => {
|
||||
let { path, key } = options;
|
||||
// 如果没有传递参数,则通过交互式输入
|
||||
if (!path || !key) {
|
||||
if (!path) {
|
||||
const answers = await inquirer.prompt([
|
||||
{
|
||||
type: 'input',
|
||||
name: 'path',
|
||||
required: true,
|
||||
message: 'Enter your path:',
|
||||
when: () => !path, // 当 username 为空时,提示用户输入
|
||||
},
|
||||
{
|
||||
type: 'input',
|
||||
name: 'key',
|
||||
message: 'Enter your key:',
|
||||
when: () => !key, // 当 password 为空时,提示用户输入
|
||||
},
|
||||
]);
|
||||
path = answers.path || path;
|
||||
}
|
||||
if (!key) {
|
||||
const answers = await inquirer.prompt([
|
||||
{
|
||||
type: 'input',
|
||||
required: false,
|
||||
name: 'key',
|
||||
message: 'Enter your key:',
|
||||
},
|
||||
]);
|
||||
key = answers.key || key;
|
||||
}
|
||||
|
||||
const res = await query.post({ path, key });
|
||||
if (res?.code === 200) {
|
||||
const data = res.data.map((item: any) => {
|
||||
// return `id: ${item.id}, title: ${item.title}`;
|
||||
|
||||
return {
|
||||
id: item.id,
|
||||
title: item.title,
|
||||
};
|
||||
});
|
||||
console.log(chalk.green(util.inspect(data, { colors: true, depth: 4 })));
|
||||
console.log('query success');
|
||||
const _list = res.data?.list || res.data;
|
||||
if (Array.isArray(_list)) {
|
||||
const data = _list.map((item: any) => {
|
||||
// return `id: ${item.id}, title: ${item.title}`;
|
||||
return {
|
||||
id: item.id,
|
||||
title: item.title,
|
||||
};
|
||||
});
|
||||
console.log(chalk.green(util.inspect(data, { colors: true, depth: 4 })));
|
||||
}
|
||||
} else {
|
||||
console.log('error', res.message || '');
|
||||
}
|
||||
});
|
||||
app.addCommand(command);
|
||||
|
||||
router.addCommand(command);
|
||||
|
||||
const localRouter = new Command('local')
|
||||
.description('router local get')
|
||||
.option('-p, --path <path>', '第一路径 path')
|
||||
.option('-k, --key <key>', '第二路径 key')
|
||||
.action(async (options) => {
|
||||
let { path, key } = options;
|
||||
// 如果没有传递参数,则通过交互式输入
|
||||
if (!path) {
|
||||
const answers = await inquirer.prompt([
|
||||
{
|
||||
type: 'input',
|
||||
name: 'path',
|
||||
required: true,
|
||||
message: 'Enter your path:',
|
||||
when: () => !path, // 当 username 为空时,提示用户输入
|
||||
},
|
||||
]);
|
||||
path = answers.path || path;
|
||||
}
|
||||
if (!key) {
|
||||
const answers = await inquirer.prompt([
|
||||
{
|
||||
type: 'input',
|
||||
required: false,
|
||||
name: 'key',
|
||||
message: 'Enter your key:',
|
||||
},
|
||||
]);
|
||||
key = answers.key || key;
|
||||
}
|
||||
const res = await runApp({ path, key });
|
||||
if (res?.code === 200) {
|
||||
console.log('query success');
|
||||
|
||||
console.log(chalk.green(util.inspect(res, { colors: true, depth: 4 })));
|
||||
} else {
|
||||
console.log('error query', res.code, res.message || '');
|
||||
}
|
||||
});
|
||||
|
||||
router.addCommand(localRouter);
|
||||
|
||||
Reference in New Issue
Block a user