@rainyun/cli 0.0.0-stage → 1.0.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.
- package/README.md +176 -2
- package/bin/postinstall.mjs +13 -0
- package/bin/rainyun-cli.mjs +13 -0
- package/catalog/human-names.json +340 -0
- package/catalog/list.openapi.json +354 -0
- package/catalog/openapi.json +34527 -0
- package/catalog/product-commands.json +332 -0
- package/catalog/public.openapi.json +1891 -0
- package/docs/AGENT_GUIDE.md +64 -0
- package/docs/install.md +44 -0
- package/docs/product-lists.md +48 -0
- package/docs/rgp.md +77 -0
- package/docs/terminal-exec.md +111 -0
- package/examples/public-panel.json +1 -0
- package/examples/public-rcs-price.json +1 -0
- package/examples/rca-terminal-input.json +1 -0
- package/examples/rcs-get.json +1 -0
- package/examples/rcs-list.json +1 -0
- package/examples/rcs-reboot.json +1 -0
- package/examples/terminal-controls.ndjson +3 -0
- package/examples/terminal-input.json +1 -0
- package/node_modules/ws/LICENSE +20 -0
- package/node_modules/ws/README.md +539 -0
- package/node_modules/ws/browser.js +8 -0
- package/node_modules/ws/index.js +22 -0
- package/node_modules/ws/lib/buffer-util.js +142 -0
- package/node_modules/ws/lib/constants.js +19 -0
- package/node_modules/ws/lib/event-target.js +292 -0
- package/node_modules/ws/lib/extension.js +203 -0
- package/node_modules/ws/lib/limiter.js +55 -0
- package/node_modules/ws/lib/permessage-deflate.js +530 -0
- package/node_modules/ws/lib/receiver.js +751 -0
- package/node_modules/ws/lib/sender.js +610 -0
- package/node_modules/ws/lib/stream.js +161 -0
- package/node_modules/ws/lib/subprotocol.js +62 -0
- package/node_modules/ws/lib/validation.js +152 -0
- package/node_modules/ws/lib/websocket-server.js +565 -0
- package/node_modules/ws/lib/websocket.js +1471 -0
- package/node_modules/ws/package.json +74 -0
- package/node_modules/ws/wrapper.mjs +21 -0
- package/package.json +29 -4
- package/skills/rainyun-cli/SKILL.md +46 -0
- package/src/api/catalog.mjs +230 -0
- package/src/api/http.mjs +222 -0
- package/src/api/request.mjs +178 -0
- package/src/api/titles.mjs +9 -0
- package/src/auth/commands.mjs +192 -0
- package/src/auth/store.mjs +162 -0
- package/src/cli.mjs +154 -0
- package/src/commands/dispatch.mjs +128 -0
- package/src/commands/fields.mjs +77 -0
- package/src/commands/products.mjs +209 -0
- package/src/commands/rgs.mjs +44 -0
- package/src/json.mjs +81 -0
- package/src/output/business.mjs +188 -0
- package/src/output/discovery.mjs +197 -0
- package/src/output/examples.mjs +71 -0
- package/src/output/format.mjs +164 -0
- package/src/output/help.json +181 -0
- package/src/output/help.mjs +334 -0
- package/src/rgp/commands.mjs +251 -0
- package/src/rgp/input.mjs +144 -0
- package/src/rgp/operations.json +625 -0
- package/src/rgp/result.mjs +103 -0
- package/src/rgp/transfer.mjs +213 -0
- package/src/terminal/commands.mjs +155 -0
- package/src/terminal/session.mjs +233 -0
- package/src/terminal/target.mjs +155 -0
|
@@ -0,0 +1,188 @@
|
|
|
1
|
+
import { lookup } from '../api/catalog.mjs';
|
|
2
|
+
import { productNode } from '../commands/products.mjs';
|
|
3
|
+
|
|
4
|
+
const resources = [
|
|
5
|
+
['ID', ['ID', 'id']],
|
|
6
|
+
['名称', ['Name', 'name', 'install_name', 'ServerName', 'server_name', 'BucketName', 'bucket_name']],
|
|
7
|
+
['状态', ['Status', 'status', 'State', 'state']],
|
|
8
|
+
['标签', ['Tag', 'tag']],
|
|
9
|
+
['IPv4', ['MainIPv4', 'main_ipv4']],
|
|
10
|
+
['备注', ['Remark', 'remark', 'Comment', 'comment']],
|
|
11
|
+
];
|
|
12
|
+
const taskFields = [
|
|
13
|
+
['任务 ID', ['id']],
|
|
14
|
+
['状态', ['status']],
|
|
15
|
+
['名称', ['name']],
|
|
16
|
+
['类型', ['type']],
|
|
17
|
+
];
|
|
18
|
+
const fileFields = [
|
|
19
|
+
['名称', ['name', 'Name', 'file_name']],
|
|
20
|
+
['类型', ['type', 'Type']],
|
|
21
|
+
['字节', ['size', 'Size']],
|
|
22
|
+
];
|
|
23
|
+
const backupFields = [
|
|
24
|
+
['标签', ['tag']],
|
|
25
|
+
['状态', ['status']],
|
|
26
|
+
['创建时间', ['created_at', 'create_time']],
|
|
27
|
+
];
|
|
28
|
+
const cronFields = [
|
|
29
|
+
['ID', ['id']],
|
|
30
|
+
['名称', ['name']],
|
|
31
|
+
['表达式', ['cron_expression']],
|
|
32
|
+
['启用', ['is_enable']],
|
|
33
|
+
];
|
|
34
|
+
const jsonHint = '完整字段可用 --json 查看。';
|
|
35
|
+
|
|
36
|
+
function scalar(row, keys) {
|
|
37
|
+
for (const key of keys) {
|
|
38
|
+
const value = row?.[key];
|
|
39
|
+
if (['string', 'number', 'boolean', 'bigint'].includes(typeof value) || JSON.isRawJSON(value)) {
|
|
40
|
+
return JSON.isRawJSON(value) ? value.rawJSON : String(value);
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
function cell(value) {
|
|
46
|
+
const chars = Array.from(value.replace(/[\u0000-\u001f\u007f-\u009f]/g, ' '));
|
|
47
|
+
return chars.length > 40 ? chars.slice(0, 39).join('') + '…' : chars.join('');
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
function width(value) {
|
|
51
|
+
return Array.from(value).reduce(
|
|
52
|
+
(n, c) =>
|
|
53
|
+
n +
|
|
54
|
+
(/[\p{Script=Han}\p{Script=Hiragana}\p{Script=Katakana}\p{Script=Hangul}\p{Extended_Pictographic}]/u.test(
|
|
55
|
+
c
|
|
56
|
+
)
|
|
57
|
+
? 2
|
|
58
|
+
: 1),
|
|
59
|
+
0
|
|
60
|
+
);
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
// Only include columns actually present. When any row has no usable fields,
|
|
64
|
+
// keep the generic view rather than silently representing that row as blanks.
|
|
65
|
+
function table(records, fields) {
|
|
66
|
+
const selected = records.slice(0, 30);
|
|
67
|
+
const columns = fields.filter(([, keys]) => selected.some((row) => scalar(row, keys) !== undefined));
|
|
68
|
+
if (
|
|
69
|
+
!columns.length ||
|
|
70
|
+
selected.some((row) => columns.every(([, keys]) => scalar(row, keys) === undefined))
|
|
71
|
+
)
|
|
72
|
+
return;
|
|
73
|
+
const rows = [
|
|
74
|
+
columns.map(([label]) => label),
|
|
75
|
+
...selected.map((row) => columns.map(([, keys]) => cell(scalar(row, keys) ?? '—'))),
|
|
76
|
+
];
|
|
77
|
+
const widths = columns.map((_, i) => Math.max(...rows.map((row) => width(row[i]))));
|
|
78
|
+
const lines = rows.map((row) =>
|
|
79
|
+
row
|
|
80
|
+
.map((v, i) => v + ' '.repeat(widths[i] - width(v)))
|
|
81
|
+
.join(' ')
|
|
82
|
+
.trimEnd()
|
|
83
|
+
);
|
|
84
|
+
if (records.length > selected.length)
|
|
85
|
+
lines.push('还有 ' + (records.length - selected.length) + ' 条,完整列表可用 --json 查看。');
|
|
86
|
+
return lines.join('\n');
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
function showData(value, ctx, summarize = true) {
|
|
90
|
+
if (value === undefined || value === null) return '无返回数据';
|
|
91
|
+
if (typeof value === 'string') return value || '(空字符串)';
|
|
92
|
+
const text = ctx.jsonStringify(value, 2);
|
|
93
|
+
if (!summarize) return text;
|
|
94
|
+
const lines = text.split('\n', 61);
|
|
95
|
+
if (lines.length > 60 || text.length > 8000) {
|
|
96
|
+
return lines.slice(0, 60).join('\n').slice(0, 8000) + '\n… 内容较长,完整数据可用 --json 查看。';
|
|
97
|
+
}
|
|
98
|
+
return text;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
function recordsPage(args, data, flags, ctx) {
|
|
102
|
+
const records = data.Records;
|
|
103
|
+
const typed = productNode(args);
|
|
104
|
+
const shortcut =
|
|
105
|
+
!['input', 'query', 'options'].some((name) => Object.hasOwn(flags, name)) &&
|
|
106
|
+
(typed?.paged ?? (['rcs', 'rgs', 'ros', 'rca', 'rbm'].includes(args[0]) && args.at(-1) === 'list'));
|
|
107
|
+
const page = shortcut ? Number(flags.page ?? 1) : null;
|
|
108
|
+
const perPage = Number(flags['per-page'] ?? 20);
|
|
109
|
+
const total = scalar(data, ['TotalRecords']);
|
|
110
|
+
const lines = [
|
|
111
|
+
(page ? '第 ' + page + ' 页 · ' : '') +
|
|
112
|
+
'本页 ' +
|
|
113
|
+
records.length +
|
|
114
|
+
' 条' +
|
|
115
|
+
(total !== undefined ? ' · 共 ' + total + ' 条' : ''),
|
|
116
|
+
];
|
|
117
|
+
if (records.length === 0) lines.push('本页没有记录。');
|
|
118
|
+
else {
|
|
119
|
+
const summary = table(records, resources);
|
|
120
|
+
lines.push('', summary ?? showData(records, ctx));
|
|
121
|
+
if (summary) lines.push('', jsonHint);
|
|
122
|
+
}
|
|
123
|
+
if (shortcut && total && /^\d+$/.test(total) && BigInt(page) * BigInt(perPage) < BigInt(total)) {
|
|
124
|
+
const project = flags['project-id'] ? ' --project-id ' + flags['project-id'] : '';
|
|
125
|
+
const canReplay = Object.keys(flags).every((flag) =>
|
|
126
|
+
['page', 'per-page', 'project-id'].includes(flag)
|
|
127
|
+
);
|
|
128
|
+
if (canReplay)
|
|
129
|
+
lines.push(
|
|
130
|
+
'下一页 rainyun-cli ' + args.join(' ') + project + ' --page ' + (page + 1) + ' --per-page ' + perPage
|
|
131
|
+
);
|
|
132
|
+
else lines.push('下一页 使用相同参数,并将 --page 改为 ' + (page + 1));
|
|
133
|
+
}
|
|
134
|
+
return lines.join('\n');
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
const outcomes = {
|
|
138
|
+
completed: '已完成',
|
|
139
|
+
accepted: '已受理',
|
|
140
|
+
accepted_unknown: '已受理,任务编号未知',
|
|
141
|
+
succeeded: '任务已完成',
|
|
142
|
+
};
|
|
143
|
+
function rgpView(result, ctx) {
|
|
144
|
+
const op = result.operation;
|
|
145
|
+
const data = result.response?.data;
|
|
146
|
+
const lines = ['RGP ' + op];
|
|
147
|
+
if (result.outcome)
|
|
148
|
+
lines.push(
|
|
149
|
+
'结果 ' + (Object.hasOwn(outcomes, result.outcome) ? outcomes[result.outcome] : result.outcome)
|
|
150
|
+
);
|
|
151
|
+
if (result.task_id !== undefined) lines.push('任务 ID ' + result.task_id);
|
|
152
|
+
if (result.local_path) lines.push('保存位置 ' + result.local_path);
|
|
153
|
+
if (result.sha256)
|
|
154
|
+
lines.push('文件大小 ' + (result.bytes ?? 0) + ' 字节', 'SHA-256 ' + result.sha256);
|
|
155
|
+
if (result.diagnostics?.length) lines.push('', ...result.diagnostics);
|
|
156
|
+
if (result.response) {
|
|
157
|
+
if (['config.read', 'file.read'].includes(op) && typeof data?.data === 'string') {
|
|
158
|
+
lines.push('', data.data || '(空文件)');
|
|
159
|
+
} else if (Array.isArray(data) && data.length === 0) {
|
|
160
|
+
lines.push('没有记录。');
|
|
161
|
+
} else {
|
|
162
|
+
const fields = {
|
|
163
|
+
'task.list': taskFields,
|
|
164
|
+
'file.list': fileFields,
|
|
165
|
+
'backup.list': backupFields,
|
|
166
|
+
'cron.list': cronFields,
|
|
167
|
+
}[op];
|
|
168
|
+
const summary = fields && Array.isArray(data) ? table(data, fields) : undefined;
|
|
169
|
+
lines.push('', summary ?? showData(data, ctx, false));
|
|
170
|
+
if (summary) lines.push('', jsonHint);
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
return lines.join('\n');
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
export function businessView(args, result, flags, ctx) {
|
|
177
|
+
if (result?.dry_run || !result?.ok) return;
|
|
178
|
+
if (args[0] === 'rgp' && args[1] === 'call') return rgpView(result, ctx);
|
|
179
|
+
if (!result.response || result.http_status === undefined) return;
|
|
180
|
+
const op = lookup(result.operation);
|
|
181
|
+
const title = (op.summary || op.operation_id) + ' · HTTP ' + result.http_status;
|
|
182
|
+
const data = result.response.data;
|
|
183
|
+
const body =
|
|
184
|
+
op.method === 'GET' && Array.isArray(data?.Records)
|
|
185
|
+
? recordsPage(args, data, flags, ctx)
|
|
186
|
+
: showData(data, ctx, op.method === 'GET');
|
|
187
|
+
return [title, '', body].join('\n');
|
|
188
|
+
}
|
|
@@ -0,0 +1,197 @@
|
|
|
1
|
+
import { previewCommand } from './examples.mjs';
|
|
2
|
+
import { titleOf } from '../api/titles.mjs';
|
|
3
|
+
import { productName } from '../commands/products.mjs';
|
|
4
|
+
import { lookup, resolve } from '../api/catalog.mjs';
|
|
5
|
+
|
|
6
|
+
// Only produce a copyable command when every dynamic argument is a shell-safe
|
|
7
|
+
// word. Quoting rules differ between Windows PowerShell, cmd and POSIX shells.
|
|
8
|
+
function nextPage(prefix, flags, offset) {
|
|
9
|
+
const args = [prefix + ' commands'];
|
|
10
|
+
if (flags.public) args.push('--public');
|
|
11
|
+
for (const name of ['query', 'scope', 'limit']) {
|
|
12
|
+
if (!Object.hasOwn(flags, name)) continue;
|
|
13
|
+
if (!/^[\p{L}\p{N}_.:-]+$/u.test(flags[name]) || String(flags[name]).startsWith('-')) {
|
|
14
|
+
return '保留本次筛选和每页条数,将 --offset 设为 ' + offset;
|
|
15
|
+
}
|
|
16
|
+
args.push('--' + name, String(flags[name]));
|
|
17
|
+
}
|
|
18
|
+
return [...args, '--offset', offset].join(' ');
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
function schemaCommand(op, panel) {
|
|
22
|
+
if (panel) return 'rainyun-cli rgp schema ' + op.operation_id;
|
|
23
|
+
return 'rainyun-cli schema ' + (productName(op.operation_id) ?? op.aliases?.[0] ?? op.operation_id);
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
function listing(data, flags, panel) {
|
|
27
|
+
const prefix = panel ? 'rainyun-cli rgp' : 'rainyun-cli';
|
|
28
|
+
const title = panel ? '游戏面板操作' : '雨云 API';
|
|
29
|
+
const filters = [
|
|
30
|
+
flags.query && '关键词「' + flags.query + '」',
|
|
31
|
+
flags.scope,
|
|
32
|
+
flags.public && '免登录',
|
|
33
|
+
].filter(Boolean);
|
|
34
|
+
const lines = [title + (filters.length ? ' · ' + filters.join(' · ') : '')];
|
|
35
|
+
if (!data.items.length) {
|
|
36
|
+
if (!data.total)
|
|
37
|
+
return lines.concat('', '没有找到匹配的接口。可以缩短关键词或移除筛选条件。').join('\n');
|
|
38
|
+
return lines
|
|
39
|
+
.concat('', '起始位置超出结果范围,共 ' + data.total + ' 项。', nextPage(prefix, flags, 0))
|
|
40
|
+
.join('\n');
|
|
41
|
+
}
|
|
42
|
+
lines.push(
|
|
43
|
+
'第 ' +
|
|
44
|
+
(data.offset + 1) +
|
|
45
|
+
'–' +
|
|
46
|
+
(data.offset + data.items.length) +
|
|
47
|
+
' 项,共 ' +
|
|
48
|
+
data.total +
|
|
49
|
+
' 项',
|
|
50
|
+
''
|
|
51
|
+
);
|
|
52
|
+
if (!panel) lines.push('以下命令使用示例参数预览请求。执行前请换成实际目标。', '');
|
|
53
|
+
for (const item of data.items) {
|
|
54
|
+
const op = panel ? item : lookup(item.operation_id);
|
|
55
|
+
const access = panel ? '' : op.authentication === 'none' ? ' · 免登录' : ' · API Key';
|
|
56
|
+
const writeLabel = item.is_write ? ' · 写操作' : '';
|
|
57
|
+
lines.push(
|
|
58
|
+
op.summary + access + writeLabel,
|
|
59
|
+
' ' + (panel ? schemaCommand(op, true) : previewCommand(op)),
|
|
60
|
+
''
|
|
61
|
+
);
|
|
62
|
+
}
|
|
63
|
+
if (!panel) lines.push('查看参数 ' + schemaCommand(data.items[0], false));
|
|
64
|
+
else lines.push('先查操作所需参数,再用 rainyun-cli rgp call 实例ID 操作名 调用。');
|
|
65
|
+
if (data.has_more) lines.push('下一页 ' + nextPage(prefix, flags, data.next_offset));
|
|
66
|
+
return lines.join('\n');
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
function shapeName(schema) {
|
|
70
|
+
if (!schema) return '任意 JSON';
|
|
71
|
+
if (schema.format === 'binary') return '文件';
|
|
72
|
+
if (schema.type === 'array')
|
|
73
|
+
return '数组' + (schema.items?.type ? '(' + shapeName(schema.items) + ')' : '');
|
|
74
|
+
return (
|
|
75
|
+
{ integer: '整数', number: '数字', string: '文本', boolean: '布尔值', object: '对象' }[
|
|
76
|
+
schema.type
|
|
77
|
+
] ?? 'JSON'
|
|
78
|
+
);
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
function parameter(lines, path, schema, requirement, description, ctx) {
|
|
82
|
+
const detail = [shapeName(schema), requirement];
|
|
83
|
+
if (schema?.enum) detail.push('可选值 ' + schema.enum.map((v) => ctx.jsonStringify(v)).join('、'));
|
|
84
|
+
if (schema?.default !== undefined) detail.push('默认 ' + ctx.jsonStringify(schema.default));
|
|
85
|
+
lines.push(' ' + path + ' · ' + detail.filter(Boolean).join(' · '));
|
|
86
|
+
if (description) lines.push(' ' + description.trim());
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
function schemaView(result, panel, flags, ctx) {
|
|
90
|
+
const op = panel ? result.data.operation : result.operation;
|
|
91
|
+
const components = result.components ?? {};
|
|
92
|
+
const scopes = panel ? [result.data.metadata_scope] : op.scopes ?? [];
|
|
93
|
+
const access =
|
|
94
|
+
op.authentication === 'none'
|
|
95
|
+
? '免登录' + (op.api_key_enabled ? ',可用 --authenticated 选择账号身份' : '')
|
|
96
|
+
: 'API Key';
|
|
97
|
+
const lines = [
|
|
98
|
+
op.summary,
|
|
99
|
+
op.method + ' ' + op.path,
|
|
100
|
+
'',
|
|
101
|
+
panel ? '操作 ' + op.operation_id : '操作 ' + titleOf(op),
|
|
102
|
+
];
|
|
103
|
+
if (!panel) lines.push('预览示例 ' + previewCommand(op));
|
|
104
|
+
if (op.aliases?.length) lines.push('别名 ' + op.aliases.join('、'));
|
|
105
|
+
lines.push('认证 ' + access + (scopes.length ? ' · ' + scopes.join(' + ') : ''));
|
|
106
|
+
if (result.is_write || op.is_write) lines.push('执行 写操作,默认提交请求。加 --dry-run 可离线预览。');
|
|
107
|
+
const description = op.description
|
|
108
|
+
?.split('\n\n')
|
|
109
|
+
.filter((p) => !/^新版 API Key 所需 Scope(全部满足):[a-z:_.,,。 -]+$/i.test(p))
|
|
110
|
+
.join('\n\n');
|
|
111
|
+
if (description) lines.push('', description);
|
|
112
|
+
const fields = panel ? op.fields : op.parameters;
|
|
113
|
+
if (fields?.length) {
|
|
114
|
+
lines.push('', '参数');
|
|
115
|
+
for (const field of fields) {
|
|
116
|
+
const path = (field.in === 'header' ? 'headers' : field.in) + '.' + field.name;
|
|
117
|
+
parameter(
|
|
118
|
+
lines,
|
|
119
|
+
path,
|
|
120
|
+
resolve(field.schema ?? field, components),
|
|
121
|
+
field.required ? '必填' : '可选',
|
|
122
|
+
field.description,
|
|
123
|
+
ctx
|
|
124
|
+
);
|
|
125
|
+
const embedded = resolve(field['x-json-content-schema'], components);
|
|
126
|
+
if (embedded?.example !== undefined)
|
|
127
|
+
lines.push(' 示例 ' + ctx.jsonStringify(embedded.example));
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
const body = resolve(op.request_body, components);
|
|
131
|
+
for (const [mime, media] of Object.entries(body?.content ?? {})) {
|
|
132
|
+
const shape = resolve(media.schema, components);
|
|
133
|
+
lines.push('', '请求体 ' + mime + (body.required ? ' · 必填' : ''));
|
|
134
|
+
if (body.description) lines.push(' ' + body.description);
|
|
135
|
+
for (const [key, def] of Object.entries(shape?.properties ?? {})) {
|
|
136
|
+
const s = resolve(def, components);
|
|
137
|
+
if (mime === 'multipart/form-data' && s.format === 'binary') {
|
|
138
|
+
lines.push(' --file ' + key + '=本地路径');
|
|
139
|
+
} else {
|
|
140
|
+
parameter(
|
|
141
|
+
lines,
|
|
142
|
+
'body.' + key,
|
|
143
|
+
s,
|
|
144
|
+
shape.required?.includes(key) ? '文档标为必填' : '',
|
|
145
|
+
s.description,
|
|
146
|
+
ctx
|
|
147
|
+
);
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
const example = media.example ?? shape?.example;
|
|
151
|
+
if (example !== undefined) lines.push('', '请求体示例', ctx.jsonStringify(example, 2));
|
|
152
|
+
}
|
|
153
|
+
if (op.transfer === 'upload') lines.push(' --source FILE 选择本地上传文件');
|
|
154
|
+
if (op.transfer === 'download') lines.push(' --output FILE 保存到新的本地文件');
|
|
155
|
+
if (flags.responses && op.responses) {
|
|
156
|
+
lines.push('', '响应');
|
|
157
|
+
for (const [status, def] of Object.entries(op.responses)) {
|
|
158
|
+
const response = resolve(def, components);
|
|
159
|
+
lines.push(' ' + status + ' ' + response.description);
|
|
160
|
+
if (status.startsWith('2')) {
|
|
161
|
+
const envelope = resolve(response.content?.['application/json']?.schema, components);
|
|
162
|
+
const variants = [...(envelope?.allOf ?? []), envelope];
|
|
163
|
+
const properties = Object.assign({}, ...variants.map((v) => resolve(v, components)?.properties));
|
|
164
|
+
const data = resolve(properties.data, components);
|
|
165
|
+
if (data) {
|
|
166
|
+
lines.push(
|
|
167
|
+
' data · ' + shapeName(data) + (data.description ? ' · ' + data.description : '')
|
|
168
|
+
);
|
|
169
|
+
const entries = Object.entries(data.properties ?? {});
|
|
170
|
+
for (const [name, def] of entries.slice(0, 8)) {
|
|
171
|
+
const child = resolve(def, components);
|
|
172
|
+
lines.push(
|
|
173
|
+
' data.' +
|
|
174
|
+
name +
|
|
175
|
+
' · ' +
|
|
176
|
+
shapeName(child) +
|
|
177
|
+
(child.description ? ' · ' + child.description : '')
|
|
178
|
+
);
|
|
179
|
+
}
|
|
180
|
+
if (entries.length > 8) lines.push(' 其余 ' + (entries.length - 8) + ' 个字段见完整定义。');
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
lines.push(
|
|
186
|
+
'',
|
|
187
|
+
'完整定义 ' + schemaCommand(op, panel) + (flags.responses ? ' --responses' : '') + ' --json'
|
|
188
|
+
);
|
|
189
|
+
return lines.join('\n');
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
export function discoveryView(args, result, flags, ctx) {
|
|
193
|
+
const panel = args[0] === 'rgp';
|
|
194
|
+
const action = panel ? args[1] : args[0];
|
|
195
|
+
if (action === 'commands') return listing(result.data, flags, panel);
|
|
196
|
+
if (action === 'schema') return schemaView(result, panel, flags, ctx);
|
|
197
|
+
}
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
import { resolve } from '../api/catalog.mjs';
|
|
2
|
+
import { productName, productNode } from '../commands/products.mjs';
|
|
3
|
+
|
|
4
|
+
// Generated words are data from the bundled catalog. Never interpolate text
|
|
5
|
+
// containing shell expansion syntax into a command users are invited to paste.
|
|
6
|
+
function quoteWord(value) {
|
|
7
|
+
const word = String(value);
|
|
8
|
+
if (/^[\p{L}\p{N}_.:=/-]+$/u.test(word)) return word;
|
|
9
|
+
if (/["'“”‘’`$!%\\\p{Cc}]/u.test(word)) throw new Error('Unsafe preview argument');
|
|
10
|
+
return '"' + word + '"';
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
function sample(name, schema) {
|
|
14
|
+
if (schema.allOf?.length === 1 && !schema.type) return sample(name, resolve(schema.allOf[0]));
|
|
15
|
+
if (schema.enum?.length) return schema.enum[0];
|
|
16
|
+
if (schema.example !== undefined && typeof schema.example !== 'object') return schema.example;
|
|
17
|
+
if (schema.default !== undefined && typeof schema.default !== 'object') return schema.default;
|
|
18
|
+
if (schema.type === 'integer' || schema.type === 'number') {
|
|
19
|
+
if (name === 'limit') return 10;
|
|
20
|
+
if (name === 'end_time' || name === 'end_date') return 1700003600;
|
|
21
|
+
if (name === 'start_time' || name === 'start_date') return 1700000000;
|
|
22
|
+
return schema.minimum ?? (/^(?:id|.*_id)$/.test(name) ? 123 : 1);
|
|
23
|
+
}
|
|
24
|
+
if (schema.type === 'boolean') return true;
|
|
25
|
+
if (schema.type === 'object' || name === 'options') return '{}';
|
|
26
|
+
if (schema.type === 'array' || name === 'paths') return '[]';
|
|
27
|
+
if (schema.pattern === '^[0-9a-fA-F]{24}$') return '0123456789abcdef01234567';
|
|
28
|
+
if (/domain|host_name/.test(name)) return 'example.com';
|
|
29
|
+
if (name === 'ip') return '192.0.2.1';
|
|
30
|
+
if (name === 'product_type') return 'rcs';
|
|
31
|
+
if (name === 'file_name') return 'example.txt';
|
|
32
|
+
return 'example';
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
// Inline JSON containing quotes is parsed differently by POSIX shells, cmd
|
|
36
|
+
// and Windows PowerShell. Scalar field arguments stay portable through npm shims.
|
|
37
|
+
export function previewCommand(op) {
|
|
38
|
+
const command = productName(op.operation_id);
|
|
39
|
+
const node = command && productNode(command.split('.'));
|
|
40
|
+
const body = resolve(op.request_body);
|
|
41
|
+
const media = body?.content['application/json'];
|
|
42
|
+
const shape = resolve(media?.schema);
|
|
43
|
+
const example = media?.example ?? shape?.example;
|
|
44
|
+
const keys = example ? Object.keys(example) : shape?.required ?? [];
|
|
45
|
+
const typed = node && !keys.length;
|
|
46
|
+
const words = typed
|
|
47
|
+
? ['rainyun-cli', ...command.split('.')]
|
|
48
|
+
: ['rainyun-cli', 'call', command ?? op.aliases?.[0] ?? op.operation_id];
|
|
49
|
+
for (const p of op.parameters ?? []) {
|
|
50
|
+
if (!p.required && p.in !== 'path') continue;
|
|
51
|
+
const section = p.in === 'header' ? 'headers' : p.in;
|
|
52
|
+
const field = section + '.' + p.name;
|
|
53
|
+
const value = sample(p.name, resolve(p.schema));
|
|
54
|
+
if (typed) {
|
|
55
|
+
// Product list commands supply their own pagination defaults.
|
|
56
|
+
if (node.paged && p.in === 'query' && p.name === 'options') continue;
|
|
57
|
+
const [flag] = [...node.fields].find(([, spec]) => spec.field === field);
|
|
58
|
+
words.push('--' + flag, quoteWord(value));
|
|
59
|
+
} else {
|
|
60
|
+
words.push(quoteWord(field + '=' + value));
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
for (const key of keys) {
|
|
64
|
+
const value = example?.[key] ?? sample(key, resolve(shape.properties[key]));
|
|
65
|
+
words.push(quoteWord('body.' + key + '=' + value));
|
|
66
|
+
}
|
|
67
|
+
if (body?.required && media && !keys.length) words.push('--body', '"{}"');
|
|
68
|
+
if (body?.content['multipart/form-data']) words.push('--file', 'image=example.jpg');
|
|
69
|
+
words.push('--dry-run');
|
|
70
|
+
return words.join(' ');
|
|
71
|
+
}
|
|
@@ -0,0 +1,164 @@
|
|
|
1
|
+
import { catalog } from '../api/catalog.mjs';
|
|
2
|
+
import { titleOf } from '../api/titles.mjs';
|
|
3
|
+
import { discoveryView } from './discovery.mjs';
|
|
4
|
+
import { businessView } from './business.mjs';
|
|
5
|
+
|
|
6
|
+
const sources = { local: '本机凭据', environment: '环境变量 RAINYUN_API_KEY', none: '未配置' };
|
|
7
|
+
const storage = { 'config-file': '明文配置文件', 'windows-dpapi': 'Windows 加密凭据' };
|
|
8
|
+
|
|
9
|
+
// Human-readable output can include text from remote servers or local input.
|
|
10
|
+
// Render control characters visibly instead of applying terminal escape codes.
|
|
11
|
+
export function safeDisplay(text) {
|
|
12
|
+
return String(text)
|
|
13
|
+
.replaceAll('\r\n', '\n')
|
|
14
|
+
.replace(
|
|
15
|
+
/[\u0000-\u0009\u000b-\u001f\u007f-\u009f\u202a-\u202e\u2066-\u2069]/gu,
|
|
16
|
+
(c) => '\\u' + c.codePointAt(0).toString(16).padStart(4, '0')
|
|
17
|
+
);
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
function authView(action, data) {
|
|
21
|
+
const lines = [];
|
|
22
|
+
if (action === 'login') lines.push('API Key 已保存到当前用户的明文配置文件。');
|
|
23
|
+
if (action === 'logout')
|
|
24
|
+
lines.push(data.removed ? '已清除本机账号凭据。' : '本机没有已保存的账号凭据。');
|
|
25
|
+
if (action === 'status') lines.push(data.verified ? 'API Key 已通过服务端验证' : '尚未配置 API Key');
|
|
26
|
+
if (data.source !== 'none') lines.push('当前来源 ' + (sources[data.source] ?? data.source));
|
|
27
|
+
if (action === 'status' && storage[data.storage]) lines.push('保存方式 ' + storage[data.storage]);
|
|
28
|
+
if (data.environment_format_valid === false)
|
|
29
|
+
lines.push('环境变量中的密钥格式无效,请修正或清除该变量。');
|
|
30
|
+
else if (action === 'status' && data.verified) {
|
|
31
|
+
lines.push('用户 ID ' + data.user.id);
|
|
32
|
+
if (data.user.name) lines.push('用户名 ' + data.user.name.replaceAll('\n', '\\n'));
|
|
33
|
+
} else if (action === 'status') lines.push('在自己的终端运行 rainyun-cli auth login 配置密钥。');
|
|
34
|
+
return lines.join('\n');
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
function terminalView(data) {
|
|
38
|
+
const target =
|
|
39
|
+
data.product.toUpperCase() +
|
|
40
|
+
' ' +
|
|
41
|
+
data.id +
|
|
42
|
+
(data.container_id ? ' / 容器 ' + data.container_id : '');
|
|
43
|
+
if ('configured' in data)
|
|
44
|
+
return (
|
|
45
|
+
target +
|
|
46
|
+
'\n' +
|
|
47
|
+
(data.saved
|
|
48
|
+
? '已导入此容器的连接资料。'
|
|
49
|
+
: data.configured
|
|
50
|
+
? '已配置连接资料,尚未联网验证。'
|
|
51
|
+
: '未配置连接资料。')
|
|
52
|
+
);
|
|
53
|
+
if ('removed' in data)
|
|
54
|
+
return (
|
|
55
|
+
target + '\n' + (data.removed ? '已移除此容器的本机连接资料。' : '本机没有此容器的连接资料。')
|
|
56
|
+
);
|
|
57
|
+
const reasons = {
|
|
58
|
+
duration: '达到采集时长',
|
|
59
|
+
output_limit: '达到输出上限',
|
|
60
|
+
client_closed: '客户端关闭连接',
|
|
61
|
+
remote_closed: '服务端关闭连接',
|
|
62
|
+
};
|
|
63
|
+
const lines = [
|
|
64
|
+
target + ' ' + data.mode,
|
|
65
|
+
'采集结束 ' + (reasons[data.stop_reason] ?? data.stop_reason) + '(' + data.duration_ms + ' ms)',
|
|
66
|
+
];
|
|
67
|
+
if (data.input_attempted)
|
|
68
|
+
lines.push(
|
|
69
|
+
data.input_sent ? '输入已发送;远程执行结果未知。' : '已尝试发送输入;远程执行结果未知。'
|
|
70
|
+
);
|
|
71
|
+
lines.push('', data.output ? '控制台输出' : '本次未收到控制台输出。');
|
|
72
|
+
if (data.output) lines.push(data.output);
|
|
73
|
+
if (data.output_truncated) lines.push('输出已截断。');
|
|
74
|
+
return lines.join('\n');
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
function operationLabel(id) {
|
|
78
|
+
// Help rendering must not mask the original failure if catalog loading failed.
|
|
79
|
+
try {
|
|
80
|
+
const op = catalog().ops.get(id);
|
|
81
|
+
if (op) return titleOf(op);
|
|
82
|
+
} catch {}
|
|
83
|
+
return id;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
function previewView(r, ctx) {
|
|
87
|
+
const lines = ['请求预览(未执行)'];
|
|
88
|
+
const add = (label, value) => lines.push(label + ' ' + value);
|
|
89
|
+
if (r.product)
|
|
90
|
+
add(
|
|
91
|
+
'目标',
|
|
92
|
+
r.product.toUpperCase() + ' ' + r.id + (r.container_id ? ' / 容器 ' + r.container_id : '')
|
|
93
|
+
);
|
|
94
|
+
if (r.operation) add('操作', operationLabel(r.operation));
|
|
95
|
+
if (r.url) {
|
|
96
|
+
const url = new URL(r.url);
|
|
97
|
+
add('请求', r.method + ' ' + url.origin + url.pathname);
|
|
98
|
+
for (const [k, v] of url.searchParams) add(' ' + k, v);
|
|
99
|
+
if (r.headers?.Referer) add('Referer', r.headers.Referer);
|
|
100
|
+
} else if (r.helper_path) add('面板请求', r.method + ' ' + r.helper_path);
|
|
101
|
+
if (r.authentication) add('身份', r.authentication === 'none' ? '匿名访问' : 'API Key');
|
|
102
|
+
if (r.mode) {
|
|
103
|
+
add('连接', r.logs ? '日志快照' : r.mode + (r.stream ? '(持续会话)' : ''));
|
|
104
|
+
add('采集限制', r.duration_ms / 1000 + ' 秒,最多 ' + r.max_output_bytes + ' 字节');
|
|
105
|
+
if (r.logs) add('日志范围', '最近 ' + r.tail_lines + ' 行,' + r.since_seconds + ' 秒内');
|
|
106
|
+
if (r.initial_input_requested) add('初始输入', '已指定,预览时未读取');
|
|
107
|
+
}
|
|
108
|
+
if (r.query && Object.keys(r.query).length) add('查询参数', '\n' + ctx.jsonStringify(r.query, 2));
|
|
109
|
+
if (r.body !== null && r.body !== undefined) add('请求体', '\n' + ctx.jsonStringify(r.body, 2));
|
|
110
|
+
if (r.files) add('文件', '\n' + ctx.jsonStringify(r.files, 2));
|
|
111
|
+
if (r.source) add('本地源文件', r.source);
|
|
112
|
+
if (r.output) add('保存到', r.output);
|
|
113
|
+
if (r.is_write) lines.push('执行此操作可能更改远端状态。');
|
|
114
|
+
return lines.join('\n');
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
export function humanResult(args, r, flags, ctx) {
|
|
118
|
+
let text;
|
|
119
|
+
if (flags.version) text = 'rainyun-cli ' + r.data.version;
|
|
120
|
+
else if (r.dry_run) text = previewView(r, ctx);
|
|
121
|
+
else if (args[0] === 'auth') text = authView(args[1], r.data);
|
|
122
|
+
else if (args[0] === 'terminal') text = terminalView(r.data);
|
|
123
|
+
else text = discoveryView(args, r, flags, ctx) ?? businessView(args, r, flags, ctx);
|
|
124
|
+
const output = safeDisplay(text ?? ctx.jsonStringify(r, 2));
|
|
125
|
+
return output.endsWith('\n') ? output : output + '\n';
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
const kinds = {
|
|
129
|
+
usage: '用法错误',
|
|
130
|
+
auth: '认证失败',
|
|
131
|
+
api: '接口返回错误',
|
|
132
|
+
network: '连接异常',
|
|
133
|
+
protocol: '响应格式异常',
|
|
134
|
+
};
|
|
135
|
+
const messages = {
|
|
136
|
+
'helper task failed': '面板任务执行失败。',
|
|
137
|
+
'unknown command; use --help': '无法识别命令,可用 rainyun-cli help 查看命令列表。',
|
|
138
|
+
'Unknown flag. Use --help.': '无法识别选项,可用 --help 查看用法。',
|
|
139
|
+
'API Key is not configured. Run rainyun-cli auth login or set RAINYUN_API_KEY.':
|
|
140
|
+
'尚未配置 API Key。请在自己的终端运行 rainyun-cli auth login,或设置 RAINYUN_API_KEY。',
|
|
141
|
+
};
|
|
142
|
+
export function humanError(error, ctx) {
|
|
143
|
+
const lines = [
|
|
144
|
+
(kinds[error.kind] ?? '错误') +
|
|
145
|
+
' ' +
|
|
146
|
+
(Object.hasOwn(messages, error.message) ? messages[error.message] : error.message),
|
|
147
|
+
];
|
|
148
|
+
if (error.operation) lines.push('操作 ' + operationLabel(error.operation));
|
|
149
|
+
const codes = [];
|
|
150
|
+
if (error.http_status !== undefined) codes.push('HTTP ' + error.http_status);
|
|
151
|
+
if (error.business_code !== undefined) codes.push('业务码 ' + ctx.jsonStringify(error.business_code));
|
|
152
|
+
if (codes.length) lines.push(codes.join(' · '));
|
|
153
|
+
if (error.details?.task_id) lines.push('任务 ID ' + error.details.task_id);
|
|
154
|
+
if (error.outcome === 'failed') {
|
|
155
|
+
const task = error.details?.response?.data;
|
|
156
|
+
const reason = [task?.error, task?.message, task?.reason].find(
|
|
157
|
+
(v) => typeof v === 'string' && v.trim()
|
|
158
|
+
);
|
|
159
|
+
if (reason) lines.push('失败原因 ' + reason);
|
|
160
|
+
}
|
|
161
|
+
if (error.outcome === 'unknown') lines.push('执行结果未知,请先查询资源或任务状态。');
|
|
162
|
+
if (error.details?.terminal?.output) lines.push('已收到的控制台输出', error.details.terminal.output);
|
|
163
|
+
return safeDisplay(lines.join('\n')) + '\n';
|
|
164
|
+
}
|