@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.
Files changed (113) hide show
  1. package/README.md +3 -3
  2. package/dist/index.js +56945 -169
  3. package/dist/policy/schema/v0.2.json +1 -1
  4. package/package.json +3 -2
  5. package/dist/api/client.js +0 -235
  6. package/dist/auth.js +0 -20
  7. package/dist/commands/agent-bootstrap.js +0 -182
  8. package/dist/commands/auth.js +0 -354
  9. package/dist/commands/batch.js +0 -413
  10. package/dist/commands/cache.js +0 -126
  11. package/dist/commands/capabilities.js +0 -385
  12. package/dist/commands/catalog.js +0 -359
  13. package/dist/commands/completion.js +0 -385
  14. package/dist/commands/config.js +0 -376
  15. package/dist/commands/daemon.js +0 -410
  16. package/dist/commands/device-meta.js +0 -159
  17. package/dist/commands/devices.js +0 -948
  18. package/dist/commands/doctor.js +0 -1015
  19. package/dist/commands/events.js +0 -563
  20. package/dist/commands/expand.js +0 -130
  21. package/dist/commands/explain.js +0 -139
  22. package/dist/commands/health.js +0 -113
  23. package/dist/commands/history.js +0 -320
  24. package/dist/commands/identity.js +0 -59
  25. package/dist/commands/install.js +0 -246
  26. package/dist/commands/mcp.js +0 -2017
  27. package/dist/commands/plan.js +0 -653
  28. package/dist/commands/policy.js +0 -586
  29. package/dist/commands/quota.js +0 -78
  30. package/dist/commands/rules.js +0 -875
  31. package/dist/commands/scenes.js +0 -264
  32. package/dist/commands/schema.js +0 -177
  33. package/dist/commands/status-sync.js +0 -131
  34. package/dist/commands/uninstall.js +0 -237
  35. package/dist/commands/upgrade-check.js +0 -107
  36. package/dist/commands/watch.js +0 -194
  37. package/dist/commands/webhook.js +0 -182
  38. package/dist/config.js +0 -258
  39. package/dist/credentials/backends/file.js +0 -101
  40. package/dist/credentials/backends/linux.js +0 -129
  41. package/dist/credentials/backends/macos.js +0 -129
  42. package/dist/credentials/backends/windows.js +0 -215
  43. package/dist/credentials/keychain.js +0 -88
  44. package/dist/credentials/prime.js +0 -52
  45. package/dist/devices/cache.js +0 -293
  46. package/dist/devices/catalog.js +0 -767
  47. package/dist/devices/device-meta.js +0 -56
  48. package/dist/devices/history-agg.js +0 -138
  49. package/dist/devices/history-query.js +0 -181
  50. package/dist/devices/param-validator.js +0 -433
  51. package/dist/devices/resources.js +0 -270
  52. package/dist/install/default-steps.js +0 -257
  53. package/dist/install/preflight.js +0 -212
  54. package/dist/install/steps.js +0 -67
  55. package/dist/lib/command-keywords.js +0 -17
  56. package/dist/lib/daemon-state.js +0 -46
  57. package/dist/lib/destructive-mode.js +0 -12
  58. package/dist/lib/devices.js +0 -382
  59. package/dist/lib/idempotency.js +0 -106
  60. package/dist/lib/plan-store.js +0 -68
  61. package/dist/lib/request-context.js +0 -12
  62. package/dist/lib/scenes.js +0 -10
  63. package/dist/logger.js +0 -16
  64. package/dist/mcp/device-history.js +0 -145
  65. package/dist/mcp/events-subscription.js +0 -213
  66. package/dist/mqtt/client.js +0 -180
  67. package/dist/mqtt/credential.js +0 -30
  68. package/dist/policy/add-rule.js +0 -124
  69. package/dist/policy/diff.js +0 -91
  70. package/dist/policy/format.js +0 -57
  71. package/dist/policy/load.js +0 -61
  72. package/dist/policy/migrate.js +0 -67
  73. package/dist/policy/schema.js +0 -18
  74. package/dist/policy/validate.js +0 -262
  75. package/dist/rules/action.js +0 -216
  76. package/dist/rules/audit-query.js +0 -89
  77. package/dist/rules/conflict-analyzer.js +0 -214
  78. package/dist/rules/cron-scheduler.js +0 -186
  79. package/dist/rules/destructive.js +0 -52
  80. package/dist/rules/engine.js +0 -757
  81. package/dist/rules/matcher.js +0 -230
  82. package/dist/rules/pid-file.js +0 -95
  83. package/dist/rules/quiet-hours.js +0 -45
  84. package/dist/rules/suggest.js +0 -95
  85. package/dist/rules/throttle.js +0 -116
  86. package/dist/rules/types.js +0 -34
  87. package/dist/rules/webhook-listener.js +0 -223
  88. package/dist/rules/webhook-token.js +0 -90
  89. package/dist/schema/field-aliases.js +0 -131
  90. package/dist/sinks/dispatcher.js +0 -12
  91. package/dist/sinks/file.js +0 -19
  92. package/dist/sinks/format.js +0 -56
  93. package/dist/sinks/homeassistant.js +0 -44
  94. package/dist/sinks/openclaw.js +0 -33
  95. package/dist/sinks/stdout.js +0 -5
  96. package/dist/sinks/telegram.js +0 -28
  97. package/dist/sinks/types.js +0 -1
  98. package/dist/sinks/webhook.js +0 -22
  99. package/dist/status-sync/manager.js +0 -268
  100. package/dist/utils/arg-parsers.js +0 -66
  101. package/dist/utils/audit.js +0 -121
  102. package/dist/utils/filter.js +0 -189
  103. package/dist/utils/flags.js +0 -186
  104. package/dist/utils/format.js +0 -117
  105. package/dist/utils/health.js +0 -101
  106. package/dist/utils/help-json.js +0 -54
  107. package/dist/utils/name-resolver.js +0 -137
  108. package/dist/utils/output.js +0 -404
  109. package/dist/utils/quota.js +0 -227
  110. package/dist/utils/redact.js +0 -68
  111. package/dist/utils/retry.js +0 -140
  112. package/dist/utils/string.js +0 -22
  113. package/dist/version.js +0 -4
