ops-toolkit 1.2.0 → 1.2.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,60 +1,201 @@
1
- import { Command } from 'commander';
2
- import chalk from 'chalk';
1
+ import { type CommandDefinition } from '@/cli/command-registry';
2
+ import { Logger } from '@/utils/logger';
3
+ import { SystemUtils } from '@/utils/system';
3
4
  import type { MonitorOptions } from '@/types/commands';
5
+ import chalk from 'chalk';
6
+
7
+ /**
8
+ * 系统监控命令处理器
9
+ */
10
+ class MonitorHandler {
11
+ /**
12
+ * 系统资源监控
13
+ */
14
+ async handleSystem(options: MonitorOptions): Promise<void> {
15
+ console.log(chalk.blue('📊 系统资源监控'));
16
+ console.log(chalk.gray(`刷新间隔: ${options.refresh}秒`));
17
+
18
+ const systemInfo = await SystemUtils.getSystemInfo();
19
+ const memoryUsage = SystemUtils.getMemoryUsage();
20
+ const cpuUsage = await SystemUtils.getCpuUsage();
21
+
22
+ console.log(chalk.cyan('\n系统信息:'));
23
+ console.log(` 主机名: ${systemInfo.hostname}`);
24
+ console.log(` 平台: ${systemInfo.platform}`);
25
+ console.log(` 架构: ${systemInfo.arch}`);
26
+ console.log(` 运行时间: ${SystemUtils.formatUptime(systemInfo.uptime)}`);
27
+
28
+ console.log(chalk.cyan('\n内存使用情况:'));
29
+ console.log(` 总内存: ${SystemUtils.formatBytes(memoryUsage.total)}`);
30
+ console.log(` 已使用: ${SystemUtils.formatBytes(memoryUsage.used)}`);
31
+ console.log(` 空闲内存: ${SystemUtils.formatBytes(memoryUsage.free)}`);
32
+ console.log(` 使用率: ${memoryUsage.percentage}%`);
33
+
34
+ console.log(chalk.cyan('\nCPU使用率:'));
35
+ console.log(` 当前使用率: ${cpuUsage}%`);
36
+
37
+ Logger.info('系统监控完成');
38
+ }
39
+
40
+ /**
41
+ * 进程监控
42
+ */
43
+ async handleProcesses(options: MonitorOptions): Promise<void> {
44
+ console.log(chalk.blue('🔄 进程监控'));
45
+ console.log(chalk.gray(`排序字段: ${options.sort}`));
46
+
47
+ const processes = await SystemUtils.getProcessList();
48
+
49
+ // 排序进程
50
+ const sortedProcesses = processes.sort((a, b) => {
51
+ switch (options.sort) {
52
+ case 'memory':
53
+ return b.memory - a.memory;
54
+ case 'name':
55
+ return a.command.localeCompare(b.command);
56
+ case 'cpu':
57
+ default:
58
+ return b.cpu - a.cpu;
59
+ }
60
+ });
61
+
62
+ // 限制数量
63
+ const limit = parseInt(options.limit?.toString() || '20');
64
+ const limitedProcesses = sortedProcesses.slice(0, limit);
65
+
66
+ console.log(chalk.cyan('\n进程列表:'));
67
+ console.log('PID\tCPU%\tMEM%\t用户\t命令');
68
+ console.log('---\t----\t----\t----\t---');
69
+
70
+ limitedProcesses.forEach(process => {
71
+ console.log(
72
+ `${process.pid}\t${process.cpu.toFixed(1)}\t${process.memory.toFixed(1)}\t${process.user}\t${process.command.substring(0, 50)}`
73
+ );
74
+ });
75
+
76
+ Logger.info(`显示 ${limitedProcesses.length} 个进程`);
77
+ }
78
+
79
+ /**
80
+ * 网络监控
81
+ */
82
+ async handleNetwork(_options: MonitorOptions): Promise<void> {
83
+ console.log(chalk.blue('🌐 网络监控'));
84
+ console.log(chalk.yellow('⚠️ 网络监控功能开发中...'));
85
+ }
86
+
87
+ /**
88
+ * 磁盘监控
89
+ */
90
+ async handleDisk(_options: MonitorOptions): Promise<void> {
91
+ console.log(chalk.blue('💾 磁盘使用监控'));
92
+ console.log(chalk.yellow('⚠️ 磁盘监控功能开发中...'));
93
+ }
94
+ }
95
+
96
+ /**
97
+ * 监控命令定义
98
+ */
99
+ export const MonitorCommand: CommandDefinition = {
100
+ name: 'monitor',
101
+ description: '系统监控命令',
102
+ alias: 'mon',
103
+ options: [
104
+ {
105
+ flags: '-r, --refresh <seconds>',
106
+ description: '刷新间隔(秒)',
107
+ defaultValue: '5',
108
+ },
109
+ {
110
+ flags: '-v, --verbose',
111
+ description: '显示详细信息',
112
+ defaultValue: false,
113
+ },
114
+ {
115
+ flags: '-o, --output <format>',
116
+ description: '输出格式(table|json)',
117
+ defaultValue: 'table',
118
+ },
119
+ ],
120
+ subcommands: [
121
+ {
122
+ name: 'system',
123
+ description: '显示系统资源使用情况',
124
+ action: async (options: MonitorOptions) => {
125
+ const handler = new MonitorHandler();
126
+ await handler.handleSystem(options);
127
+ },
128
+ },
129
+ {
130
+ name: 'processes',
131
+ description: '显示运行中的进程',
132
+ options: [
133
+ {
134
+ flags: '-s, --sort <field>',
135
+ description: '排序字段(cpu|memory|name)',
136
+ defaultValue: 'cpu',
137
+ },
138
+ {
139
+ flags: '-l, --limit <number>',
140
+ description: '进程数量限制',
141
+ defaultValue: '20',
142
+ },
143
+ {
144
+ flags: '-u, --user <user>',
145
+ description: '按用户过滤',
146
+ },
147
+ ],
148
+ action: async (options: MonitorOptions) => {
149
+ const handler = new MonitorHandler();
150
+ await handler.handleProcesses(options);
151
+ },
152
+ },
153
+ {
154
+ name: 'network',
155
+ description: '显示网络统计信息',
156
+ options: [
157
+ {
158
+ flags: '-i, --interface <iface>',
159
+ description: '指定网络接口',
160
+ },
161
+ {
162
+ flags: '-r, --realtime',
163
+ description: '实时网络监控',
164
+ defaultValue: false,
165
+ },
166
+ ],
167
+ action: async (_options: MonitorOptions) => {
168
+ const handler = new MonitorHandler();
169
+ await handler.handleNetwork(_options);
170
+ },
171
+ },
172
+ {
173
+ name: 'disk',
174
+ description: '显示磁盘使用情况',
175
+ options: [
176
+ {
177
+ flags: '-a, --all',
178
+ description: '显示所有文件系统',
179
+ defaultValue: false,
180
+ },
181
+ {
182
+ flags: '-h, --human',
183
+ description: '人类可读格式',
184
+ defaultValue: true,
185
+ },
186
+ ],
187
+ action: async (_options: MonitorOptions) => {
188
+ const handler = new MonitorHandler();
189
+ await handler.handleDisk(_options);
190
+ },
191
+ },
192
+ ],
193
+ action: async (options: MonitorOptions) => {
194
+ // 默认执行系统监控
195
+ const handler = new MonitorHandler();
196
+ await handler.handleSystem(options);
197
+ },
198
+ };
4
199
 
