@switchbot/openapi-cli 2.7.2 → 3.1.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 +481 -103
- package/dist/api/client.js +23 -1
- package/dist/commands/agent-bootstrap.js +47 -2
- package/dist/commands/auth.js +354 -0
- package/dist/commands/batch.js +20 -4
- package/dist/commands/capabilities.js +155 -65
- package/dist/commands/config.js +109 -0
- package/dist/commands/daemon.js +367 -0
- package/dist/commands/devices.js +62 -11
- package/dist/commands/doctor.js +417 -8
- package/dist/commands/events.js +3 -3
- package/dist/commands/explain.js +1 -2
- package/dist/commands/health.js +113 -0
- package/dist/commands/install.js +246 -0
- package/dist/commands/mcp.js +888 -7
- package/dist/commands/plan.js +379 -103
- package/dist/commands/policy.js +586 -0
- package/dist/commands/rules.js +875 -0
- package/dist/commands/scenes.js +140 -0
- package/dist/commands/schema.js +0 -2
- package/dist/commands/status-sync.js +131 -0
- package/dist/commands/uninstall.js +237 -0
- package/dist/commands/upgrade-check.js +88 -0
- package/dist/config.js +14 -0
- package/dist/credentials/backends/file.js +101 -0
- package/dist/credentials/backends/linux.js +129 -0
- package/dist/credentials/backends/macos.js +129 -0
- package/dist/credentials/backends/windows.js +215 -0
- package/dist/credentials/keychain.js +88 -0
- package/dist/credentials/prime.js +52 -0
- package/dist/devices/catalog.js +4 -10
- package/dist/index.js +30 -1
- package/dist/install/default-steps.js +257 -0
- package/dist/install/preflight.js +212 -0
- package/dist/install/steps.js +67 -0
- package/dist/lib/command-keywords.js +17 -0
- package/dist/lib/daemon-state.js +46 -0
- package/dist/lib/destructive-mode.js +12 -0
- package/dist/lib/devices.js +1 -1
- package/dist/lib/plan-store.js +68 -0
- package/dist/policy/add-rule.js +124 -0
- package/dist/policy/diff.js +91 -0
- package/dist/policy/examples/policy.example.yaml +99 -0
- package/dist/policy/format.js +57 -0
- package/dist/policy/load.js +61 -0
- package/dist/policy/migrate.js +67 -0
- package/dist/policy/schema/v0.2.json +331 -0
- package/dist/policy/schema.js +18 -0
- package/dist/policy/validate.js +262 -0
- package/dist/rules/action.js +205 -0
- package/dist/rules/audit-query.js +89 -0
- package/dist/rules/conflict-analyzer.js +203 -0
- package/dist/rules/cron-scheduler.js +186 -0
- package/dist/rules/destructive.js +52 -0
- package/dist/rules/engine.js +757 -0
- package/dist/rules/matcher.js +230 -0
- package/dist/rules/pid-file.js +95 -0
- package/dist/rules/quiet-hours.js +45 -0
- package/dist/rules/suggest.js +95 -0
- package/dist/rules/throttle.js +116 -0
- package/dist/rules/types.js +34 -0
- package/dist/rules/webhook-listener.js +223 -0
- package/dist/rules/webhook-token.js +90 -0
- package/dist/status-sync/manager.js +268 -0
- package/dist/utils/audit.js +12 -2
- package/dist/utils/health.js +101 -0
- package/dist/utils/output.js +72 -23
- package/dist/utils/retry.js +81 -0
- package/package.json +12 -4
|
@@ -0,0 +1,586 @@
|
|
|
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
|
+
}
|