@switchbot/openapi-cli 3.1.1 → 3.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/README.md +3 -3
- package/dist/index.js +56945 -169
- package/dist/policy/schema/v0.2.json +1 -1
- package/package.json +3 -2
- package/dist/api/client.js +0 -235
- package/dist/auth.js +0 -20
- package/dist/commands/agent-bootstrap.js +0 -182
- package/dist/commands/auth.js +0 -354
- package/dist/commands/batch.js +0 -413
- package/dist/commands/cache.js +0 -126
- package/dist/commands/capabilities.js +0 -385
- package/dist/commands/catalog.js +0 -359
- package/dist/commands/completion.js +0 -385
- package/dist/commands/config.js +0 -376
- package/dist/commands/daemon.js +0 -410
- package/dist/commands/device-meta.js +0 -159
- package/dist/commands/devices.js +0 -948
- package/dist/commands/doctor.js +0 -1015
- package/dist/commands/events.js +0 -563
- package/dist/commands/expand.js +0 -130
- package/dist/commands/explain.js +0 -139
- package/dist/commands/health.js +0 -113
- package/dist/commands/history.js +0 -320
- package/dist/commands/identity.js +0 -59
- package/dist/commands/install.js +0 -246
- package/dist/commands/mcp.js +0 -2017
- package/dist/commands/plan.js +0 -653
- package/dist/commands/policy.js +0 -586
- package/dist/commands/quota.js +0 -78
- package/dist/commands/rules.js +0 -875
- package/dist/commands/scenes.js +0 -264
- package/dist/commands/schema.js +0 -177
- package/dist/commands/status-sync.js +0 -131
- package/dist/commands/uninstall.js +0 -237
- package/dist/commands/upgrade-check.js +0 -107
- package/dist/commands/watch.js +0 -194
- package/dist/commands/webhook.js +0 -182
- package/dist/config.js +0 -258
- package/dist/credentials/backends/file.js +0 -101
- package/dist/credentials/backends/linux.js +0 -129
- package/dist/credentials/backends/macos.js +0 -129
- package/dist/credentials/backends/windows.js +0 -215
- package/dist/credentials/keychain.js +0 -88
- package/dist/credentials/prime.js +0 -52
- package/dist/devices/cache.js +0 -293
- package/dist/devices/catalog.js +0 -767
- package/dist/devices/device-meta.js +0 -56
- package/dist/devices/history-agg.js +0 -138
- package/dist/devices/history-query.js +0 -181
- package/dist/devices/param-validator.js +0 -433
- package/dist/devices/resources.js +0 -270
- package/dist/install/default-steps.js +0 -257
- package/dist/install/preflight.js +0 -212
- package/dist/install/steps.js +0 -67
- package/dist/lib/command-keywords.js +0 -17
- package/dist/lib/daemon-state.js +0 -46
- package/dist/lib/destructive-mode.js +0 -12
- package/dist/lib/devices.js +0 -382
- package/dist/lib/idempotency.js +0 -106
- package/dist/lib/plan-store.js +0 -68
- package/dist/lib/request-context.js +0 -12
- package/dist/lib/scenes.js +0 -10
- package/dist/logger.js +0 -16
- package/dist/mcp/device-history.js +0 -145
- package/dist/mcp/events-subscription.js +0 -213
- package/dist/mqtt/client.js +0 -180
- package/dist/mqtt/credential.js +0 -30
- package/dist/policy/add-rule.js +0 -124
- package/dist/policy/diff.js +0 -91
- package/dist/policy/format.js +0 -57
- package/dist/policy/load.js +0 -61
- package/dist/policy/migrate.js +0 -67
- package/dist/policy/schema.js +0 -18
- package/dist/policy/validate.js +0 -262
- package/dist/rules/action.js +0 -216
- package/dist/rules/audit-query.js +0 -89
- package/dist/rules/conflict-analyzer.js +0 -214
- package/dist/rules/cron-scheduler.js +0 -186
- package/dist/rules/destructive.js +0 -52
- package/dist/rules/engine.js +0 -757
- package/dist/rules/matcher.js +0 -230
- package/dist/rules/pid-file.js +0 -95
- package/dist/rules/quiet-hours.js +0 -45
- package/dist/rules/suggest.js +0 -95
- package/dist/rules/throttle.js +0 -116
- package/dist/rules/types.js +0 -34
- package/dist/rules/webhook-listener.js +0 -223
- package/dist/rules/webhook-token.js +0 -90
- package/dist/schema/field-aliases.js +0 -131
- package/dist/sinks/dispatcher.js +0 -12
- package/dist/sinks/file.js +0 -19
- package/dist/sinks/format.js +0 -56
- package/dist/sinks/homeassistant.js +0 -44
- package/dist/sinks/openclaw.js +0 -33
- package/dist/sinks/stdout.js +0 -5
- package/dist/sinks/telegram.js +0 -28
- package/dist/sinks/types.js +0 -1
- package/dist/sinks/webhook.js +0 -22
- package/dist/status-sync/manager.js +0 -268
- package/dist/utils/arg-parsers.js +0 -66
- package/dist/utils/audit.js +0 -121
- package/dist/utils/filter.js +0 -189
- package/dist/utils/flags.js +0 -186
- package/dist/utils/format.js +0 -117
- package/dist/utils/health.js +0 -101
- package/dist/utils/help-json.js +0 -54
- package/dist/utils/name-resolver.js +0 -137
- package/dist/utils/output.js +0 -404
- package/dist/utils/quota.js +0 -227
- package/dist/utils/redact.js +0 -68
- package/dist/utils/retry.js +0 -140
- package/dist/utils/string.js +0 -22
- package/dist/version.js +0 -4
package/dist/commands/auth.js
DELETED
|
@@ -1,354 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* `switchbot auth` command group (v2.9 preview, part of Phase 3A).
|
|
3
|
-
*
|
|
4
|
-
* Surfaces the credential store abstraction added in F1/F2 so users
|
|
5
|
-
* can introspect, write to, delete from, and migrate into the OS
|
|
6
|
-
* keychain without editing `~/.switchbot/config.json` by hand.
|
|
7
|
-
*
|
|
8
|
-
* All subcommands honour the active `--profile <name>` flag so a user
|
|
9
|
-
* who runs multiple accounts keeps the keychain entries cleanly
|
|
10
|
-
* partitioned.
|
|
11
|
-
*
|
|
12
|
-
* No credential material is ever printed in plain text. `get` emits
|
|
13
|
-
* a masked summary only; `set` reads via a TTY prompt (echo-off) or a
|
|
14
|
-
* file passed via `--stdin-file <path>`. `migrate` never touches the
|
|
15
|
-
* keychain unless the backend reports `writable: true`.
|
|
16
|
-
*/
|
|
17
|
-
import fs from 'node:fs';
|
|
18
|
-
import path from 'node:path';
|
|
19
|
-
import os from 'node:os';
|
|
20
|
-
import readline from 'node:readline';
|
|
21
|
-
import { exitWithError, isJsonMode, printJson } from '../utils/output.js';
|
|
22
|
-
import { stringArg } from '../utils/arg-parsers.js';
|
|
23
|
-
import { getActiveProfile } from '../lib/request-context.js';
|
|
24
|
-
import { selectCredentialStore, } from '../credentials/keychain.js';
|
|
25
|
-
function activeProfile() {
|
|
26
|
-
return getActiveProfile() ?? 'default';
|
|
27
|
-
}
|
|
28
|
-
function maskValue(value) {
|
|
29
|
-
if (value.length === 0)
|
|
30
|
-
return '';
|
|
31
|
-
if (value.length <= 4)
|
|
32
|
-
return '*'.repeat(value.length);
|
|
33
|
-
const head = value.slice(0, 2);
|
|
34
|
-
const tail = value.slice(-2);
|
|
35
|
-
return `${head}${'*'.repeat(Math.max(4, value.length - 4))}${tail}`;
|
|
36
|
-
}
|
|
37
|
-
async function promptSecret(question) {
|
|
38
|
-
const rl = readline.createInterface({ input: process.stdin, output: process.stderr, terminal: true });
|
|
39
|
-
return new Promise((resolve) => {
|
|
40
|
-
process.stderr.write(question);
|
|
41
|
-
const stdin = process.stdin;
|
|
42
|
-
let answer = '';
|
|
43
|
-
const onData = (chunk) => {
|
|
44
|
-
const s = chunk.toString('utf-8');
|
|
45
|
-
for (const ch of s) {
|
|
46
|
-
if (ch === '\r' || ch === '\n') {
|
|
47
|
-
stdin.removeListener('data', onData);
|
|
48
|
-
if (stdin.setRawMode)
|
|
49
|
-
stdin.setRawMode(false);
|
|
50
|
-
stdin.pause();
|
|
51
|
-
process.stderr.write('\n');
|
|
52
|
-
rl.close();
|
|
53
|
-
resolve(answer);
|
|
54
|
-
return;
|
|
55
|
-
}
|
|
56
|
-
if (ch === '\u0003') {
|
|
57
|
-
process.exit(130);
|
|
58
|
-
}
|
|
59
|
-
if (ch === '\u007f' || ch === '\b') {
|
|
60
|
-
answer = answer.slice(0, -1);
|
|
61
|
-
continue;
|
|
62
|
-
}
|
|
63
|
-
answer += ch;
|
|
64
|
-
}
|
|
65
|
-
};
|
|
66
|
-
if (stdin.setRawMode)
|
|
67
|
-
stdin.setRawMode(true);
|
|
68
|
-
stdin.resume();
|
|
69
|
-
stdin.on('data', onData);
|
|
70
|
-
});
|
|
71
|
-
}
|
|
72
|
-
function readStdinFile(filePath) {
|
|
73
|
-
if (!fs.existsSync(filePath)) {
|
|
74
|
-
exitWithError({
|
|
75
|
-
code: 2,
|
|
76
|
-
kind: 'usage',
|
|
77
|
-
message: `--stdin-file: file not found: ${filePath}`,
|
|
78
|
-
});
|
|
79
|
-
}
|
|
80
|
-
let parsed;
|
|
81
|
-
try {
|
|
82
|
-
parsed = JSON.parse(fs.readFileSync(filePath, 'utf-8'));
|
|
83
|
-
}
|
|
84
|
-
catch (err) {
|
|
85
|
-
exitWithError({
|
|
86
|
-
code: 2,
|
|
87
|
-
kind: 'usage',
|
|
88
|
-
message: `--stdin-file: invalid JSON: ${err instanceof Error ? err.message : String(err)}`,
|
|
89
|
-
});
|
|
90
|
-
}
|
|
91
|
-
if (!parsed ||
|
|
92
|
-
typeof parsed !== 'object' ||
|
|
93
|
-
typeof parsed.token !== 'string' ||
|
|
94
|
-
typeof parsed.secret !== 'string') {
|
|
95
|
-
exitWithError({
|
|
96
|
-
code: 2,
|
|
97
|
-
kind: 'usage',
|
|
98
|
-
message: '--stdin-file must contain a JSON object with "token" and "secret" strings.',
|
|
99
|
-
});
|
|
100
|
-
}
|
|
101
|
-
const { token, secret } = parsed;
|
|
102
|
-
if (!token || !secret) {
|
|
103
|
-
exitWithError({
|
|
104
|
-
code: 2,
|
|
105
|
-
kind: 'usage',
|
|
106
|
-
message: '--stdin-file: token and secret must be non-empty.',
|
|
107
|
-
});
|
|
108
|
-
}
|
|
109
|
-
return { token, secret };
|
|
110
|
-
}
|
|
111
|
-
function cleanupMigratedSourceFile(sourceFile, parsed) {
|
|
112
|
-
const next = { ...parsed };
|
|
113
|
-
delete next.token;
|
|
114
|
-
delete next.secret;
|
|
115
|
-
if (Object.keys(next).length === 0) {
|
|
116
|
-
fs.unlinkSync(sourceFile);
|
|
117
|
-
return 'deleted';
|
|
118
|
-
}
|
|
119
|
-
fs.writeFileSync(sourceFile, JSON.stringify(next, null, 2), { mode: 0o600 });
|
|
120
|
-
return 'scrubbed';
|
|
121
|
-
}
|
|
122
|
-
export function registerAuthCommand(program) {
|
|
123
|
-
const auth = program
|
|
124
|
-
.command('auth')
|
|
125
|
-
.description('Manage SwitchBot credentials in the OS keychain (preview)');
|
|
126
|
-
const keychain = auth
|
|
127
|
-
.command('keychain')
|
|
128
|
-
.description('OS keychain backend (describe/get/set/delete/migrate)');
|
|
129
|
-
keychain
|
|
130
|
-
.command('describe')
|
|
131
|
-
.description('Show which credential backend is active on this machine')
|
|
132
|
-
.action(async () => {
|
|
133
|
-
const store = await selectCredentialStore();
|
|
134
|
-
const desc = store.describe();
|
|
135
|
-
if (isJsonMode()) {
|
|
136
|
-
printJson(desc);
|
|
137
|
-
return;
|
|
138
|
-
}
|
|
139
|
-
console.log(`backend : ${desc.backend}`);
|
|
140
|
-
console.log(`tag : ${desc.tag}`);
|
|
141
|
-
console.log(`writable: ${desc.writable ? 'yes' : 'no'}`);
|
|
142
|
-
if (desc.notes)
|
|
143
|
-
console.log(`notes : ${desc.notes}`);
|
|
144
|
-
});
|
|
145
|
-
keychain
|
|
146
|
-
.command('get')
|
|
147
|
-
.description('Check whether the active profile has credentials (masked output)')
|
|
148
|
-
.action(async () => {
|
|
149
|
-
const profile = activeProfile();
|
|
150
|
-
const store = await selectCredentialStore();
|
|
151
|
-
const creds = await store.get(profile);
|
|
152
|
-
if (!creds) {
|
|
153
|
-
if (isJsonMode()) {
|
|
154
|
-
printJson({ profile, backend: store.name, present: false });
|
|
155
|
-
return;
|
|
156
|
-
}
|
|
157
|
-
console.log(`No credentials found for profile "${profile}" in backend "${store.name}".`);
|
|
158
|
-
process.exit(1);
|
|
159
|
-
}
|
|
160
|
-
if (isJsonMode()) {
|
|
161
|
-
printJson({
|
|
162
|
-
profile,
|
|
163
|
-
backend: store.name,
|
|
164
|
-
present: true,
|
|
165
|
-
token: { length: creds.token.length, masked: maskValue(creds.token) },
|
|
166
|
-
secret: { length: creds.secret.length, masked: maskValue(creds.secret) },
|
|
167
|
-
});
|
|
168
|
-
return;
|
|
169
|
-
}
|
|
170
|
-
console.log(`profile : ${profile}`);
|
|
171
|
-
console.log(`backend : ${store.name}`);
|
|
172
|
-
console.log(`token : ${maskValue(creds.token)} (${creds.token.length} chars)`);
|
|
173
|
-
console.log(`secret : ${maskValue(creds.secret)} (${creds.secret.length} chars)`);
|
|
174
|
-
});
|
|
175
|
-
keychain
|
|
176
|
-
.command('set')
|
|
177
|
-
.description('Write token and secret to the keychain for the active profile')
|
|
178
|
-
.option('--stdin-file <path>', 'Read {"token","secret"} JSON from file (for non-TTY environments)', stringArg('--stdin-file'))
|
|
179
|
-
.action(async (options) => {
|
|
180
|
-
const profile = activeProfile();
|
|
181
|
-
const store = await selectCredentialStore();
|
|
182
|
-
if (!store.describe().writable) {
|
|
183
|
-
exitWithError({
|
|
184
|
-
code: 1,
|
|
185
|
-
kind: 'runtime',
|
|
186
|
-
message: `backend "${store.name}" is not writable on this machine`,
|
|
187
|
-
hint: 'Install the OS keychain helper or use ~/.switchbot/config.json directly.',
|
|
188
|
-
});
|
|
189
|
-
}
|
|
190
|
-
let bundle;
|
|
191
|
-
if (options.stdinFile) {
|
|
192
|
-
bundle = readStdinFile(options.stdinFile);
|
|
193
|
-
}
|
|
194
|
-
else if (process.stdin.isTTY) {
|
|
195
|
-
const token = (await promptSecret('Token : ')).trim();
|
|
196
|
-
const secret = (await promptSecret('Secret: ')).trim();
|
|
197
|
-
if (!token || !secret) {
|
|
198
|
-
exitWithError({
|
|
199
|
-
code: 2,
|
|
200
|
-
kind: 'usage',
|
|
201
|
-
message: 'Both token and secret are required.',
|
|
202
|
-
});
|
|
203
|
-
}
|
|
204
|
-
bundle = { token, secret };
|
|
205
|
-
}
|
|
206
|
-
else {
|
|
207
|
-
exitWithError({
|
|
208
|
-
code: 2,
|
|
209
|
-
kind: 'usage',
|
|
210
|
-
message: 'Non-TTY input requires --stdin-file <path>.',
|
|
211
|
-
});
|
|
212
|
-
}
|
|
213
|
-
try {
|
|
214
|
-
await store.set(profile, bundle);
|
|
215
|
-
}
|
|
216
|
-
catch (err) {
|
|
217
|
-
exitWithError({
|
|
218
|
-
code: 1,
|
|
219
|
-
kind: 'runtime',
|
|
220
|
-
message: `keychain write failed: ${err instanceof Error ? err.message : String(err)}`,
|
|
221
|
-
});
|
|
222
|
-
}
|
|
223
|
-
if (isJsonMode()) {
|
|
224
|
-
printJson({ profile, backend: store.name, written: true });
|
|
225
|
-
return;
|
|
226
|
-
}
|
|
227
|
-
console.log(`Stored credentials for profile "${profile}" in backend "${store.name}".`);
|
|
228
|
-
});
|
|
229
|
-
keychain
|
|
230
|
-
.command('delete')
|
|
231
|
-
.description('Remove credentials for the active profile from the keychain')
|
|
232
|
-
.option('--yes', 'Skip the interactive confirmation prompt')
|
|
233
|
-
.action(async (options) => {
|
|
234
|
-
const profile = activeProfile();
|
|
235
|
-
const store = await selectCredentialStore();
|
|
236
|
-
if (!options.yes && process.stdin.isTTY) {
|
|
237
|
-
const reply = (await promptSecret(`Delete credentials for profile "${profile}" from backend "${store.name}"? type DELETE to confirm: `)).trim();
|
|
238
|
-
if (reply !== 'DELETE') {
|
|
239
|
-
if (isJsonMode()) {
|
|
240
|
-
printJson({ profile, backend: store.name, deleted: false, reason: 'cancelled' });
|
|
241
|
-
return;
|
|
242
|
-
}
|
|
243
|
-
console.log('Aborted.');
|
|
244
|
-
process.exit(0);
|
|
245
|
-
}
|
|
246
|
-
}
|
|
247
|
-
try {
|
|
248
|
-
await store.delete(profile);
|
|
249
|
-
}
|
|
250
|
-
catch (err) {
|
|
251
|
-
exitWithError({
|
|
252
|
-
code: 1,
|
|
253
|
-
kind: 'runtime',
|
|
254
|
-
message: `keychain delete failed: ${err instanceof Error ? err.message : String(err)}`,
|
|
255
|
-
});
|
|
256
|
-
}
|
|
257
|
-
if (isJsonMode()) {
|
|
258
|
-
printJson({ profile, backend: store.name, deleted: true });
|
|
259
|
-
return;
|
|
260
|
-
}
|
|
261
|
-
console.log(`Deleted credentials for profile "${profile}" in backend "${store.name}".`);
|
|
262
|
-
});
|
|
263
|
-
keychain
|
|
264
|
-
.command('migrate')
|
|
265
|
-
.description('Copy credentials from ~/.switchbot/config.json (or --profile) into the keychain')
|
|
266
|
-
.option('--delete-file', 'Remove the source credential file when possible; otherwise scrub token/secret and keep metadata')
|
|
267
|
-
.action(async (options) => {
|
|
268
|
-
const profile = activeProfile();
|
|
269
|
-
const store = await selectCredentialStore();
|
|
270
|
-
if (!store.describe().writable) {
|
|
271
|
-
exitWithError({
|
|
272
|
-
code: 1,
|
|
273
|
-
kind: 'runtime',
|
|
274
|
-
message: `backend "${store.name}" is not writable on this machine`,
|
|
275
|
-
});
|
|
276
|
-
}
|
|
277
|
-
const sourceFile = profile === 'default'
|
|
278
|
-
? path.join(os.homedir(), '.switchbot', 'config.json')
|
|
279
|
-
: path.join(os.homedir(), '.switchbot', 'profiles', `${profile}.json`);
|
|
280
|
-
if (!fs.existsSync(sourceFile)) {
|
|
281
|
-
exitWithError({
|
|
282
|
-
code: 2,
|
|
283
|
-
kind: 'usage',
|
|
284
|
-
message: `source file not found: ${sourceFile}`,
|
|
285
|
-
hint: 'Run "switchbot config set-token" first or use "switchbot auth keychain set" directly.',
|
|
286
|
-
});
|
|
287
|
-
}
|
|
288
|
-
let parsed;
|
|
289
|
-
try {
|
|
290
|
-
const raw = JSON.parse(fs.readFileSync(sourceFile, 'utf-8'));
|
|
291
|
-
if (!raw || typeof raw !== 'object' || Array.isArray(raw)) {
|
|
292
|
-
throw new Error('expected a JSON object');
|
|
293
|
-
}
|
|
294
|
-
parsed = raw;
|
|
295
|
-
}
|
|
296
|
-
catch (err) {
|
|
297
|
-
exitWithError({
|
|
298
|
-
code: 1,
|
|
299
|
-
kind: 'runtime',
|
|
300
|
-
message: `failed to parse ${sourceFile}: ${err instanceof Error ? err.message : String(err)}`,
|
|
301
|
-
});
|
|
302
|
-
}
|
|
303
|
-
const token = typeof parsed.token === 'string' ? parsed.token : '';
|
|
304
|
-
const secret = typeof parsed.secret === 'string' ? parsed.secret : '';
|
|
305
|
-
if (!token || !secret) {
|
|
306
|
-
exitWithError({
|
|
307
|
-
code: 1,
|
|
308
|
-
kind: 'runtime',
|
|
309
|
-
message: `source file missing token or secret: ${sourceFile}`,
|
|
310
|
-
});
|
|
311
|
-
}
|
|
312
|
-
try {
|
|
313
|
-
await store.set(profile, { token, secret });
|
|
314
|
-
}
|
|
315
|
-
catch (err) {
|
|
316
|
-
exitWithError({
|
|
317
|
-
code: 1,
|
|
318
|
-
kind: 'runtime',
|
|
319
|
-
message: `keychain write failed: ${err instanceof Error ? err.message : String(err)}`,
|
|
320
|
-
});
|
|
321
|
-
}
|
|
322
|
-
let cleanup = 'kept';
|
|
323
|
-
if (options.deleteFile) {
|
|
324
|
-
try {
|
|
325
|
-
cleanup = cleanupMigratedSourceFile(sourceFile, parsed);
|
|
326
|
-
}
|
|
327
|
-
catch (err) {
|
|
328
|
-
// Non-fatal: migration succeeded, we just couldn't clean up.
|
|
329
|
-
console.error(`warning: could not remove ${sourceFile}: ${err instanceof Error ? err.message : String(err)}`);
|
|
330
|
-
}
|
|
331
|
-
}
|
|
332
|
-
if (isJsonMode()) {
|
|
333
|
-
printJson({
|
|
334
|
-
profile,
|
|
335
|
-
backend: store.name,
|
|
336
|
-
migrated: true,
|
|
337
|
-
sourceFile,
|
|
338
|
-
sourceDeleted: cleanup === 'deleted',
|
|
339
|
-
sourceScrubbed: cleanup === 'scrubbed',
|
|
340
|
-
});
|
|
341
|
-
return;
|
|
342
|
-
}
|
|
343
|
-
console.log(`Migrated profile "${profile}" to backend "${store.name}".`);
|
|
344
|
-
const cleanupNote = cleanup === 'deleted'
|
|
345
|
-
? ' (deleted)'
|
|
346
|
-
: cleanup === 'scrubbed'
|
|
347
|
-
? ' (credentials removed; metadata kept)'
|
|
348
|
-
: '';
|
|
349
|
-
console.log(`source: ${sourceFile}${cleanupNote}`);
|
|
350
|
-
if (!options.deleteFile) {
|
|
351
|
-
console.log('Source file kept — pass --delete-file on the next run to remove it.');
|
|
352
|
-
}
|
|
353
|
-
});
|
|
354
|
-
}
|