96 lines
3.0 KiB
TypeScript
96 lines
3.0 KiB
TypeScript
import { program, Command } from '@/program.ts';
|
||
import { query } from '@/module/query.ts';
|
||
import { QueryConfig } from '@kevisual/api/query-config';
|
||
import { showMore } from '@/uitls/show-more.ts';
|
||
import fs from 'node:fs';
|
||
import path from 'node:path';
|
||
|
||
const queryConfig = new QueryConfig({ query: query as any });
|
||
const command = new Command('remote-config')
|
||
.alias('rc').description('获取或设置远程配置');
|
||
|
||
|
||
const getCommand = new Command('get')
|
||
.option('-k, --key <key>', '配置键名')
|
||
.action(async (options) => {
|
||
const { key } = options || {};
|
||
if (!key) {
|
||
console.log('Please provide a key using -k or --key option.');
|
||
return;
|
||
}
|
||
const res = await queryConfig.getConfigByKey(key);
|
||
console.log('res Config Result:', showMore(res.data));
|
||
})
|
||
|
||
const listCommand = new Command('list')
|
||
.description('列出所有配置')
|
||
.action(async () => {
|
||
const res = await queryConfig.listConfig();
|
||
console.log('Remote Configs:', res);
|
||
if (res.code === 200) {
|
||
const list = res.data?.list || [];
|
||
list.forEach(item => {
|
||
console.log(item.id, item.key, item.data);
|
||
});
|
||
}
|
||
|
||
});
|
||
|
||
const updateCommand = new Command('update')
|
||
.description('更新远程配置')
|
||
.option('-k, --key <key>', '配置键名')
|
||
.option('-v, --value <value>', '配置值')
|
||
.option('-f, --file <file>', '从文件读取配置值')
|
||
.action(async (options) => {
|
||
const { key, value, file } = options || {};
|
||
if (!key) {
|
||
console.log('请提供配置键名,使用 -k 或 --key 选项。', options);
|
||
return;
|
||
}
|
||
try {
|
||
let data: any = {}
|
||
const filePath = path.resolve(process.cwd(), file);
|
||
const hasFile = fs.existsSync(filePath);
|
||
if (value) {
|
||
data = JSON.parse(value);
|
||
} else if (file || hasFile) {
|
||
// 从文件读取配置值
|
||
if (!hasFile) {
|
||
console.log('指定的文件不存在:', filePath);
|
||
return;
|
||
}
|
||
data = JSON.parse(fs.readFileSync(filePath, 'utf-8'));
|
||
} else {
|
||
console.log('请提供配置值,使用 -v 或 --value 选项,或使用 -f 或 --file 从文件读取。');
|
||
return;
|
||
}
|
||
const res = await queryConfig.updateConfig({
|
||
key,
|
||
data,
|
||
});
|
||
console.log('Update Config Result:', showMore(res.data));
|
||
} catch (error) {
|
||
console.log('Error parsing JSON:');
|
||
}
|
||
});
|
||
|
||
const deleteCommand = new Command('delete')
|
||
.description('删除远程配置')
|
||
.option('-i, --id <id>', '配置ID')
|
||
.option('-k, --key <key>', '配置键名')
|
||
.action(async (options) => {
|
||
const { key, id } = options || {};
|
||
if (!key && !id) {
|
||
console.log('请提供配置键名或配置ID,使用 -k 或 --key 选项,或 -i 或 --id 选项。');
|
||
return;
|
||
}
|
||
const res = await queryConfig.deleteConfig({ key, id });
|
||
console.log('Delete Config Result:', showMore(res));
|
||
});
|
||
|
||
command.addCommand(listCommand);
|
||
command.addCommand(getCommand);
|
||
command.addCommand(updateCommand);
|
||
command.addCommand(deleteCommand);
|
||
|
||
program.addCommand(command); |