@vitex/tcm-cli 0.0.0-stage → 0.2.0
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.
- package/LICENSE +29 -0
- package/README.md +206 -2
- package/bin/tcm.js +15 -0
- package/package.json +33 -4
- package/src/args.js +107 -0
- package/src/auth.js +46 -0
- package/src/commands/account.js +96 -0
- package/src/commands/agent-artifact.js +48 -0
- package/src/commands/auth.js +93 -0
- package/src/commands/author.js +71 -0
- package/src/commands/aweme.js +33 -0
- package/src/commands/comment-candidate.js +39 -0
- package/src/commands/comment-strategy.js +65 -0
- package/src/commands/comment.js +66 -0
- package/src/commands/common.js +36 -0
- package/src/commands/draft.js +69 -0
- package/src/commands/extension-artifact.js +47 -0
- package/src/commands/extension-task.js +57 -0
- package/src/commands/hot-comment.js +19 -0
- package/src/commands/index.js +42 -0
- package/src/commands/item.js +20 -0
- package/src/commands/machine-job.js +33 -0
- package/src/commands/machine-task.js +39 -0
- package/src/commands/machine.js +83 -0
- package/src/commands/material.js +60 -0
- package/src/commands/user.js +55 -0
- package/src/commands/workbench.js +43 -0
- package/src/config.js +8 -0
- package/src/http.js +97 -0
- package/src/index.js +293 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
Proprietary License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 58.com and/or its affiliates. All rights reserved.
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of
|
|
6
|
+
this software and associated documentation files (the "Software"), to use the
|
|
7
|
+
Software solely for its intended purpose, subject to the following conditions:
|
|
8
|
+
|
|
9
|
+
1. Use only. You may install and use the Software, but you may not modify,
|
|
10
|
+
adapt, translate, reverse engineer, decompile, disassemble, or create
|
|
11
|
+
derivative works of the Software.
|
|
12
|
+
|
|
13
|
+
2. No redistribution. You may not copy, publish, distribute, sublicense, sell,
|
|
14
|
+
rent, lease, or otherwise make the Software available to any third party, in
|
|
15
|
+
whole or in part, without prior written permission from the copyright
|
|
16
|
+
holder.
|
|
17
|
+
|
|
18
|
+
3. Notices. You may not remove or alter this license or any copyright,
|
|
19
|
+
trademark, or other proprietary notice contained in the Software.
|
|
20
|
+
|
|
21
|
+
4. No warranty. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
22
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
23
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO
|
|
24
|
+
EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES
|
|
25
|
+
OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
|
|
26
|
+
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
|
27
|
+
DEALINGS IN THE SOFTWARE.
|
|
28
|
+
|
|
29
|
+
Any use not expressly granted above is prohibited.
|
package/README.md
CHANGED
|
@@ -1,3 +1,207 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @vitex/tcm-cli
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
TCM 控制台命令行工具,面向 agent 使用。所有业务输入/输出都是 JSON,可直接被脚本或 agent 解析。
|
|
4
|
+
|
|
5
|
+
- 零运行时依赖,基于 Node.js 内置模块(Node ≥ 18.17)
|
|
6
|
+
- 常规 CLI 体验:`help`、子命令、`--help`、退出码
|
|
7
|
+
- `login` / `logout` / `status` 三个命令管理授权
|
|
8
|
+
- 授权信息保存在 `~/.tmc/auth.json`,所有业务子命令从这里读取用户 Key
|
|
9
|
+
|
|
10
|
+
## 安装
|
|
11
|
+
|
|
12
|
+
```bash
|
|
13
|
+
# 发布后
|
|
14
|
+
npm install -g @vitex/tcm-cli
|
|
15
|
+
|
|
16
|
+
# 或本地开发
|
|
17
|
+
node bin/tcm.js --help
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
## 快速开始
|
|
21
|
+
|
|
22
|
+
```bash
|
|
23
|
+
# 登录:用户 Key 在控制台「系统 - 用户」里获取(uk_ 开头)
|
|
24
|
+
tcm login --key uk_xxxxxxxx --server https://tcm.example.com
|
|
25
|
+
|
|
26
|
+
# 也可以从 stdin 读取,避免 Key 进入 shell 历史
|
|
27
|
+
cat key.txt | tcm login --key - --server https://tcm.example.com
|
|
28
|
+
|
|
29
|
+
# 也可以用位置参数或环境变量
|
|
30
|
+
tcm login uk_xxxxxxxx
|
|
31
|
+
TCM_SERVER_URL=https://tcm.example.com tcm login --key uk_xxxxxxxx
|
|
32
|
+
|
|
33
|
+
# 查看当前登录状态
|
|
34
|
+
tcm status
|
|
35
|
+
|
|
36
|
+
# 调用后端接口
|
|
37
|
+
tcm machine list --statuses NORMAL --pageSize 20
|
|
38
|
+
tcm account get --id 12
|
|
39
|
+
tcm machine-job create --type EXEC --machineIds 1,2 --input '{"command":"ipconfig /all"}'
|
|
40
|
+
|
|
41
|
+
# 退出登录
|
|
42
|
+
tcm logout
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
## 授权
|
|
46
|
+
|
|
47
|
+
授权文件默认位于 `~/.tmc/auth.json`(目录 0700、文件 0600),内容形如:
|
|
48
|
+
|
|
49
|
+
```json
|
|
50
|
+
{
|
|
51
|
+
"serverUrl": "https://tcm.example.com",
|
|
52
|
+
"apiKey": "uk_xxxxxxxx",
|
|
53
|
+
"user": { "id": 1, "teamId": 9, "name": "alice", "roles": ["ADMIN"] },
|
|
54
|
+
"createdAt": "2025-01-01T00:00:00.000Z"
|
|
55
|
+
}
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
所有请求使用 `Authorization: ApiKey <key>`。服务端地址、Key、授权文件路径都可以临时覆盖:
|
|
59
|
+
|
|
60
|
+
| 环境变量 | 说明 |
|
|
61
|
+
| --- | --- |
|
|
62
|
+
| `TCM_SERVER_URL` | 服务端地址(默认 `https://tcm.58corp.com/`) |
|
|
63
|
+
| `TCM_API_KEY` | 用户 Key |
|
|
64
|
+
| `TCM_AUTH_FILE` | 授权文件路径(默认 `~/.tmc/auth.json`) |
|
|
65
|
+
|
|
66
|
+
优先级:命令行参数 > 环境变量 > 授权文件 > 默认值。
|
|
67
|
+
|
|
68
|
+
## 全局选项
|
|
69
|
+
|
|
70
|
+
| 选项 | 说明 |
|
|
71
|
+
| --- | --- |
|
|
72
|
+
| `--server <url>` | 覆盖服务端地址 |
|
|
73
|
+
| `--key <key>` | 覆盖用户 Key |
|
|
74
|
+
| `--auth-file <path>` | 授权文件路径 |
|
|
75
|
+
| `--timeout <ms>` | 请求超时毫秒数(默认 30000) |
|
|
76
|
+
| `--compact` | 紧凑 JSON 输出(单行) |
|
|
77
|
+
| `--version, -V` | 显示版本号 |
|
|
78
|
+
| `--help, -h` | 显示帮助 |
|
|
79
|
+
|
|
80
|
+
## 输出与退出码
|
|
81
|
+
|
|
82
|
+
- 成功:将后端 `ApiResponse.data` 以 JSON 写到 stdout
|
|
83
|
+
- 失败:将错误以 JSON 写到 stderr,例如:
|
|
84
|
+
|
|
85
|
+
```json
|
|
86
|
+
{
|
|
87
|
+
"error": {
|
|
88
|
+
"type": "ApiError",
|
|
89
|
+
"message": "未登录或用户 Key 已失效",
|
|
90
|
+
"code": 401,
|
|
91
|
+
"status": 401,
|
|
92
|
+
"data": null
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
```
|
|
96
|
+
|
|
97
|
+
| 退出码 | 含义 |
|
|
98
|
+
| --- | --- |
|
|
99
|
+
| `0` | 成功 |
|
|
100
|
+
| `1` | 业务/网络/授权错误(`ApiError`) |
|
|
101
|
+
| `2` | 参数或用法错误(`UsageError`) |
|
|
102
|
+
|
|
103
|
+
业务失败(HTTP 200 但 `code !== 1`)与 HTTP 非 2xx 都会被视为失败。触发限流时后端返回 `HTTP 429`,错误的 `data.retryAfterMs` 给出建议重试等待毫秒数。
|
|
104
|
+
|
|
105
|
+
## 参数约定
|
|
106
|
+
|
|
107
|
+
- 标量:`--pageNum 1`、`--status NORMAL`
|
|
108
|
+
- 数组:可重复或逗号分隔,也支持 JSON 数组
|
|
109
|
+
- `--statuses NORMAL,HIDDEN`
|
|
110
|
+
- `--statuses NORMAL --statuses HIDDEN`
|
|
111
|
+
- `--statuses '["NORMAL","HIDDEN"]'`
|
|
112
|
+
- JSON 字段:传 JSON 字符串、`@文件` 或 `-`(stdin)
|
|
113
|
+
- `--input '{"command":"ls"}'`
|
|
114
|
+
- `--input @input.json`
|
|
115
|
+
- 文件上传(multipart):传本地路径,如 `--file ./a.png`
|
|
116
|
+
- 布尔筛选:出现即为 `true`,例如 `--verified`
|
|
117
|
+
- 必填布尔值:显式写 `true` / `false`,例如 `tcm comment update --folded false`(`--folded` 这种开关写法表达不了 `false`)
|
|
118
|
+
|
|
119
|
+
## 命令一览
|
|
120
|
+
|
|
121
|
+
通用命令:
|
|
122
|
+
|
|
123
|
+
- `tcm login` — 使用用户 Key 登录并保存到 `~/.tmc/auth.json`
|
|
124
|
+
- `tcm logout` — 退出登录并删除本地授权文件
|
|
125
|
+
- `tcm status` — 查看当前登录状态(`--local` 只读本地)
|
|
126
|
+
|
|
127
|
+
### user — 用户
|
|
128
|
+
|
|
129
|
+
- `tcm user list` — 分页查询用户(管理员)
|
|
130
|
+
- `tcm user me` — 当前登录用户
|
|
131
|
+
- `tcm user create` — 新建用户(返回仅此一次可见的明文密码)
|
|
132
|
+
- `tcm user update` — 编辑用户
|
|
133
|
+
- `tcm user reset-password` — 重置用户密码
|
|
134
|
+
- `tcm user api-key` — 获取当前用户 API Key(旧 Key 立即失效)
|
|
135
|
+
- `tcm user change-password` — 修改当前用户密码
|
|
136
|
+
|
|
137
|
+
### workbench — 工作台统计
|
|
138
|
+
|
|
139
|
+
- `account-cards`、`machine-cards`
|
|
140
|
+
- `accounts`、`comments`、`awemes`、`comment-records`、`comment-candidates`、`comment-likes`、`top-comments`
|
|
141
|
+
- `tasks`、`task-success-rate`、`task-type-trend`、`task-status-trend`、`task-type-share`、`task-status-share`、`task-error-top`
|
|
142
|
+
- `comment-overview-trend`
|
|
143
|
+
|
|
144
|
+
统计接口一般需要 `--startDate` / `--endDate`;任务类接口还支持 `--types`、`--status`。
|
|
145
|
+
|
|
146
|
+
### machine — 机器
|
|
147
|
+
|
|
148
|
+
`list`、`get`、`create`、`update`、`status`、`names`、`summary`、`logs`、`statistics`
|
|
149
|
+
|
|
150
|
+
### account — 账号
|
|
151
|
+
|
|
152
|
+
`list`、`get`、`update`、`status`、`toggle-verify`、`brief`、`summary`、`logs`、`cookies`
|
|
153
|
+
|
|
154
|
+
### machine-job — 机器作业
|
|
155
|
+
|
|
156
|
+
`list`、`create`(`EXEC` / `REBOOT` / `UPDATE` / `UPDATE_EXTENSION`)
|
|
157
|
+
|
|
158
|
+
### machine-task — 机器任务
|
|
159
|
+
|
|
160
|
+
`list`、`cancel`、`abort`、`reset`
|
|
161
|
+
|
|
162
|
+
### extension-task — 插件任务
|
|
163
|
+
|
|
164
|
+
`list`、`get`、`create`、`cancel`、`abort`、`reset`
|
|
165
|
+
|
|
166
|
+
### agent-artifact / extension-artifact — 制品
|
|
167
|
+
|
|
168
|
+
`list`、`create`、`update`、`activate`、`statistics`
|
|
169
|
+
|
|
170
|
+
### author / aweme / comment / comment-candidate / hot-comment — 神评
|
|
171
|
+
|
|
172
|
+
- `author`:`list`、`get`、`create`、`update`、`status`、`profile`
|
|
173
|
+
- `aweme`:`list`、`get`、`analysis`
|
|
174
|
+
- `comment`:`list`、`get`、`refresh`、`update`
|
|
175
|
+
- `comment-candidate`:`list`、`discard`
|
|
176
|
+
- `comment-strategy`(仅管理员):`list`、`get`、`create`、`update`、`delete`
|
|
177
|
+
- `hot-comment`:`list`
|
|
178
|
+
|
|
179
|
+
### material / draft / item — 素材 / 草稿 / 作品
|
|
180
|
+
|
|
181
|
+
- `material`:`list`、`get`、`create`、`update`、`upload`
|
|
182
|
+
- `draft`:`list`、`create`、`create-urls`、`discard`
|
|
183
|
+
- `item`:`list`
|
|
184
|
+
|
|
185
|
+
每个命令都支持 `--help` 查看完整参数,例如:
|
|
186
|
+
|
|
187
|
+
```bash
|
|
188
|
+
tcm machine list --help
|
|
189
|
+
tcm machine-job create --help
|
|
190
|
+
```
|
|
191
|
+
|
|
192
|
+
## Agent 用法示例
|
|
193
|
+
|
|
194
|
+
```bash
|
|
195
|
+
# 获取第一页在线机器,单行 JSON
|
|
196
|
+
tcm machine list --health ONLINE --compact
|
|
197
|
+
|
|
198
|
+
# 批量创建执行作业
|
|
199
|
+
tcm machine-job create --type EXEC --machineIds 1,2,3 \
|
|
200
|
+
--input '{"command":"systeminfo","timeoutSec":120}' --compact
|
|
201
|
+
|
|
202
|
+
# 组合筛选条件
|
|
203
|
+
tcm account list --statuses NORMAL --machineId 3 --compact
|
|
204
|
+
|
|
205
|
+
# 把作业输入写到文件再引用
|
|
206
|
+
tcm machine-job create --type EXEC --machineIds 1 --input @job.json
|
|
207
|
+
```
|
package/bin/tcm.js
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import { main } from '../src/index.js'
|
|
3
|
+
|
|
4
|
+
main(process.argv.slice(2)).then(
|
|
5
|
+
(code) => {
|
|
6
|
+
process.exitCode = code
|
|
7
|
+
},
|
|
8
|
+
(error) => {
|
|
9
|
+
// main 内部已经处理了所有已知错误,这里只兜底,避免抛栈污染 JSON 输出。
|
|
10
|
+
process.stderr.write(
|
|
11
|
+
JSON.stringify({ error: { type: 'InternalError', message: error?.message || String(error) } }) + '\n',
|
|
12
|
+
)
|
|
13
|
+
process.exitCode = 1
|
|
14
|
+
},
|
|
15
|
+
)
|
package/package.json
CHANGED
|
@@ -1,6 +1,35 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@vitex/tcm-cli",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.2.0",
|
|
4
|
+
"description": "TCM 控制台命令行工具(面向 agent,JSON 输入输出)",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"bin": {
|
|
7
|
+
"tcm": "bin/tcm.js"
|
|
8
|
+
},
|
|
9
|
+
"publishConfig": {
|
|
10
|
+
"access": "public"
|
|
11
|
+
},
|
|
12
|
+
"exports": {
|
|
13
|
+
".": "./src/index.js"
|
|
14
|
+
},
|
|
15
|
+
"files": [
|
|
16
|
+
"bin",
|
|
17
|
+
"src",
|
|
18
|
+
"README.md"
|
|
19
|
+
],
|
|
20
|
+
"engines": {
|
|
21
|
+
"node": ">=18.17"
|
|
22
|
+
},
|
|
23
|
+
"scripts": {
|
|
24
|
+
"start": "node bin/tcm.js",
|
|
25
|
+
"test": "node --test",
|
|
26
|
+
"prepublishOnly": "npm test"
|
|
27
|
+
},
|
|
28
|
+
"keywords": [
|
|
29
|
+
"tcm",
|
|
30
|
+
"cli",
|
|
31
|
+
"agent"
|
|
32
|
+
],
|
|
33
|
+
"license": "SEE LICENSE IN LICENSE",
|
|
34
|
+
"private": false
|
|
35
|
+
}
|
package/src/args.js
ADDED
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
import fs from 'node:fs'
|
|
2
|
+
import { parseArgs } from 'node:util'
|
|
3
|
+
|
|
4
|
+
export class UsageError extends Error {
|
|
5
|
+
constructor(message) {
|
|
6
|
+
super(message)
|
|
7
|
+
this.name = 'UsageError'
|
|
8
|
+
}
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
/** 读取 JSON 参数:支持内联 JSON、@文件、- 表示 stdin。 */
|
|
12
|
+
export function parseJsonArg(raw) {
|
|
13
|
+
const value = String(raw)
|
|
14
|
+
let text
|
|
15
|
+
if (value === '-') {
|
|
16
|
+
text = fs.readFileSync(0, 'utf8')
|
|
17
|
+
} else if (value.startsWith('@')) {
|
|
18
|
+
text = fs.readFileSync(value.slice(1), 'utf8')
|
|
19
|
+
} else {
|
|
20
|
+
text = value
|
|
21
|
+
}
|
|
22
|
+
return JSON.parse(text)
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
function coerce(name, spec, raw) {
|
|
26
|
+
switch (spec.type) {
|
|
27
|
+
case 'number': {
|
|
28
|
+
const value = Number(raw)
|
|
29
|
+
if (!Number.isFinite(value)) throw new UsageError(`--${name} 需要是数字`)
|
|
30
|
+
return value
|
|
31
|
+
}
|
|
32
|
+
case 'array': {
|
|
33
|
+
const items = Array.isArray(raw) ? raw : [raw]
|
|
34
|
+
const out = []
|
|
35
|
+
for (const item of items) {
|
|
36
|
+
const text = String(item).trim()
|
|
37
|
+
if (!text) continue
|
|
38
|
+
if (text.startsWith('[')) {
|
|
39
|
+
const parsed = JSON.parse(text)
|
|
40
|
+
if (!Array.isArray(parsed)) throw new UsageError(`--${name} 需要是数组`)
|
|
41
|
+
out.push(...parsed)
|
|
42
|
+
continue
|
|
43
|
+
}
|
|
44
|
+
out.push(...text.split(',').map((part) => part.trim()).filter(Boolean))
|
|
45
|
+
}
|
|
46
|
+
return spec.items === 'number' ? out.map((item) => Number(item)) : out
|
|
47
|
+
}
|
|
48
|
+
case 'json':
|
|
49
|
+
try {
|
|
50
|
+
return parseJsonArg(raw)
|
|
51
|
+
} catch (error) {
|
|
52
|
+
throw new UsageError(`--${name} 不是合法 JSON:${error.message}`)
|
|
53
|
+
}
|
|
54
|
+
case 'boolean':
|
|
55
|
+
return raw === true || raw === 'true'
|
|
56
|
+
case 'flag': {
|
|
57
|
+
// 值必须显式给出:flag 用字符串选项承接,--x false 才能真的传 false。
|
|
58
|
+
const text = String(raw).trim().toLowerCase()
|
|
59
|
+
if (text !== 'true' && text !== 'false') {
|
|
60
|
+
throw new UsageError(`--${name} 需要是 true 或 false`)
|
|
61
|
+
}
|
|
62
|
+
return text === 'true'
|
|
63
|
+
}
|
|
64
|
+
default:
|
|
65
|
+
return String(raw)
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/** 把 parseArgs 的值按参数定义转换,并套用默认值、校验必填。 */
|
|
70
|
+
export function buildParamValues(command, values) {
|
|
71
|
+
const params = {}
|
|
72
|
+
for (const [name, spec] of Object.entries(command.params || {})) {
|
|
73
|
+
const raw = values[name]
|
|
74
|
+
let value = raw === undefined ? undefined : coerce(name, spec, raw)
|
|
75
|
+
if (value === undefined && spec.default !== undefined) value = spec.default
|
|
76
|
+
if (value === undefined && spec.required) {
|
|
77
|
+
throw new UsageError(`缺少必填参数 --${name}`)
|
|
78
|
+
}
|
|
79
|
+
if (value !== undefined) params[name] = value
|
|
80
|
+
}
|
|
81
|
+
return params
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/** 组装 parseArgs 选项表:全局选项 + 命令参数。 */
|
|
85
|
+
export function buildOptions(command, globalOptions) {
|
|
86
|
+
const options = { ...globalOptions }
|
|
87
|
+
for (const [name, spec] of Object.entries(command.params || {})) {
|
|
88
|
+
const option = { type: spec.type === 'boolean' ? 'boolean' : 'string' }
|
|
89
|
+
if (spec.type === 'array') option.multiple = true
|
|
90
|
+
if (spec.alias) option.short = spec.alias
|
|
91
|
+
options[name] = option
|
|
92
|
+
}
|
|
93
|
+
return options
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
export function parseCommandArgs(command, args, globalOptions) {
|
|
97
|
+
try {
|
|
98
|
+
return parseArgs({
|
|
99
|
+
args,
|
|
100
|
+
options: buildOptions(command, globalOptions),
|
|
101
|
+
strict: true,
|
|
102
|
+
allowPositionals: true,
|
|
103
|
+
})
|
|
104
|
+
} catch (error) {
|
|
105
|
+
throw new UsageError(error.message)
|
|
106
|
+
}
|
|
107
|
+
}
|
package/src/auth.js
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import fs from 'node:fs'
|
|
2
|
+
import os from 'node:os'
|
|
3
|
+
import path from 'node:path'
|
|
4
|
+
|
|
5
|
+
/** 默认授权文件位置:~/.tmc/auth.json */
|
|
6
|
+
export function defaultAuthFile() {
|
|
7
|
+
return path.join(os.homedir(), '.tmc', 'auth.json')
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
/** 解析授权文件位置,优先级:--auth-file > TCM_AUTH_FILE > 默认位置 */
|
|
11
|
+
export function resolveAuthFile(override) {
|
|
12
|
+
return override || process.env.TCM_AUTH_FILE || defaultAuthFile()
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
/** 读取授权信息;文件不存在或损坏时返回 null。 */
|
|
16
|
+
export function readAuth(file) {
|
|
17
|
+
try {
|
|
18
|
+
const parsed = JSON.parse(fs.readFileSync(file, 'utf8'))
|
|
19
|
+
if (!parsed || typeof parsed !== 'object' || Array.isArray(parsed)) return null
|
|
20
|
+
return parsed
|
|
21
|
+
} catch {
|
|
22
|
+
return null
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/** 写入授权信息,目录 0700、文件 0600。 */
|
|
27
|
+
export function writeAuth(file, data) {
|
|
28
|
+
fs.mkdirSync(path.dirname(file), { recursive: true, mode: 0o700 })
|
|
29
|
+
fs.writeFileSync(file, `${JSON.stringify(data, null, 2)}\n`, { mode: 0o600 })
|
|
30
|
+
try {
|
|
31
|
+
fs.chmodSync(file, 0o600)
|
|
32
|
+
} catch {
|
|
33
|
+
// Windows 等平台可能不支持,忽略即可。
|
|
34
|
+
}
|
|
35
|
+
return file
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/** 删除授权文件。 */
|
|
39
|
+
export function removeAuth(file) {
|
|
40
|
+
try {
|
|
41
|
+
fs.rmSync(file, { force: true })
|
|
42
|
+
return true
|
|
43
|
+
} catch {
|
|
44
|
+
return false
|
|
45
|
+
}
|
|
46
|
+
}
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
import { pick, post, string, number, array, boolean, pageNum, pageSize, id } from './common.js'
|
|
2
|
+
|
|
3
|
+
export default {
|
|
4
|
+
summary: '账号',
|
|
5
|
+
commands: {
|
|
6
|
+
list: {
|
|
7
|
+
summary: '分页查询账号',
|
|
8
|
+
params: {
|
|
9
|
+
keyword: string('关键词'),
|
|
10
|
+
statuses: array('状态多选:NORMAL / WARMING_UP / HIDDEN'),
|
|
11
|
+
health: string('健康状态:ONLINE / OFFLINE'),
|
|
12
|
+
loginStatus: boolean('是否已登录'),
|
|
13
|
+
verified: boolean('是否已认证'),
|
|
14
|
+
machineId: number('机器 ID'),
|
|
15
|
+
tags: array('标签'),
|
|
16
|
+
pageNum: pageNum(1),
|
|
17
|
+
pageSize: pageSize(10),
|
|
18
|
+
},
|
|
19
|
+
build: (p) =>
|
|
20
|
+
post(
|
|
21
|
+
'/api/fleet/account/page',
|
|
22
|
+
pick(p, [
|
|
23
|
+
'keyword',
|
|
24
|
+
'statuses',
|
|
25
|
+
'health',
|
|
26
|
+
'loginStatus',
|
|
27
|
+
'verified',
|
|
28
|
+
'machineId',
|
|
29
|
+
'tags',
|
|
30
|
+
'pageNum',
|
|
31
|
+
'pageSize',
|
|
32
|
+
]),
|
|
33
|
+
),
|
|
34
|
+
},
|
|
35
|
+
get: {
|
|
36
|
+
summary: '账号详情',
|
|
37
|
+
params: { id: id('账号 ID') },
|
|
38
|
+
build: (p) => post('/api/fleet/account/detail', pick(p, ['id'])),
|
|
39
|
+
},
|
|
40
|
+
update: {
|
|
41
|
+
summary: '编辑账号(状态、描述、标签、任务能力)',
|
|
42
|
+
params: {
|
|
43
|
+
id: id('账号 ID'),
|
|
44
|
+
description: string('描述'),
|
|
45
|
+
tags: array('标签'),
|
|
46
|
+
status: string('状态:NORMAL / WARMING_UP / HIDDEN'),
|
|
47
|
+
capabilities: array(
|
|
48
|
+
'任务能力多选:AWEMES / ITEMS / COMMENT / COMMENT_INFO / COMMENT_DIGG / HOT_COMMENT / PUBLISH',
|
|
49
|
+
),
|
|
50
|
+
},
|
|
51
|
+
build: (p) =>
|
|
52
|
+
post(
|
|
53
|
+
'/api/fleet/account/update',
|
|
54
|
+
pick(p, ['id', 'description', 'tags', 'status', 'capabilities']),
|
|
55
|
+
),
|
|
56
|
+
},
|
|
57
|
+
status: {
|
|
58
|
+
summary: '切换账号状态',
|
|
59
|
+
params: {
|
|
60
|
+
id: id('账号 ID'),
|
|
61
|
+
status: string('状态:NORMAL / WARMING_UP / HIDDEN', { required: true }),
|
|
62
|
+
},
|
|
63
|
+
build: (p) => post('/api/fleet/account/status', pick(p, ['id', 'status'])),
|
|
64
|
+
},
|
|
65
|
+
'toggle-verify': {
|
|
66
|
+
summary: '切换账号认证状态',
|
|
67
|
+
params: { id: id('账号 ID') },
|
|
68
|
+
build: (p) => post('/api/fleet/account/toggleverify', pick(p, ['id'])),
|
|
69
|
+
},
|
|
70
|
+
brief: {
|
|
71
|
+
summary: '按 ID 批量查询账号简要信息',
|
|
72
|
+
params: { ids: array('账号 ID 列表', { items: 'number', required: true }) },
|
|
73
|
+
build: (p) => post('/api/fleet/account/brief', pick(p, ['ids'])),
|
|
74
|
+
},
|
|
75
|
+
summary: {
|
|
76
|
+
summary: '账号汇总',
|
|
77
|
+
params: {},
|
|
78
|
+
build: () => post('/api/fleet/account/summary'),
|
|
79
|
+
},
|
|
80
|
+
logs: {
|
|
81
|
+
summary: '账号日志分页',
|
|
82
|
+
params: {
|
|
83
|
+
accountId: number('账号 ID'),
|
|
84
|
+
type: string('日志类型,例如 CREATE / STATUS_SWITCH / ONLINE / OFFLINE / LOGIN / LOGOUT'),
|
|
85
|
+
pageNum: pageNum(1),
|
|
86
|
+
pageSize: pageSize(10),
|
|
87
|
+
},
|
|
88
|
+
build: (p) => post('/api/fleet/account/log/page', pick(p, ['accountId', 'type', 'pageNum', 'pageSize'])),
|
|
89
|
+
},
|
|
90
|
+
cookies: {
|
|
91
|
+
summary: '获取账号 Cookies',
|
|
92
|
+
params: { id: id('账号 ID') },
|
|
93
|
+
build: (p) => post('/api/fleet/account/cookies', pick(p, ['id'])),
|
|
94
|
+
},
|
|
95
|
+
},
|
|
96
|
+
}
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import { pick, post, string, number, pageNum, pageSize, id, appendFile } from './common.js'
|
|
2
|
+
|
|
3
|
+
export default {
|
|
4
|
+
summary: 'Agent 制品',
|
|
5
|
+
commands: {
|
|
6
|
+
list: {
|
|
7
|
+
summary: '分页查询 Agent 制品',
|
|
8
|
+
params: {
|
|
9
|
+
os: string('操作系统:MAC / WINDOWS / LINUX'),
|
|
10
|
+
keyword: string('关键词'),
|
|
11
|
+
pageNum: pageNum(1),
|
|
12
|
+
pageSize: pageSize(10),
|
|
13
|
+
},
|
|
14
|
+
build: (p) => post('/api/fleet/agent_artifact/page', pick(p, ['os', 'keyword', 'pageNum', 'pageSize'])),
|
|
15
|
+
},
|
|
16
|
+
create: {
|
|
17
|
+
summary: '上传 Agent 制品(multipart)',
|
|
18
|
+
params: {
|
|
19
|
+
file: string('本地文件路径', { required: true }),
|
|
20
|
+
description: string('描述'),
|
|
21
|
+
},
|
|
22
|
+
build: async (p) => {
|
|
23
|
+
const formData = new FormData()
|
|
24
|
+
await appendFile(formData, 'file', p.file)
|
|
25
|
+
if (p.description !== undefined) formData.append('description', p.description)
|
|
26
|
+
return { method: 'POST', path: '/api/fleet/agent_artifact/create', formData }
|
|
27
|
+
},
|
|
28
|
+
},
|
|
29
|
+
update: {
|
|
30
|
+
summary: '编辑 Agent 制品描述',
|
|
31
|
+
params: {
|
|
32
|
+
id: id('制品 ID'),
|
|
33
|
+
description: string('描述'),
|
|
34
|
+
},
|
|
35
|
+
build: (p) => post('/api/fleet/agent_artifact/update', pick(p, ['id', 'description'])),
|
|
36
|
+
},
|
|
37
|
+
activate: {
|
|
38
|
+
summary: '激活 Agent 制品',
|
|
39
|
+
params: { id: id('制品 ID') },
|
|
40
|
+
build: (p) => post('/api/fleet/agent_artifact/activate', pick(p, ['id'])),
|
|
41
|
+
},
|
|
42
|
+
statistics: {
|
|
43
|
+
summary: 'Agent 制品版本统计',
|
|
44
|
+
params: {},
|
|
45
|
+
build: () => post('/api/fleet/agent_artifact/statistics'),
|
|
46
|
+
},
|
|
47
|
+
},
|
|
48
|
+
}
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
import fs from 'node:fs'
|
|
2
|
+
import { readAuth, writeAuth, removeAuth } from '../auth.js'
|
|
3
|
+
import { ApiError } from '../http.js'
|
|
4
|
+
import { DEFAULT_SERVER } from '../config.js'
|
|
5
|
+
import { boolean } from './common.js'
|
|
6
|
+
|
|
7
|
+
function resolveServer(global, stored) {
|
|
8
|
+
const server = global.server || process.env.TCM_SERVER_URL || stored?.serverUrl || DEFAULT_SERVER
|
|
9
|
+
return String(server).replace(/\/+$/, '')
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
function authInfo(file, stored) {
|
|
13
|
+
return {
|
|
14
|
+
authFile: file,
|
|
15
|
+
serverUrl: stored?.serverUrl || null,
|
|
16
|
+
loggedIn: Boolean(stored?.apiKey),
|
|
17
|
+
user: stored?.user || null,
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export const topCommands = {
|
|
22
|
+
login: {
|
|
23
|
+
summary: '使用用户 Key 登录并保存到 ~/.tmc/auth.json',
|
|
24
|
+
auth: false,
|
|
25
|
+
params: {},
|
|
26
|
+
examples: [
|
|
27
|
+
'tcm login --key uk_xxx --server https://tcm.example.com',
|
|
28
|
+
'tcm login uk_xxx',
|
|
29
|
+
'TCM_SERVER_URL=https://tcm.example.com tcm login --key uk_xxx',
|
|
30
|
+
'echo uk_xxx | tcm login --key -',
|
|
31
|
+
],
|
|
32
|
+
async run({ positionals, global, request, resolveContext }) {
|
|
33
|
+
const ctx = resolveContext(global)
|
|
34
|
+
const stored = readAuth(ctx.file)
|
|
35
|
+
const serverUrl = resolveServer(global, stored)
|
|
36
|
+
let apiKey = global.key || process.env.TCM_API_KEY || positionals[0]
|
|
37
|
+
if (apiKey === '-') apiKey = fs.readFileSync(0, 'utf8').trim()
|
|
38
|
+
if (!apiKey) {
|
|
39
|
+
throw new ApiError('缺少用户 Key,请使用:tcm login --key <userKey>', { status: 400 })
|
|
40
|
+
}
|
|
41
|
+
const user = await request({ serverUrl, apiKey, method: 'GET', path: '/api/user/me' })
|
|
42
|
+
const file = writeAuth(ctx.file, {
|
|
43
|
+
serverUrl,
|
|
44
|
+
apiKey,
|
|
45
|
+
user,
|
|
46
|
+
createdAt: new Date().toISOString(),
|
|
47
|
+
})
|
|
48
|
+
return { loggedIn: true, authFile: file, serverUrl, user }
|
|
49
|
+
},
|
|
50
|
+
},
|
|
51
|
+
|
|
52
|
+
logout: {
|
|
53
|
+
summary: '退出登录并删除本地授权文件',
|
|
54
|
+
auth: false,
|
|
55
|
+
params: {},
|
|
56
|
+
async run({ global, resolveContext }) {
|
|
57
|
+
const ctx = resolveContext(global)
|
|
58
|
+
const existed = Boolean(readAuth(ctx.file))
|
|
59
|
+
removeAuth(ctx.file)
|
|
60
|
+
return { loggedIn: false, authFile: ctx.file, removed: existed }
|
|
61
|
+
},
|
|
62
|
+
},
|
|
63
|
+
|
|
64
|
+
status: {
|
|
65
|
+
summary: '查看当前登录状态',
|
|
66
|
+
auth: false,
|
|
67
|
+
params: {
|
|
68
|
+
local: boolean('只读取本地授权文件,不请求服务端'),
|
|
69
|
+
},
|
|
70
|
+
async run({ values, global, request, resolveContext }) {
|
|
71
|
+
const ctx = resolveContext(global)
|
|
72
|
+
const stored = readAuth(ctx.file)
|
|
73
|
+
if (!ctx.apiKey) {
|
|
74
|
+
return authInfo(ctx.file, stored)
|
|
75
|
+
}
|
|
76
|
+
if (values.local) {
|
|
77
|
+
return authInfo(ctx.file, { ...stored, serverUrl: ctx.serverUrl, apiKey: ctx.apiKey })
|
|
78
|
+
}
|
|
79
|
+
const user = await request({
|
|
80
|
+
serverUrl: ctx.serverUrl,
|
|
81
|
+
apiKey: ctx.apiKey,
|
|
82
|
+
method: 'GET',
|
|
83
|
+
path: '/api/user/me',
|
|
84
|
+
})
|
|
85
|
+
return {
|
|
86
|
+
loggedIn: true,
|
|
87
|
+
authFile: ctx.file,
|
|
88
|
+
serverUrl: ctx.serverUrl,
|
|
89
|
+
user,
|
|
90
|
+
}
|
|
91
|
+
},
|
|
92
|
+
},
|
|
93
|
+
}
|