@switchbot/openapi-cli 2.7.2 → 3.0.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 +383 -101
- package/dist/commands/agent-bootstrap.js +47 -2
- package/dist/commands/auth.js +354 -0
- package/dist/commands/config.js +30 -0
- package/dist/commands/devices.js +0 -1
- package/dist/commands/doctor.js +184 -7
- package/dist/commands/events.js +3 -3
- package/dist/commands/explain.js +1 -2
- package/dist/commands/install.js +246 -0
- package/dist/commands/mcp.js +796 -3
- package/dist/commands/plan.js +110 -14
- package/dist/commands/policy.js +469 -0
- package/dist/commands/rules.js +657 -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/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 +23 -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/devices.js +0 -1
- 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 +302 -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/cron-scheduler.js +186 -0
- package/dist/rules/destructive.js +52 -0
- package/dist/rules/engine.js +567 -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 +78 -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/package.json +12 -4
|
@@ -5,6 +5,9 @@ import { readProfileMeta } from '../config.js';
|
|
|
5
5
|
import { todayUsage, DAILY_QUOTA } from '../utils/quota.js';
|
|
6
6
|
import { ALL_STRATEGIES } from '../utils/name-resolver.js';
|
|
7
7
|
import { IDENTITY } from './identity.js';
|
|
8
|
+
import { resolvePolicyPath, loadPolicyFile, PolicyFileNotFoundError, PolicyYamlParseError, } from '../policy/load.js';
|
|
9
|
+
import { validateLoadedPolicy } from '../policy/validate.js';
|
|
10
|
+
import { selectCredentialStore } from '../credentials/keychain.js';
|
|
8
11
|
import { createRequire } from 'node:module';
|
|
9
12
|
const require = createRequire(import.meta.url);
|
|
10
13
|
const { version: pkgVersion } = require('../../package.json');
|
|
@@ -28,7 +31,47 @@ const QUICK_REFERENCE = {
|
|
|
28
31
|
observability: ['doctor --json', 'quota status', 'cache status', 'events mqtt-tail'],
|
|
29
32
|
history: ['history range <id> --since 7d', 'history stats <id>'],
|
|
30
33
|
meta: ['devices meta set <id> --alias <name>', 'devices meta list', 'devices meta get <id>'],
|
|
34
|
+
policy: ['policy validate', 'policy new', 'policy migrate'],
|
|
35
|
+
auth: ['auth keychain describe', 'auth keychain migrate', 'auth keychain get'],
|
|
31
36
|
};
|
|
37
|
+
function readPolicyStatus() {
|
|
38
|
+
// Lightweight read — used by the bootstrap payload so agents know whether
|
|
39
|
+
// a policy file exists and is healthy without shelling out to
|
|
40
|
+
// `switchbot policy validate`. Parallel to `checkPolicy` in doctor but
|
|
41
|
+
// returns a more compact shape (no first-error drill-down; agents who
|
|
42
|
+
// want that run the dedicated command).
|
|
43
|
+
const policyPath = resolvePolicyPath();
|
|
44
|
+
try {
|
|
45
|
+
const loaded = loadPolicyFile(policyPath);
|
|
46
|
+
const result = validateLoadedPolicy(loaded);
|
|
47
|
+
return {
|
|
48
|
+
present: true,
|
|
49
|
+
valid: result.valid,
|
|
50
|
+
path: policyPath,
|
|
51
|
+
schemaVersion: result.schemaVersion,
|
|
52
|
+
errorCount: result.valid ? 0 : result.errors.length,
|
|
53
|
+
};
|
|
54
|
+
}
|
|
55
|
+
catch (err) {
|
|
56
|
+
if (err instanceof PolicyFileNotFoundError) {
|
|
57
|
+
return { present: false, valid: null, path: policyPath };
|
|
58
|
+
}
|
|
59
|
+
if (err instanceof PolicyYamlParseError) {
|
|
60
|
+
return { present: true, valid: false, path: policyPath, errorCount: 1 };
|
|
61
|
+
}
|
|
62
|
+
return { present: false, valid: null, path: policyPath };
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
async function readCredentialsBackend() {
|
|
66
|
+
try {
|
|
67
|
+
const store = await selectCredentialStore();
|
|
68
|
+
const desc = store.describe();
|
|
69
|
+
return { name: store.name, label: desc.backend, writable: desc.writable };
|
|
70
|
+
}
|
|
71
|
+
catch {
|
|
72
|
+
return { name: 'file', label: 'File (~/.switchbot/config.json)', writable: true };
|
|
73
|
+
}
|
|
74
|
+
}
|
|
32
75
|
export function registerAgentBootstrapCommand(program) {
|
|
33
76
|
program
|
|
34
77
|
.command('agent-bootstrap')
|
|
@@ -49,12 +92,13 @@ Examples:
|
|
|
49
92
|
$ switchbot agent-bootstrap | jq '.devices | length'
|
|
50
93
|
$ switchbot agent-bootstrap --compact | jq '.quickReference'
|
|
51
94
|
`)
|
|
52
|
-
.action((opts) => {
|
|
95
|
+
.action(async (opts) => {
|
|
53
96
|
const compact = Boolean(opts.compact);
|
|
54
97
|
const cache = loadCache();
|
|
55
98
|
const catalog = getEffectiveCatalog();
|
|
56
99
|
const usage = todayUsage();
|
|
57
100
|
const meta = readProfileMeta(undefined);
|
|
101
|
+
const credentialsBackend = await readCredentialsBackend();
|
|
58
102
|
const cachedDevices = cache
|
|
59
103
|
? Object.entries(cache.devices).map(([id, d]) => ({
|
|
60
104
|
deviceId: id,
|
|
@@ -91,7 +135,6 @@ Examples:
|
|
|
91
135
|
command: c.command,
|
|
92
136
|
parameter: c.parameter,
|
|
93
137
|
safetyTier: tier,
|
|
94
|
-
destructive: tier === 'destructive',
|
|
95
138
|
idempotent: Boolean(c.idempotent),
|
|
96
139
|
};
|
|
97
140
|
}),
|
|
@@ -120,6 +163,8 @@ Examples:
|
|
|
120
163
|
remaining: usage.remaining,
|
|
121
164
|
dailyLimit: DAILY_QUOTA,
|
|
122
165
|
},
|
|
166
|
+
policyStatus: readPolicyStatus(),
|
|
167
|
+
credentialsBackend,
|
|
123
168
|
devices: cachedDevices,
|
|
124
169
|
catalog: {
|
|
125
170
|
scope: cachedDevices.length > 0 ? 'used' : 'all',
|
|
@@ -0,0 +1,354 @@
|
|
|
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
|
+
}
|
package/dist/commands/config.js
CHANGED
|
@@ -89,6 +89,36 @@ async function promptSecret(question) {
|
|
|
89
89
|
void mutableStdout;
|
|
90
90
|
});
|
|
91
91
|
}
|
|
92
|
+
/**
|
|
93
|
+
* Interactive echo-off prompt for token + secret. Used by both
|
|
94
|
+
* `switchbot config set-token` and the install orchestrator. Throws if
|
|
95
|
+
* stdin is not a TTY.
|
|
96
|
+
*/
|
|
97
|
+
export async function promptTokenAndSecret() {
|
|
98
|
+
if (!process.stdin.isTTY) {
|
|
99
|
+
throw new Error('interactive prompt requires a TTY');
|
|
100
|
+
}
|
|
101
|
+
const token = (await promptSecret('Token: ')).trim();
|
|
102
|
+
const secret = (await promptSecret('Secret: ')).trim();
|
|
103
|
+
if (!token || !secret) {
|
|
104
|
+
throw new Error('token and secret are both required');
|
|
105
|
+
}
|
|
106
|
+
return { token, secret };
|
|
107
|
+
}
|
|
108
|
+
/**
|
|
109
|
+
* Read a two-line credential file (line 1 = token, line 2 = secret)
|
|
110
|
+
* and unlink it on success. The installer's `--token-file` escape
|
|
111
|
+
* hatch uses this; keeps credentials off the command line and shell
|
|
112
|
+
* history for CI-style installs.
|
|
113
|
+
*/
|
|
114
|
+
export function readCredentialsFile(filePath) {
|
|
115
|
+
const raw = fs.readFileSync(filePath, 'utf-8');
|
|
116
|
+
const lines = raw.split(/\r?\n/).filter((l) => l.length > 0);
|
|
117
|
+
if (lines.length < 2) {
|
|
118
|
+
throw new Error(`credential file ${filePath} must contain two lines: token, then secret`);
|
|
119
|
+
}
|
|
120
|
+
return { token: lines[0].trim(), secret: lines[1].trim() };
|
|
121
|
+
}
|
|
92
122
|
export function registerConfigCommand(program) {
|
|
93
123
|
const config = program
|
|
94
124
|
.command('config')
|