UNPKG

claude-key-manager

Version:

🚀 Claude Key Manager 4.1.3 - 修复密钥验证问题,支持更多密钥格式

265 lines (241 loc) 8.54 kB
#!/usr/bin/env node const { Command } = require('commander'); const chalk = require('chalk'); const boxen = require('boxen'); const KeyManager = require('../src/key-manager'); const InteractiveMenu = require('../src/interactive-menu'); const VersionManager = require('../src/version-manager'); const InlineMenu = require('../src/inline-menu'); const program = new Command(); const keyManager = new KeyManager(); const versionManager = new VersionManager(); const inlineMenu = new InlineMenu(keyManager); // 版本和基本信息 program .name('claude-key') .description('🚀 Claude Key Manager 4.0 - 终极API密钥管理工具') .version('4.1.3'); // 添加密钥 (支持API URL) program .command('add') .description('📝 添加新的Claude API密钥') .argument('<key>', 'Claude API密钥 (sk-ant-...)') .option('-n, --name <name>', '密钥名称', 'default') .option('-d, --description <desc>', '密钥描述') .option('-u, --url <url>', 'API Base URL', 'https://api.anthropic.com') .action(async (key, options) => { try { await keyManager.addKey(key, options.name, options.description, options.url); console.log(chalk.green('✅ 密钥添加成功!')); } catch (error) { console.log(chalk.red('❌ 错误:'), error.message); process.exit(1); } }); // 列出所有密钥 program .command('list') .alias('ls') .description('📋 列出所有已保存的密钥') .option('-v, --verbose', '显示详细信息') .action(async (options) => { try { const keys = await keyManager.listKeys(options.verbose); if (keys.length === 0) { console.log(chalk.yellow('⚠️ 没有找到任何密钥')); return; } console.log(boxen( `📋 已保存的密钥 (${keys.length}个)\n\n` + keys.map(k => `${k.active ? '👉' : ' '} ${chalk.bold(k.name)}${k.description ? ` - ${k.description}` : ''}` ).join('\n'), { padding: 1, borderColor: 'blue' } )); } catch (error) { console.log(chalk.red('❌ 错误:'), error.message); } }); // 切换密钥 program .command('switch') .alias('use') .description('🔄 切换当前使用的密钥') .argument('<name>', '要切换到的密钥名称') .action(async (name) => { try { await keyManager.switchKey(name); console.log(chalk.green(`✅ 已切换到密钥: ${chalk.bold(name)}`)); } catch (error) { console.log(chalk.red('❌ 错误:'), error.message); } }); // 删除密钥 (你要的功能) program .command('delete') .alias('remove') .alias('rm') .description('🗑️ 删除指定密钥') .argument('<name>', '要删除的密钥名称') .option('-f, --force', '强制删除,跳过确认') .action(async (name, options) => { try { const success = await keyManager.deleteKey(name, options.force); if (success) { console.log(chalk.green(`✅ 密钥 '${name}' 删除成功`)); } } catch (error) { console.log(chalk.red('❌ 错误:'), error.message); } }); // 显示当前密钥 program .command('current') .description('👁️ 显示当前活跃的密钥') .action(async () => { try { const current = await keyManager.getCurrentKey(); if (current) { console.log(chalk.green(`🔑 当前密钥: ${chalk.bold(current.name)}`)); if (current.description) { console.log(chalk.gray(` 描述: ${current.description}`)); } } else { console.log(chalk.yellow('⚠️ 没有设置当前密钥')); } } catch (error) { console.log(chalk.red('❌ 错误:'), error.message); } }); // 交互式菜单 (你要的功能) program .command('interactive') .alias('menu') .description('🎯 进入交互式菜单模式') .action(async () => { const menu = new InteractiveMenu(keyManager); await menu.start(); }); // 版本信息 (你要的新功能!) program .command('version') .alias('v') .description('📋 显示版本信息并检查更新') .action(async () => { try { await versionManager.showVersionInfo(); } catch (error) { console.log(chalk.red('❌ 错误:'), error.message); } }); // 更新助手 (你要的新功能!) program .command('update') .alias('upgrade') .description('🔄 检查并更新到最新版本') .action(async () => { try { await versionManager.updateHelper(); } catch (error) { console.log(chalk.red('❌ 错误:'), error.message); } }); // 启动时检查更新 (更主动的更新策略) async function checkForUpdatesOnStartup() { try { const notification = await versionManager.getUpdateNotification(); if (notification.show) { console.log(boxen( `${notification.message}\n\n` + `${chalk.green('3秒后自动更新...')} (按 Ctrl+C 取消)\n` + `或立即运行: ${chalk.blue(notification.command)}`, { padding: { top: 0, bottom: 0, left: 1, right: 1 }, borderColor: 'yellow', borderStyle: 'round' } )); console.log(''); // 空行分隔 // 3秒倒计时,用户可以按Ctrl+C取消 return new Promise((resolve) => { let countdown = 3; const timer = setInterval(() => { process.stdout.write(`\r${chalk.blue('自动更新倒计时:')} ${chalk.yellow(countdown)} 秒...`); countdown--; if (countdown < 0) { clearInterval(timer); process.stdout.write('\r' + ' '.repeat(30) + '\r'); // 清除倒计时 console.log(chalk.green('🚀 开始自动更新...')); // 执行自动更新 versionManager.updateHelper().then(() => { resolve(true); }).catch(() => { console.log(chalk.yellow('\n⚠️ 自动更新失败,继续使用当前版本')); resolve(false); }); } }, 1000); // 监听Ctrl+C取消更新 process.on('SIGINT', () => { clearInterval(timer); process.stdout.write('\r' + ' '.repeat(30) + '\r'); // 清除倒计时 console.log(chalk.gray('\n更新已取消,继续使用当前版本')); resolve(false); }); }); } } catch (error) { // 静默失败,不影响主功能 } return false; } // 没有参数时显示帮助或进入交互模式 if (process.argv.length === 2) { // 先显示更新提醒,如果需要更新则自动更新 checkForUpdatesOnStartup().then((updated) => { // 如果已经更新并重启,这里的代码不会执行 if (!updated) { // 没有更新或更新被取消,继续正常流程 // 检测是否输入了 "/" process.stdin.setRawMode(true); process.stdin.resume(); process.stdin.setEncoding('utf8'); console.log(chalk.blue('💡 输入 "/" 显示快速命令,或使用 --help 查看所有命令')); process.stdin.on('data', async (key) => { if (key === '/') { // 不跳转,直接在当前界面显示选项 (你要的功能!) const selectedAction = await inlineMenu.showInlineOptions(); if (selectedAction) { await inlineMenu.executeAction(selectedAction); } // 执行完操作后重新显示提示 console.log(chalk.blue('\n💡 输入 "/" 显示快速命令,或使用 --help 查看所有命令')); } else if (key === '\u0003') { // Ctrl+C process.exit(0); } }); } }); } else { // 对于有参数的命令,静默检查更新但不自动更新(避免干扰命令执行) const originalArgs = process.argv.slice(2); // 快速检查是否是update命令 if (originalArgs[0] === 'update' || originalArgs[0] === 'upgrade') { // 直接执行update命令,不需要额外检查 program.parse(); } else { // 其他命令先执行,后台静默提示 program.parse(); // 命令执行后,静默检查更新 setTimeout(async () => { try { const notification = await versionManager.getUpdateNotification(); if (notification.show) { console.log(chalk.gray(`\n💡 提示: 有新版本 ${notification.command.split(' ').pop()} 可用,运行 'claude-key update' 更新`)); } } catch (error) { // 静默失败 } }, 100); } }