-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcli.js
More file actions
121 lines (102 loc) · 3.24 KB
/
cli.js
File metadata and controls
121 lines (102 loc) · 3.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
#!/usr/bin/env node
const { execSync } = require('child_process');
// ANSI 颜色
const colors = {
reset: '\x1b[0m',
bright: '\x1b[1m',
red: '\x1b[31m',
green: '\x1b[32m',
yellow: '\x1b[33m',
blue: '\x1b[34m',
cyan: '\x1b[36m'
};
function colorize(text, color) {
return `${colors[color] || ''}${text}${colors.reset}`;
}
// 执行命令
function exec(cmd, silent = false) {
try {
return execSync(cmd, { encoding: 'utf-8', stdio: silent ? 'pipe' : 'inherit' });
} catch (error) {
if (!silent) console.error(colorize(`执行失败: ${error.message}`, 'red'));
return null;
}
}
// 检查 OpenClaw
function checkOpenClaw() {
try {
execSync('openclaw --version', { stdio: 'pipe' });
return true;
} catch {
console.error(colorize('❌ OpenClaw 未安装或不在 PATH 中', 'red'));
console.log(colorize('请先安装 OpenClaw: npm install -g openclaw', 'cyan'));
process.exit(1);
}
}
// 获取命令
const args = process.argv.slice(2);
const command = args[0];
// 执行
checkOpenClaw();
switch (command) {
case 'status':
console.log(colorize('\n✓ OpenClaw 状态:\n', 'green'));
exec('openclaw status');
break;
case 'start':
console.log(colorize('启动 Gateway...', 'cyan'));
exec('openclaw gateway start', true);
console.log(colorize('✓ 完成', 'green'));
break;
case 'stop':
console.log(colorize('停止 Gateway...', 'cyan'));
exec('openclaw gateway stop', true);
console.log(colorize('✓ 完成', 'green'));
break;
case 'restart':
console.log(colorize('重启 Gateway...', 'cyan'));
exec('openclaw gateway restart', true);
console.log(colorize('✓ 完成', 'green'));
break;
case 'sessions':
exec('openclaw sessions list');
break;
case 'agents':
exec('openclaw agents list');
break;
case 'update':
console.log(colorize('更新 OpenClaw...', 'cyan'));
exec('npm update -g @qingchencloud/openclaw-zh');
console.log(colorize('✓ 更新完成', 'green'));
break;
case 'doctor':
console.log(colorize('\n🔍 诊断中...\n', 'cyan'));
// 检查版本
try {
const version = execSync('openclaw --version', { encoding: 'utf-8', stdio: 'pipe' });
console.log(colorize('✓', 'green'), 'OpenClaw 版本:', version.trim());
} catch {
console.log(colorize('✗', 'red'), 'OpenClaw 未安装');
}
// 检查 Gateway
try {
execSync('openclaw gateway status', { encoding: 'utf-8', stdio: 'pipe' });
console.log(colorize('✓', 'green'), 'Gateway 状态: 运行中');
} catch {
console.log(colorize('⚠', 'yellow'), 'Gateway 未运行');
}
console.log(colorize('\n诊断完成\n', 'cyan'));
break;
default:
console.log(colorize('OpenClaw 快捷工具 v1.0.0\n', 'bright'));
console.log('使用方法:');
console.log(' ocl status - 查看状态');
console.log(' ocl start - 启动 Gateway');
console.log(' ocl stop - 停止 Gateway');
console.log(' ocl restart - 重启 Gateway');
console.log(' ocl sessions - 查看会话');
console.log(' ocl agents - 查看 agents');
console.log(' ocl update - 更新 OpenClaw');
console.log(' ocl doctor - 诊断问题');
break;
}