ops-toolkit 1.1.0 → 1.2.1

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.
@@ -0,0 +1,433 @@
1
+ # Ops Toolkit 架构文档
2
+
3
+ ## 概述
4
+
5
+ Ops Toolkit 是一个现代化的 DevOps CLI 工具包,采用模块化架构设计,提供系统监控、日志管理、部署工具等功能。
6
+
7
+ ## 系统架构
8
+
9
+ ### 整体架构图
10
+
11
+ ```plantuml
12
+ @startuml
13
+ !theme plain
14
+ skinparam backgroundColor #F8F9FA
15
+ skinparam rectangle {
16
+ BorderColor #2C3E50
17
+ BackgroundColor #ECF0F1
18
+ }
19
+ skinparam package {
20
+ BorderColor #3498DB
21
+ BackgroundColor #EBF5FB
22
+ }
23
+
24
+ package "CLI 入口层" {
25
+ [bin/ops-toolkit.ts] as bin
26
+ [src/index.ts] as index
27
+ }
28
+
29
+ package "CLI 核心层" {
30
+ [src/cli/app.ts] as cli_app
31
+ [src/cli/command-registry.ts] as registry
32
+ [src/cli/command-discovery.ts] as discovery
33
+ }
34
+
35
+ package "命令层" {
36
+ [src/commands/monitor/] as monitor_cmd
37
+ [src/commands/logs/] as logs_cmd
38
+ [src/commands/deploy/] as deploy_cmd
39
+ [src/commands/system/] as system_cmd
40
+ }
41
+
42
+ package "工具层" {
43
+ [src/utils/logger.ts] as logger
44
+ [src/utils/config.ts] as config
45
+ [src/utils/error-handlers.ts] as error_handler
46
+ [src/utils/error-reporter.ts] as error_reporter
47
+ [src/utils/system.ts] as system_utils
48
+ }
49
+
50
+ package "类型定义" {
51
+ [src/types/commands.ts] as types_commands
52
+ [src/types/ui.ts] as types_ui
53
+ [src/types/system.ts] as types_system
54
+ }
55
+
56
+ bin --> cli_app
57
+ index --> cli_app
58
+ cli_app --> registry
59
+ cli_app --> discovery
60
+ discovery --> monitor_cmd
61
+ discovery --> logs_cmd
62
+ discovery --> deploy_cmd
63
+ discovery --> system_cmd
64
+ registry --> logger
65
+ registry --> config
66
+ registry --> error_handler
67
+ monitor_cmd --> system_utils
68
+ logs_cmd --> logger
69
+ deploy_cmd --> config
70
+ system_cmd --> system_utils
71
+ error_handler --> error_reporter
72
+ logger --> types_ui
73
+ config --> types_commands
74
+ system_utils --> types_system
75
+ @enduml
76
+ ```
77
+
78
+ ### 核心组件
79
+
80
+ #### CLI 应用程序 (CLIApp)
81
+
82
+ CLI应用程序是整个系统的核心,负责:
83
+
84
+ - 初始化配置和错误处理
85
+ - 注册和管理命令
86
+ - 提供统一的启动入口
87
+
88
+ #### 命令注册系统 (CommandRegistry)
89
+
90
+ 提供灵活的命令注册机制:
91
+
92
+ - 支持命令和子命令注册
93
+ - 统一的选项处理
94
+ - 命令验证和管理
95
+
96
+ #### 命令发现器 (CommandDiscovery)
97
+
98
+ 自动发现和加载命令:
99
+
100
+ - 扫描命令目录
101
+ - 支持多种导出格式
102
+ - 热加载命令模块
103
+
104
+ ## 数据流架构
105
+
106
+ ### 命令执行流程
107
+
108
+ ```plantuml
109
+ @startuml
110
+ !theme plain
111
+ skinparam participant {
112
+ BackgroundColor #E8F4FD
113
+ BorderColor #3498DB
114
+ }
115
+
116
+ participant User
117
+ participant CLI as "CLI App"
118
+ participant Registry as "CommandRegistry"
119
+ Discovery as "CommandDiscovery"
120
+ Command as "Command"
121
+ Logger as "Logger"
122
+ ErrorHandler as "ErrorHandler"
123
+
124
+ User -> CLI: 执行命令
125
+ CLI -> Registry: 查找命令
126
+ Registry -> Discovery: 发现命令
127
+ Discovery -> Command: 加载命令模块
128
+ Command --> Discovery: 返回命令定义
129
+ Discovery --> Registry: 注册命令
130
+ Registry --> CLI: 返回命令实例
131
+ CLI -> Command: 执行命令
132
+ Command -> Logger: 记录日志
133
+ Command -> ErrorHandler: 处理错误
134
+ CLI -> User: 返回结果
135
+
136
+ @enduml
137
+ ```
138
+
139
+ ### 错误处理流程
140
+
141
+ ```plantuml
142
+ @startuml
143
+ !theme plain
144
+ skinparam participant {
145
+ BackgroundColor #FFF5F5
146
+ BorderColor #E74C3C
147
+ }
148
+
149
+ participant Process
150
+ participant ErrorHandler
151
+ participant ErrorReporter
152
+ participant Logger
153
+ participant File as "Log File"
154
+
155
+ Process -> ErrorHandler: 发生错误
156
+ ErrorHandler -> ErrorReporter: 创建错误报告
157
+ ErrorReporter -> Logger: 记录错误详情
158
+ Logger -> File: 写入文件
159
+ ErrorReporter --> Process: 返回错误报告
160
+ ErrorHandler -> Process: 根据严重程度处理
161
+ Process -> Process: 继续执行或退出
162
+
163
+ @enduml
164
+ ```
165
+
166
+ ## 配置系统
167
+
168
+ ### 配置管理架构
169
+
170
+ ```plantuml
171
+ @startuml
172
+ !theme plain
173
+ skinparam rectangle {
174
+ BackgroundColor #FFF9E6
175
+ BorderColor #F39C12
176
+ }
177
+
178
+ state "配置管理" as Config {
179
+ state "初始化" as Init
180
+ state "加载" as Load
181
+ state "验证" as Validate
182
+ state "使用" as Use
183
+ state "保存" as Save
184
+ state "备份" as Backup
185
+
186
+ Init --> Load
187
+ Load --> Validate
188
+ Validate --> Use
189
+ Use --> Save: 配置更新
190
+ Save --> Backup
191
+ Backup --> Load: 回滚
192
+ }
193
+
194
+ note right of Config
195
+ - 支持配置验证
196
+ - 自动备份机制
197
+ - 环境特定配置
198
+ - 配置热重载
199
+ end note
200
+
201
+ @enduml
202
+ ```
203
+
204
+ ### 配置层次结构
205
+
206
+ ```plantuml
207
+ @startuml
208
+ !theme plain
209
+ skinparam note {
210
+ BackgroundColor #E8F5E8
211
+ BorderColor #27AE60
212
+ }
213
+
214
+ rectangle "全局配置" as Global {
215
+ rectangle "环境配置" as Env
216
+ rectangle "用户配置" as User
217
+ rectangle "系统配置" as System
218
+ }
219
+
220
+ rectangle "模块配置" as Module {
221
+ rectangle "监控配置" as Monitor
222
+ rectangle "日志配置" as Logs
223
+ rectangle "部署配置" as Deploy
224
+ rectangle "UI配置" as UI
225
+ }
226
+
227
+ Global --> Module
228
+ Env --> Module
229
+ User --> Module
230
+ System --> Module
231
+
232
+ note right of Global
233
+ - 环境变量覆盖
234
+ - 用户自定义设置
235
+ - 系统默认值
236
+ end note
237
+
238
+ note right of Module
239
+ - 模块特定配置
240
+ - 继承全局设置
241
+ - 支持验证规则
242
+ end note
243
+
244
+ @enduml
245
+ ```
246
+
247
+ ## 日志系统
248
+
249
+ ### 日志级别和流向
250
+
251
+ ```plantuml
252
+ @startuml
253
+ !theme plain
254
+ skinparam participant {
255
+ BackgroundColor #F0F8FF
256
+ BorderColor #5DADE2
257
+ }
258
+
259
+ participant Application
260
+ participant Logger
261
+ participant FileHandler
262
+ participant ConsoleHandler
263
+ participant Rotator as "LogRotator"
264
+
265
+ Application -> Logger: 写入日志
266
+ Logger -> Logger: 检查日志级别
267
+ alt 超过级别限制
268
+ Logger --> Application: 忽略日志
269
+ else 符合级别要求
270
+ Logger -> ConsoleHandler: 输出到控制台
271
+ Logger -> FileHandler: 写入文件
272
+ FileHandler -> Rotator: 检查文件大小
273
+ alt 文件过大
274
+ Rotator -> Rotator: 轮转日志文件
275
+ end
276
+ FileHandler --> Application: 写入完成
277
+ end
278
+ @enduml
279
+ ```
280
+
281
+ ## 开发指南
282
+
283
+ ### 添加新命令
284
+
285
+ 1. **创建命令目录**
286
+
287
+ ```bash
288
+ mkdir src/commands/my-command
289
+ ```
290
+
291
+ 2. **实现命令类**
292
+
293
+ ```typescript
294
+ export const MyCommand: CommandDefinition = {
295
+ name: 'my-command',
296
+ description: '我的自定义命令',
297
+ action: async options => {
298
+ // 命令逻辑
299
+ },
300
+ };
301
+ ```
302
+
303
+ 3. **自动发现**
304
+ 命令发现器会自动扫描并注册新命令。
305
+
306
+ ### 扩展配置
307
+
308
+ ```typescript
309
+ // 注册配置验证器
310
+ ConfigManager.registerValidator('myValidator', {
311
+ validate: config => {
312
+ /* 验证逻辑 */
313
+ },
314
+ getErrors: () => {
315
+ /* 返回错误信息 */
316
+ },
317
+ });
318
+ ```
319
+
320
+ ### 错误处理
321
+
322
+ ```typescript
323
+ try {
324
+ // 业务逻辑
325
+ } catch (error) {
326
+ errorHandler(
327
+ error,
328
+ {
329
+ command: 'my-command',
330
+ action: 'execute',
331
+ },
332
+ ErrorSeverity.HIGH
333
+ );
334
+ }
335
+ ```
336
+
337
+ ## 部署和构建
338
+
339
+ ### 构建流程
340
+
341
+ ```plantuml
342
+ @startuml
343
+ !theme plain
344
+ skinparam rectangle {
345
+ BackgroundColor #F8F9FA
346
+ BorderColor #6C757D
347
+ }
348
+
349
+ rectangle "源代码" as Source {
350
+ rectangle "TypeScript" as TS
351
+ rectangle "类型定义" as Types
352
+ }
353
+
354
+ rectangle "构建过程" as Build {
355
+ rectangle "类型检查" as TypeCheck
356
+ rectangle "代码检查" as Lint
357
+ rectangle "编译" as Compile
358
+ }
359
+
360
+ rectangle "输出" as Output {
361
+ rectangle "JavaScript" as JS
362
+ rectangle "类型声明" as DTS
363
+ }
364
+
365
+ TS --> TypeCheck
366
+ Types --> TypeCheck
367
+ TypeCheck --> Lint
368
+ Lint --> Compile
369
+ Compile --> JS
370
+ Compile --> DTS
371
+
372
+ @enduml
373
+ ```
374
+
375
+ ### 质量保证
376
+
377
+ - **类型检查**:`bun run typecheck`
378
+ - **代码规范**:`bun run lint`
379
+ - **自动修复**:`bun run lint:fix`
380
+ - **格式化**:`bun run format`
381
+
382
+ ## 性能优化
383
+
384
+ ### 内存管理
385
+
386
+ - 日志缓冲区限制(1000条)
387
+ - 配置缓存机制
388
+ - 命令延迟加载
389
+
390
+ ### 异步处理
391
+
392
+ - 所有I/O操作使用异步模式
393
+ - 错误处理不阻塞主流程
394
+ - 并发命令执行支持
395
+
396
+ ## 安全考虑
397
+
398
+ - 配置文件权限控制
399
+ - 敏感信息不记录日志
400
+ - 输入验证和清理
401
+ - 错误信息脱敏
402
+
403
+ ## 扩展性
404
+
405
+ ### 插件系统
406
+
407
+ 系统支持通过命令发现机制扩展插件:
408
+
409
+ ```typescript
410
+ // 插件示例
411
+ export class MyPlugin {
412
+ static commands = [MyCommand1, MyCommand2];
413
+ static config = {
414
+ /* 插件配置 */
415
+ };
416
+ }
417
+ ```
418
+
419
+ ### API接口
420
+
421
+ 统一的命令接口确保兼容性和可测试性。
422
+
423
+ ## 总结
424
+
425
+ Ops Toolkit 采用现代化的架构设计,具备以下特点:
426
+
427
+ - **模块化**:清晰的层次结构
428
+ - **可扩展**:灵活的插件机制
429
+ - **健壮性**:完善的错误处理
430
+ - **可维护**:类型安全和文档完善
431
+ - **高性能**:异步和优化机制
432
+
433
+ 这个架构为未来的功能扩展和性能优化提供了坚实的基础。