5
- // 创建监控命令
6
- export const MonitorCommand = new Command('monitor')
7
- .description('System monitoring commands')
8
- .alias('mon');
9
-
10
- // 系统资源监控
11
- MonitorCommand.command('system')
12
- .description('Show system resource usage')
13
- .option('-r, --refresh <seconds>', 'Refresh interval in seconds', '5')
14
- .option('-v, --verbose', 'Show detailed information', false)
15
- .option('-o, --output <format>', 'Output format (table|json)', 'table')
16
- .action(async (_options: MonitorOptions) => {
17
- console.log(chalk.blue('📊 System Resource Monitor'));
18
- console.log(chalk.gray(`Refresh interval: ${_options.refresh}s`));
19
-
20
- // TODO: 实现系统监控逻辑
21
- console.log(chalk.yellow('⚠️ System monitoring feature coming soon...'));
22
- });
23
-
24
- // 进程监控
25
- MonitorCommand.command('processes')
26
- .description('Show running processes')
27
- .option('-s, --sort <field>', 'Sort by field (cpu|memory|name)', 'cpu')
28
- .option('-l, --limit <number>', 'Limit number of processes', '20')
29
- .option('-u, --user <user>', 'Filter by user')
30
- .action(async (_options: MonitorOptions) => {
31
- console.log(chalk.blue('🔄 Process Monitor'));
32
- console.log(chalk.gray(`Sort by: ${_options.sort}`));
33
-
34
- // TODO: 实现进程监控逻辑
35
- console.log(chalk.yellow('⚠️ Process monitoring feature coming soon...'));
36
- });
37
-
38
- // 网络监控
39
- MonitorCommand.command('network')
40
- .description('Show network statistics')
41
- .option('-i, --interface <iface>', 'Specific network interface')
42
- .option('-r, --realtime', 'Real-time network monitoring', false)
43
- .action(async (_options: MonitorOptions) => {
44
- console.log(chalk.blue('🌐 Network Monitor'));
45
-
46
- // TODO: 实现网络监控逻辑
47
- console.log(chalk.yellow('⚠️ Network monitoring feature coming soon...'));
48
- });
49
-
50
- // 磁盘监控
51
- MonitorCommand.command('disk')
52
- .description('Show disk usage')
53
- .option('-a, --all', 'Show all filesystems', false)
54
- .option('-h, --human', 'Human readable format', true)
55
- .action(async (_options: MonitorOptions) => {
56
- console.log(chalk.blue('💾 Disk Usage Monitor'));
57
-
58
- // TODO: 实现磁盘监控逻辑
59
- console.log(chalk.yellow('⚠️ Disk monitoring feature coming soon...'));
60
- });
200
+ // 默认导出命令定义
201
+ export default MonitorCommand;
package/src/index.ts CHANGED
@@ -1,82 +1,9 @@
1
1
  #!/usr/bin/env bun
