update add remote config
This commit is contained in:
96
src/command/config-remote.ts
Normal file
96
src/command/config-remote.ts
Normal file
@@ -0,0 +1,96 @@
|
||||
import { program, Command } from '@/program.ts';
|
||||
import { query } from '@/module/query.ts';
|
||||
import { QueryConfig } from '@/query/query-config/query-config.ts';
|
||||
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);
|
||||
@@ -15,6 +15,8 @@ import './command/sync/sync.ts';
|
||||
import './command/app/index.ts';
|
||||
|
||||
import './command/gist/index.ts';
|
||||
import './command/config-remote.ts';
|
||||
|
||||
// program.parse(process.argv);
|
||||
|
||||
export const runParser = async (argv: string[]) => {
|
||||
|
||||
121
src/query/query-config/query-config.ts
Normal file
121
src/query/query-config/query-config.ts
Normal file
@@ -0,0 +1,121 @@
|
||||
/**
|
||||
* 配置查询
|
||||
* @updatedAt 2025-12-03 10:33:00
|
||||
*/
|
||||
import { Query } from '@kevisual/query';
|
||||
import type { Result } from '@kevisual/query/query';
|
||||
type QueryConfigOpts = {
|
||||
query?: Query;
|
||||
};
|
||||
export type Config<T = any> = {
|
||||
id?: string;
|
||||
title?: string;
|
||||
key?: string;
|
||||
description?: string;
|
||||
data?: T;
|
||||
createdAt?: string;
|
||||
updatedAt?: string;
|
||||
};
|
||||
export type UploadConfig = {
|
||||
key?: string;
|
||||
version?: string;
|
||||
};
|
||||
type PostOpts = {
|
||||
token?: string;
|
||||
payload?: Record<string, any>;
|
||||
};
|
||||
export const defaultConfigKeys = ['upload.json', 'workspace.json', 'ai.json', 'user.json', 'life.json'] as const;
|
||||
type DefaultConfigKey = (typeof defaultConfigKeys)[number];
|
||||
|
||||
export class QueryConfig {
|
||||
query: Query;
|
||||
constructor(opts?: QueryConfigOpts) {
|
||||
this.query = opts?.query || new Query();
|
||||
}
|
||||
async post<T = Config>(data: any) {
|
||||
return this.query.post<T>({ path: 'config', ...data });
|
||||
}
|
||||
async getConfig({ id, key }: { id?: string; key?: string }, opts?: PostOpts) {
|
||||
return this.post({
|
||||
key: 'get',
|
||||
data: {
|
||||
id,
|
||||
key,
|
||||
},
|
||||
...opts,
|
||||
});
|
||||
}
|
||||
async updateConfig(data: Config, opts?: PostOpts) {
|
||||
return this.post({
|
||||
key: 'update',
|
||||
data,
|
||||
...opts,
|
||||
});
|
||||
}
|
||||
async deleteConfig(data: { id?: string, key?: string }, opts?: PostOpts) {
|
||||
console.log('Delete Config Params:', data);
|
||||
return this.post({
|
||||
key: 'delete',
|
||||
data,
|
||||
});
|
||||
}
|
||||
async listConfig(opts?: PostOpts) {
|
||||
return this.post<{ list: Config[] }>({
|
||||
key: 'list',
|
||||
...opts,
|
||||
});
|
||||
}
|
||||
/**
|
||||
* 获取上传配置
|
||||
* @returns
|
||||
*/
|
||||
async getUploadConfig(opts?: PostOpts) {
|
||||
return this.post<Result<Config<UploadConfig>>>({
|
||||
key: 'getUploadConfig',
|
||||
...opts,
|
||||
});
|
||||
}
|
||||
/**
|
||||
* 更新上传配置
|
||||
* @param data
|
||||
* @returns
|
||||
*/
|
||||
async updateUploadConfig(data: Config, opts?: PostOpts) {
|
||||
return this.post<Result<Config<UploadConfig>>>({
|
||||
key: 'updateUploadConfig',
|
||||
data,
|
||||
...opts,
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* 检测配置是否存在
|
||||
* @param id
|
||||
* @returns
|
||||
*/
|
||||
async detectConfig(opts?: PostOpts) {
|
||||
return this.post<{ updateList: Config[] }>({
|
||||
key: 'detect',
|
||||
...opts,
|
||||
});
|
||||
}
|
||||
/**
|
||||
* 获取配置, 获取默认的配置项
|
||||
* @param key
|
||||
* @returns
|
||||
*/
|
||||
async getConfigByKey(key: DefaultConfigKey, opts?: PostOpts) {
|
||||
return this.post<Result<Config>>({
|
||||
key: 'defaultConfig',
|
||||
configKey: key,
|
||||
...opts,
|
||||
});
|
||||
}
|
||||
async getByKey<T = any>(key: string, opts?: PostOpts) {
|
||||
return this.post<Result<Config<T>>>({
|
||||
key: 'get',
|
||||
...opts,
|
||||
data: { key },
|
||||
});
|
||||
}
|
||||
}
|
||||
5
src/uitls/show-more.ts
Normal file
5
src/uitls/show-more.ts
Normal file
@@ -0,0 +1,5 @@
|
||||
import util from 'node:util';
|
||||
|
||||
export const showMore = (obj: any, depth = 5) => {
|
||||
return util.inspect(obj, { showHidden: false, depth, colors: true });
|
||||
};
|
||||
Reference in New Issue
Block a user