@@ -1,586 +0,0 @@
1
- import { readFileSync, writeFileSync, existsSync, mkdirSync, copyFileSync, statSync } from 'node:fs';
2
- import { dirname, resolve as resolvePath } from 'node:path';
3
- import { fileURLToPath } from 'node:url';
4
- import { parse as yamlParse } from 'yaml';
5
- import { printJson, emitJsonError, isJsonMode, exitWithError } from '../utils/output.js';
6
- import { loadPolicyFile, resolvePolicyPath, DEFAULT_POLICY_PATH, PolicyFileNotFoundError, PolicyYamlParseError, } from '../policy/load.js';
7
- import { validateLoadedPolicy } from '../policy/validate.js';
8
- import { formatValidationResult } from '../policy/format.js';
9
- import { CURRENT_POLICY_SCHEMA_VERSION, SUPPORTED_POLICY_SCHEMA_VERSIONS, } from '../policy/schema.js';
10
- import { planMigration, PolicyMigrationError } from '../policy/migrate.js';
11
- import { addRuleToPolicyFile, AddRuleError } from '../policy/add-rule.js';
12
- import { diffPolicyValues } from '../policy/diff.js';
13
- // Latest version the CLI knows how to migrate *to*.
14
- // CURRENT_POLICY_SCHEMA_VERSION is the version `policy new` emits by default.
15
- const LATEST_SUPPORTED_VERSION = SUPPORTED_POLICY_SCHEMA_VERSIONS[SUPPORTED_POLICY_SCHEMA_VERSIONS.length - 1];
16
- function readEmbeddedTemplate() {
17
- const url = new URL('../policy/examples/policy.example.yaml', import.meta.url);
18
- return readFileSync(fileURLToPath(url), 'utf-8');
19
- }
20
- export class PolicyFileExistsError extends Error {
21
- policyPath;
22
- constructor(policyPath) {
23
- super(`refusing to overwrite existing policy at ${policyPath}`);
24
- this.policyPath = policyPath;
25
- this.name = 'PolicyFileExistsError';
26
- }
27
- }
28
- /**
29
- * Write the starter policy template to `policyPath`. Refuses to
30
- * overwrite an existing file unless `opts.force === true` — the install
31
- * orchestrator uses `skipExisting: true` instead, which returns
32
- * `skipped: true` without touching the file.
33
- */
34
- export function scaffoldPolicyFile(policyPath, opts = {}) {
35
- const force = opts.force === true;
36
- if (existsSync(policyPath)) {
37
- if (opts.skipExisting) {
38
- return { policyPath, schemaVersion: CURRENT_POLICY_SCHEMA_VERSION, bytesWritten: 0, overwritten: false, skipped: true };
39
- }
40
- if (!force)
41
- throw new PolicyFileExistsError(policyPath);
42
- }
43
- const template = readEmbeddedTemplate();
44
- mkdirSync(dirname(policyPath), { recursive: true });
45
- writeFileSync(policyPath, template, { encoding: 'utf-8' });
46
- return {
47
- policyPath,
48
- schemaVersion: CURRENT_POLICY_SCHEMA_VERSION,
49
- bytesWritten: Buffer.byteLength(template, 'utf-8'),
50
- overwritten: force,
51
- };
52
- }
53
- function exitPolicyError(kind, message, extra = {}) {
54
- const code = kind === 'file-not-found' ? 2 : kind === 'yaml-parse' ? 3 : 4;
55
- if (isJsonMode()) {
56
- emitJsonError({ code, kind, message, ...extra });
57
- }
58
- else {
59
- console.error(message);
60
- for (const [k, v] of Object.entries(extra)) {
61
- if (typeof v === 'string')
62
- console.error(` ${k}: ${v}`);
63
- }
64
- }
65
- process.exit(code);
66
- }
67
- function summarizeChangeValue(v) {
68
- if (v === null)
69
- return 'null';
70
- if (v === undefined)
71
- return 'undefined';
72
- if (typeof v === 'string')
73
- return JSON.stringify(v.length > 64 ? `${v.slice(0, 61)}...` : v);
74
- if (typeof v === 'number' || typeof v === 'boolean')
75
- return String(v);
76
- if (Array.isArray(v))
77
- return `[array:${v.length}]`;
78
- if (typeof v === 'object')
79
- return `{object:${Object.keys(v).length}}`;
80
- return String(v);
81
- }
82
- export function registerPolicyCommand(program) {
83
- const policy = program
84
- .command('policy')
85
- .description('Validate, scaffold, and migrate policy.yaml for the OpenClaw SwitchBot skill')
86
- .addHelpText('after', `
87
- The policy file tells an AI agent your device aliases, quiet hours,
88
- audit log path, and which actions always or never need confirmation.
89
- Default location: ${DEFAULT_POLICY_PATH}
90
-
91
- Subcommands:
92
- validate [path] Check a policy file against the embedded schema
93
- new [path] Write a starter policy to the default location (or a given path)
94
- migrate [path] Upgrade a policy file to the latest supported schema
95
- (v${CURRENT_POLICY_SCHEMA_VERSION} → v${LATEST_SUPPORTED_VERSION} today; no-op if already current)
96
- diff <left> <right>
97
- Compare two policy files and print structural + line diff
98
- add-rule Append a rule YAML (from stdin) into automation.rules[]
99
-
100
- Exit codes (validate):
101
- 0 valid
102
- 1 invalid (schema violations)
103
- 2 file not found
104
- 3 YAML parse error
105
- 4 internal error
106
-
107
- Exit codes (migrate):
108
- 0 no-op (already on the target version) or successful migration
109
- 2 file not found
110
- 3 YAML parse error
111
- 6 source version unsupported by this CLI
112
- 7 migration precheck failed (the upgraded file would not validate)
113
-
114
- Examples:
115
- $ switchbot policy validate
116
- $ switchbot policy validate ./policy.yaml
117
- $ switchbot policy validate --json | jq '.data.errors'
118
- $ switchbot policy new
119
- $ switchbot policy new ./policy.yaml --force
120
- $ switchbot policy migrate
121
- $ switchbot policy diff ./policy.before.yaml ./policy.after.yaml
122
- `);
123
- policy
124
- .command('validate [path]')
125
- .description(`Validate a policy.yaml against the embedded v${CURRENT_POLICY_SCHEMA_VERSION} schema`)
126
- .option('--no-color', 'disable ANSI color in human output')
127
- .option('--no-snippet', 'omit the source-line + caret preview')
128
- .action((pathArg, opts) => {
129
- const policyPath = resolvePolicyPath({ flag: pathArg });
130
- let loaded;
131
- try {
132
- loaded = loadPolicyFile(policyPath);
133
- }
134
- catch (err) {
135
- if (err instanceof PolicyFileNotFoundError) {
136
- exitPolicyError('file-not-found', `policy file not found: ${err.policyPath}`, {
137
- hint: `run \`switchbot policy new\` to create one at the default location (${DEFAULT_POLICY_PATH})`,
138
- policyPath: err.policyPath,
139
- });
140
- }
141
- if (err instanceof PolicyYamlParseError) {
142
- exitPolicyError('yaml-parse', `YAML parse error in ${err.policyPath}: ${err.message}`, {
143
- policyPath: err.policyPath,
144
- yamlErrors: err.yamlErrors,
145
- });
146
- }
147
- exitPolicyError('internal', `unexpected error loading policy: ${String(err)}`);
148
- }
149
- const result = validateLoadedPolicy(loaded);
150
- if (isJsonMode()) {
151
- printJson(result);
152
- process.exit(result.valid ? 0 : 1);
153
- }
154
- console.log(formatValidationResult(result, loaded.source, {
155
- color: opts.color !== false,
156
- noSnippet: opts.snippet === false,
157
- }));
158
- process.exit(result.valid ? 0 : 1);
159
- });
160
- policy
161
- .command('new [path]')
162
- .description('Write a starter policy.yaml (fails if the file exists unless --force)')
163
- .option('-f, --force', 'overwrite an existing policy file')
164
- .action((pathArg, opts) => {
165
- const policyPath = resolvePolicyPath({ flag: pathArg });
166
- const force = opts.force === true;
167
- let result;
168
- try {
169
- result = scaffoldPolicyFile(policyPath, { force });
170
- }
171
- catch (err) {
172
- if (err instanceof PolicyFileExistsError) {
173
- const message = err.message;
174
- const hint = 'pass --force to overwrite, or choose a different path';
175
- if (isJsonMode()) {
176
- emitJsonError({ code: 5, kind: 'exists', message, hint, policyPath });
177
- }
178
- else {
179
- console.error(message);
180
- console.error(`hint: ${hint}`);
181
- }
182
- process.exit(5);
183
- }
184
- throw err;
185
- }
186
- if (isJsonMode()) {
187
- printJson(result);
188
- }
189
- else {
190
- console.log(`✓ wrote starter policy to ${result.policyPath}`);
191
- console.log(` schema version: ${result.schemaVersion}`);
192
- console.log(` next steps:`);
193
- console.log(` 1. open the file and fill in the aliases block`);
194
- console.log(` 2. run \`switchbot policy validate\``);
195
- }
196
- });
197
- policy
198
- .command('migrate [path]')
199
- .description(`Upgrade a policy file to the latest supported schema (currently v${LATEST_SUPPORTED_VERSION})`)
200
- .option('--dry-run', 'show what would change without writing the file')
201
- .option('--to <version>', `target schema version (default: ${LATEST_SUPPORTED_VERSION})`, LATEST_SUPPORTED_VERSION)
202
- .action((pathArg, opts) => {
203
- const policyPath = resolvePolicyPath({ flag: pathArg });
204
- let loaded;
205
- try {
206
- loaded = loadPolicyFile(policyPath);
207
- }
208
- catch (err) {
209
- if (err instanceof PolicyFileNotFoundError) {
210
- exitPolicyError('file-not-found', `policy file not found: ${err.policyPath}`, {
211
- hint: 'run `switchbot policy new` first',
212
- policyPath: err.policyPath,
213
- });
214
- }
215
- if (err instanceof PolicyYamlParseError) {
216
- exitPolicyError('yaml-parse', `YAML parse error in ${err.policyPath}: ${err.message}`, {
217
- policyPath: err.policyPath,
218
- });
219
- }
220
- exitPolicyError('internal', `unexpected error loading policy: ${String(err)}`);
221
- }
222
- const data = loaded.data;
223
- const fileVersion = typeof data?.version === 'string' ? data.version : undefined;
224
- const target = opts.to ?? LATEST_SUPPORTED_VERSION;
225
- const basePayload = {
226
- policyPath,
227
- fileVersion,
228
- targetVersion: target,
229
- supportedVersions: SUPPORTED_POLICY_SCHEMA_VERSIONS,
230
- };
231
- if (!fileVersion) {
232
- const message = `policy has no \`version\` field — add \`version: "${CURRENT_POLICY_SCHEMA_VERSION}"\` and run \`switchbot policy validate\``;
233
- const payload = { ...basePayload, status: 'no-version-field', message };
234
- if (isJsonMode())
235
- printJson(payload);
236
- else
237
- console.log(`! ${message}`);
238
- return;
239
- }
240
- if (!SUPPORTED_POLICY_SCHEMA_VERSIONS.includes(fileVersion)) {
241
- const message = `policy schema v${fileVersion} is not supported by this CLI (supports: ${SUPPORTED_POLICY_SCHEMA_VERSIONS.join(', ')})`;
242
- const hint = 'upgrade @switchbot/openapi-cli, or downgrade the policy file to a supported version';
243
- if (isJsonMode())
244
- emitJsonError({ code: 6, kind: 'unsupported-version', ...basePayload, message, hint });
245
- else {
246
- console.error(message);
247
- console.error(`hint: ${hint}`);
248
- }
249
- process.exit(6);
250
- }
251
- if (!SUPPORTED_POLICY_SCHEMA_VERSIONS.includes(target)) {
252
- const message = `--to ${target}: unknown target version (supports: ${SUPPORTED_POLICY_SCHEMA_VERSIONS.join(', ')})`;
253
- if (isJsonMode())
254
- emitJsonError({ code: 6, kind: 'unsupported-target', ...basePayload, message });
255
- else
256
- console.error(message);
257
- process.exit(6);
258
- }
259
- if (fileVersion === target) {
260
- const message = `already on schema v${target}; no migration needed`;
261
- const payload = { ...basePayload, status: 'already-current', message, bytesWritten: 0 };
262
- if (isJsonMode())
263
- printJson(payload);
264
- else
265
- console.log(`✓ ${message}`);
266
- return;
267
- }
268
- let plan;
269
- try {
270
- plan = planMigration(loaded, fileVersion, target);
271
- }
272
- catch (err) {
273
- if (err instanceof PolicyMigrationError) {
274
- const payload = { ...basePayload, status: 'migration-error', kind: err.code, message: err.message };
275
- if (isJsonMode())
276
- emitJsonError({ code: 4, ...payload });
277
- else
278
- console.error(err.message);
279
- process.exit(4);
280
- }
281
- throw err;
282
- }
283
- if (!plan.precheck.valid) {
284
- const message = `migrated policy fails schema v${target} precheck; file not written`;
285
- const payload = {
286
- ...basePayload,
287
- status: 'precheck-failed',
288
- message,
289
- errors: plan.precheck.errors,
290
- };
291
- if (isJsonMode())
292
- emitJsonError({ code: 7, kind: 'migration-precheck-failed', ...payload });
293
- else {
294
- console.error(message);
295
- console.error(formatValidationResult(plan.precheck, plan.nextSource, { color: true }));
296
- console.error('hint: fix the validation errors above in the current file, then re-run `switchbot policy migrate`.');
297
- }
298
- process.exit(7);
299
- }
300
- const bytesWritten = Buffer.byteLength(plan.nextSource, 'utf-8');
301
- const finalPayload = {
302
- ...basePayload,
303
- status: opts.dryRun ? 'dry-run' : 'migrated',
304
- from: plan.fromVersion,
305
- to: plan.toVersion,
306
- bytesWritten: opts.dryRun ? 0 : bytesWritten,
307
- };
308
- if (opts.dryRun) {
309
- if (isJsonMode())
310
- printJson(finalPayload);
311
- else {
312
- console.log(`• dry-run: would upgrade ${policyPath} (v${plan.fromVersion} → v${plan.toVersion})`);
313
- console.log(` bytes: ${bytesWritten}`);
314
- console.log(` precheck: valid against v${target}`);
315
- }
316
- return;
317
- }
318
- writeFileSync(policyPath, plan.nextSource, { encoding: 'utf-8' });
319
- if (isJsonMode())
320
- printJson(finalPayload);
321
- else {
322
- console.log(`✓ migrated ${policyPath} to schema v${plan.toVersion} (from v${plan.fromVersion})`);
323
- console.log(` bytes written: ${bytesWritten}`);
324
- }
325
- });
326
- policy
327
- .command('diff <left> <right>')
328
- .description('Compare two policy files and print structural changes + line diff')
329
- .action((leftPath, rightPath) => {
330
- let leftSource = '';
331
- let rightSource = '';
332
- try {
333
- leftSource = readFileSync(leftPath, 'utf-8');
334
- }
335
- catch (err) {
336
- if (err?.code === 'ENOENT') {
337
- exitPolicyError('file-not-found', `policy file not found: ${leftPath}`, { policyPath: leftPath });
338
- }
339
- exitPolicyError('internal', `failed to read ${leftPath}: ${String(err)}`);
340
- }
341
- try {
342
- rightSource = readFileSync(rightPath, 'utf-8');
343
- }
344
- catch (err) {
345
- if (err?.code === 'ENOENT') {
346
- exitPolicyError('file-not-found', `policy file not found: ${rightPath}`, { policyPath: rightPath });
347
- }
348
- exitPolicyError('internal', `failed to read ${rightPath}: ${String(err)}`);
349
- }
350
- let leftDoc;
351
- let rightDoc;
352
- try {
353
- leftDoc = yamlParse(leftSource);
354
- }
355
- catch (err) {
356
- exitPolicyError('yaml-parse', `YAML parse error in ${leftPath}: ${err.message}`, {
357
- policyPath: leftPath,
358
- });
359
- }
360
- try {
361
- rightDoc = yamlParse(rightSource);
362
- }
363
- catch (err) {
364
- exitPolicyError('yaml-parse', `YAML parse error in ${rightPath}: ${err.message}`, {
365
- policyPath: rightPath,
366
- });
367
- }
368
- const result = diffPolicyValues(leftDoc, rightDoc, leftSource, rightSource);
369
- if (isJsonMode()) {
370
- printJson({
371
- leftPath,
372
- rightPath,
373
- ...result,
374
- });
375
- return;
376
- }
377
- if (result.equal) {
378
- console.log(`✓ no structural differences between ${leftPath} and ${rightPath}`);
379
- return;
380
- }
381
- console.log(`~ policy diff: ${leftPath} -> ${rightPath}`);
382
- console.log(` changes: ${result.changeCount} (added=${result.stats.added}, removed=${result.stats.removed}, changed=${result.stats.changed})`);
383
- if (result.truncated) {
384
- console.log(' note: output truncated at max structural changes');
385
- }
386
- for (const c of result.changes) {
387
- if (c.kind === 'added') {
388
- console.log(` + ${c.path}: ${summarizeChangeValue(c.after)}`);
389
- }
390
- else if (c.kind === 'removed') {
391
- console.log(` - ${c.path}: ${summarizeChangeValue(c.before)}`);
392
- }
393
- else {
394
- console.log(` ~ ${c.path}: ${summarizeChangeValue(c.before)} -> ${summarizeChangeValue(c.after)}`);
395
- }
396
- }
397
- console.log('');
398
- console.log(result.diff);
399
- });
400
- policy
401
- .command('add-rule')
402
- .description('Append a rule (read from stdin) into automation.rules[] in policy.yaml')
403
- .option('--policy <path>', 'Path to policy.yaml (or set $SWITCHBOT_POLICY_PATH)')
404
- .option('--enable', 'Set automation.enabled: true after inserting the rule')
405
- .option('--force', 'Overwrite an existing rule with the same name')
406
- .option('--dry-run', 'Print the diff without writing to disk')
407
- .addHelpText('after', `
408
- Reads rule YAML from stdin. Combine with 'rules suggest' for a full pipeline:
409
-
410
- $ switchbot rules suggest --intent "turn off lights at 10pm" --trigger cron \\
411
- --device <id> | switchbot policy add-rule --dry-run
412
- $ switchbot rules suggest --intent "turn off lights at 10pm" --trigger cron \\
413
- --device <id> | switchbot policy add-rule --enable
414
- `)
415
- .action(async (opts) => {
416
- const policyPath = resolvePolicyPath({ flag: opts.policy });
417
- let ruleYaml;
418
- try {
419
- ruleYaml = await readStdinText();
420
- }
421
- catch (err) {
422
- exitPolicyError('internal', `failed to read stdin: ${err.message}`);
423
- }
424
- if (!ruleYaml.trim()) {
425
- exitPolicyError('internal', 'no rule YAML received on stdin');
426
- }
427
- try {
428
- const result = addRuleToPolicyFile({
429
- ruleYaml: ruleYaml,
430
- policyPath,
431
- enableAutomation: opts.enable,
432
- force: opts.force,
433
- dryRun: opts.dryRun,
434
- });
435
- if (isJsonMode()) {
436
- printJson({
437
- policyPath,
438
- ruleName: result.ruleName,
439
- written: result.written,
440
- diff: result.diff,
441
- });
442
- }
443
- else {
444
- console.log(result.diff);
445
- if (result.written) {
446
- console.log(`✓ rule "${result.ruleName}" added to ${policyPath}`);
447
- }
448
- else {
449
- console.log(`• dry-run: rule "${result.ruleName}" not written`);
450
- }
451
- }
452
- }
453
- catch (err) {
454
- if (err instanceof AddRuleError) {
455
- exitPolicyError('internal', err.message, { kind: err.code });
456
- }
457
- throw err;
458
- }
459
- });
460
- // switchbot policy backup [file]
461
- policy
462
- .command('backup [file]')
463
- .description('Copy the active policy to a backup file (default: <policy>.bak.yaml).')
464
- .option('--force', 'Overwrite an existing backup file.')
465
- .addHelpText('after', `
466
- Creates a point-in-time snapshot of the active policy file so it can be
467
- restored if a migration or manual edit breaks things.
468
-
469
- Default backup path: same directory as the policy, with ".bak.yaml" suffix.
470
-
471
- Examples:
472
- $ switchbot policy backup
473
- $ switchbot policy backup ./my-backup.yaml
474
- $ switchbot policy backup --force
475
- `)
476
- .action((fileArg, opts) => {
477
- const source = resolvePolicyPath({});
478
- if (!existsSync(source)) {
479
- exitPolicyError('file-not-found', `policy file not found: ${source}`, { path: source });
480
- }
481
- const dest = fileArg
482
- ? resolvePath(fileArg)
483
- : source.replace(/\.yaml$/, '.bak.yaml').replace(/\.yml$/, '.bak.yml');
484
- if (!opts.force && existsSync(dest)) {
485
- exitWithError({ code: 2, kind: 'usage', message: `Backup file already exists: ${dest}. Use --force to overwrite.` });
486
- }
487
- try {
488
- copyFileSync(source, dest);
489
- }
490
- catch (err) {
491
- exitPolicyError('internal', `Failed to write backup: ${err instanceof Error ? err.message : String(err)}`, { source, dest });
492
- }
493
- const size = statSync(dest).size;
494
- if (isJsonMode()) {
495
- printJson({ ok: true, source, dest, sizeBytes: size });
496
- }
497
- else {
498
- console.log(`Backup written: ${dest} (${size} bytes)`);
499
- console.log(`Restore with: switchbot policy restore ${dest}`);
500
- }
501
- });
502
- // switchbot policy restore <file>
503
- policy
504
- .command('restore <file>')
505
- .description('Restore a policy backup, validating it before applying.')
506
- .option('--no-validate', 'Skip schema validation before restoring (use if migrating manually).')
507
- .addHelpText('after', `
508
- Validates the backup file against the current schema before overwriting the
509
- active policy. A .pre-restore.bak.yaml snapshot of the current policy is
510
- automatically created before overwriting. Use --no-validate to skip
511
- validation (e.g. when restoring an older version for manual migration).
512
-
513
- Example:
514
- $ switchbot policy restore ./policy.bak.yaml
515
- `)
516
- .action((fileArg, opts) => {
517
- const source = resolvePath(fileArg);
518
- if (!existsSync(source)) {
519
- exitPolicyError('file-not-found', `restore source not found: ${source}`, { path: source });
520
- }
521
- // Validate before touching the active policy.
522
- if (opts.validate !== false) {
523
- let loaded;
524
- try {
525
- loaded = loadPolicyFile(source);
526
- }
527
- catch (err) {
528
- if (err instanceof PolicyFileNotFoundError) {
529
- exitPolicyError('file-not-found', `restore source not found: ${err.policyPath}`, { path: err.policyPath });
530
- }
531
- if (err instanceof PolicyYamlParseError) {
532
- exitPolicyError('yaml-parse', `YAML parse error in ${err.policyPath}: ${err.message}`, { path: err.policyPath });
533
- }
534
- throw err;
535
- }
536
- const vResult = validateLoadedPolicy(loaded);
537
- if (!vResult.valid) {
538
- const firstError = vResult.errors[0]?.message ?? 'schema validation failed';
539
- exitWithError({
540
- code: 1, kind: 'usage',
541
- message: `Backup failed validation: ${firstError}`,
542
- context: { errorCount: vResult.errors.length, hint: 'Use --no-validate to restore anyway.' },
543
- });
544
- }
545
- }
546
- const dest = resolvePolicyPath({});
547
- const destDir = dirname(dest);
548
- if (!existsSync(destDir)) {
549
- mkdirSync(destDir, { recursive: true, mode: 0o700 });
550
- }
551
- // Take an auto-backup of the current policy before overwriting.
552
- if (existsSync(dest)) {
553
- const autoBackup = dest.replace(/\.yaml$/, '.pre-restore.bak.yaml').replace(/\.yml$/, '.pre-restore.bak.yml');
554
- try {
555
- copyFileSync(dest, autoBackup);
556
- }
557
- catch {
558
- // best-effort — if it fails, proceed anyway
559
- }
560
- }
561
- try {
562
- copyFileSync(source, dest);
563
- }
564
- catch (err) {
565
- exitPolicyError('internal', `Failed to restore: ${err instanceof Error ? err.message : String(err)}`, { source, dest });
566
- }
567
- const size = statSync(dest).size;
568
- if (isJsonMode()) {
569
- printJson({ ok: true, restored: dest, from: source, sizeBytes: size });
570
- }
571
- else {
572
- console.log(`Policy restored from: ${source}`);
573
- console.log(`Active policy: ${dest} (${size} bytes)`);
574
- console.log('Run `switchbot policy validate` to confirm the restored file is valid.');
575
- }
576
- });
577
- }
578
- function readStdinText() {
579
- return new Promise((resolve, reject) => {
580
- let buf = '';
581
- process.stdin.setEncoding('utf8');
582
- process.stdin.on('data', (chunk) => (buf += chunk));
583
- process.stdin.on('end', () => resolve(buf));
584
- process.stdin.on('error', reject);
585
- });
586
- }
@@ -1,78 +0,0 @@
1
- import { printJson, isJsonMode } from '../utils/output.js';
2
- import { DAILY_QUOTA, loadQuota, resetQuota, todayUsage, } from '../utils/quota.js';
3
- export function registerQuotaCommand(program) {
4
- const quota = program
5
- .command('quota')
6
- .description('Inspect and manage the local SwitchBot API request counter')
7
- .addHelpText('after', `
8
- Every request the CLI makes is counted locally in ~/.switchbot/quota.json.
9
- Counts are bucketed by local date, one record per endpoint pattern. This
10
- is a best-effort mirror of the SwitchBot 10,000/day limit — it does not
11
- include requests made outside this CLI (mobile app, other scripts).
12
-
13
- Subcommands:
14
- status Show today's usage and the last 7 days (alias: show)
15
- reset Delete the local counter file
16
-
17
- Examples:
18
- $ switchbot quota status
19
- $ switchbot quota show # alias of 'status'
20
- $ switchbot quota status --json
21
- $ switchbot quota reset
22
- `);
23
- quota
24
- .command('status')
25
- .alias('show')
26
- .description("Show today's usage and the last 7 days (alias: show)")
27
- .action(() => {
28
- const usage = todayUsage();
29
- const history = loadQuota();
30
- if (isJsonMode()) {
31
- printJson({
32
- today: {
33
- date: usage.date,
34
- total: usage.total,
35
- remaining: usage.remaining,
36
- dailyLimit: DAILY_QUOTA,
37
- endpoints: usage.endpoints,
38
- },
39
- history: history.days,
40
- });
41
- return;
42
- }
43
- console.log(`Today (${usage.date}):`);
44
- console.log(` Requests used: ${usage.total} / ${DAILY_QUOTA}`);
45
- console.log(` Remaining budget: ${usage.remaining}`);
46
- if (Object.keys(usage.endpoints).length === 0) {
47
- console.log(' (no requests recorded yet)');
48
- }
49
- else {
50
- console.log(' Endpoint breakdown:');
51
- const entries = Object.entries(usage.endpoints).sort((a, b) => b[1] - a[1]);
52
- for (const [endpoint, count] of entries) {
53
- console.log(` ${endpoint.padEnd(48)} ${count}`);
54
- }
55
- }
56
- const otherDays = Object.entries(history.days)
57
- .filter(([d]) => d !== usage.date)
58
- .sort((a, b) => b[0].localeCompare(a[0]));
59
- if (otherDays.length > 0) {
60
- console.log('\nRecent history:');
61
- for (const [date, bucket] of otherDays) {
62
- console.log(` ${date} ${bucket.total}`);
63
- }
64
- }
65
- });
66
- quota
67
- .command('reset')
68
- .description('Delete the local quota counter file')
69
- .action(() => {
70
- resetQuota();
71
- if (isJsonMode()) {
72
- printJson({ reset: true });
73
- }
74
- else {
75
- console.log('Quota counter reset.');
76
- }
77
- });
78
- }