2
2
 
3
- import { program } from 'commander';
4
- import chalk from 'chalk';
5
- import figlet from 'figlet';
3
+ import { createCLI } from './cli/app';
6
4
 
7
- async function main() {
8
- const welcomeText = figlet.textSync('ops-toolkit', {
9
- font: 'Standard',
10
- horizontalLayout: 'default',
11
- verticalLayout: 'default',
12
- });
13
-
14
- console.log(chalk.cyan(welcomeText));
15
-
16
- program.name('ops').description('全面的DevOps CLI工具包').version('1.0.0');
17
-
18
- program.option('-d, --debug', '启用调试模式', false);
19
- program.option('-v, --verbose', '启用详细日志', false);
20
-
21
- program
22
- .command('ui', { isDefault: true })
23
- .description('启动交互式终端界面')
24
- .action(async () => {
25
- console.log(chalk.green('🚀 ops-toolkit CLI正在运行!'));
26
- console.log(chalk.blue('📋 可用命令:'));
27
- console.log(chalk.white(' ops monitor - 系统监控'));
28
- console.log(chalk.white(' ops logs - 日志管理'));
29
- console.log(chalk.white(' ops deploy - 部署工具'));
30
- console.log(chalk.white(' ops system - 系统管理'));
31
- console.log(chalk.gray('\n🔧 UI功能即将推出...'));
32
- });
33
-
34
- program
35
- .command('monitor')
36
- .description('系统监控')
37
- .action(async () => {
38
- console.log(chalk.blue('📊 系统监控'));
39
- console.log(chalk.yellow('⚠️ 监控功能即将推出...'));
40
- });
41
-
42
- program
43
- .command('logs')
44
- .description('日志管理')
45
- .action(async () => {
46
- console.log(chalk.blue('📋 日志管理'));
47
- console.log(chalk.yellow('⚠️ 日志管理功能即将推出...'));
48
- });
49
-
50
- program
51
- .command('deploy')
52
- .description('部署工具')
53
- .action(async () => {
54
- console.log(chalk.blue('🚀 部署工具'));
55
- console.log(chalk.yellow('⚠️ 部署功能即将推出...'));
56
- });
57
-
58
- program
59
- .command('system')
60
- .description('系统管理')
61
- .action(async () => {
62
- console.log(chalk.blue('⚙️ 系统管理'));
63
- console.log(chalk.yellow('⚠️ 系统管理功能即将推出...'));
64
- });
65
-
66
- program.parse();
67
- }
68
-
69
- process.on('uncaughtException', error => {
70
- console.error(chalk.red('❌ 未捕获异常:'), error);
71
- process.exit(1);
72
- });
73
-
74
- process.on('unhandledRejection', (reason, promise) => {
75
- console.error(chalk.red('❌ 未处理的Promise拒绝:'), promise, '原因:', reason);
76
- process.exit(1);
77
- });
78
-
79
- main().catch(error => {
80
- console.error(chalk.red('❌ 启动应用失败:'), error);
5
+ // 启动CLI应用程序
6
+ createCLI().catch(error => {
7
+ console.error('启动失败:', error);
81
8
  process.exit(1);
82
9
  });
package/src/types/ui.ts CHANGED
@@ -2,7 +2,7 @@
2
2
  export interface UIComponent {
3
3
  id: string;
4
4
  type: 'text' | 'table' | 'chart' | 'form' | 'layout';
5
- props?: Record<string, any>;
5
+ props?: Record<string, unknown>;
6
6
  children?: UIComponent[];
7
7
  }
8
8
 
@@ -40,7 +40,7 @@ export interface FormField {
40
40
  type: 'text' | 'password' | 'select' | 'checkbox' | 'radio';
41
41
  required?: boolean;
42
42
  options?: string[];
43
- defaultValue?: any;
43
+ defaultValue?: string | number | boolean;
44
44
  }
45
45
 
46
46
  export interface Theme {
@@ -57,5 +57,5 @@ export interface AppState {
57
57
  currentPage: string;
58
58
  loading: boolean;
59
59
  error?: string;
60
- data: Record<string, any>;
60
+ data: Record<string, unknown>;
61
61
  }