@hex4c59/pi-ext-config-sync 0.0.0-stage → 0.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/CHANGELOG.md +22 -0
- package/LICENSE +21 -0
- package/README.md +223 -2
- package/extensions/config-sync.ts +163 -0
- package/package.json +46 -4
- package/src/collect.ts +273 -0
- package/src/crypto.ts +229 -0
- package/src/files.ts +430 -0
- package/src/github.ts +664 -0
- package/src/hash.ts +22 -0
- package/src/operations.ts +829 -0
- package/src/plan.ts +123 -0
- package/src/secret-input.ts +89 -0
- package/src/types.ts +121 -0
|
@@ -0,0 +1,829 @@
|
|
|
1
|
+
import { rm } from "node:fs/promises";
|
|
2
|
+
import {
|
|
3
|
+
backupLocalFiles,
|
|
4
|
+
errorMessage,
|
|
5
|
+
isRecord,
|
|
6
|
+
loadConfig,
|
|
7
|
+
loadState,
|
|
8
|
+
normalizeRelativePath,
|
|
9
|
+
readAuthObject,
|
|
10
|
+
readSyncToken,
|
|
11
|
+
readTextFile,
|
|
12
|
+
remoteToLocalPath,
|
|
13
|
+
saveConfig,
|
|
14
|
+
saveState,
|
|
15
|
+
storeSyncToken,
|
|
16
|
+
syncDir,
|
|
17
|
+
updateAuthEntries,
|
|
18
|
+
writeBytesFile,
|
|
19
|
+
} from "./files.ts";
|
|
20
|
+
import { collectAgentFiles, collectLocalHashes, mergeSettings } from "./collect.ts";
|
|
21
|
+
import {
|
|
22
|
+
authEntriesEqual,
|
|
23
|
+
decryptJson,
|
|
24
|
+
encryptJson,
|
|
25
|
+
filterAuthEntries,
|
|
26
|
+
mergeAuthEntries,
|
|
27
|
+
validatePassphrase,
|
|
28
|
+
} from "./crypto.ts";
|
|
29
|
+
import * as github from "./github.ts";
|
|
30
|
+
import { sha256Hex } from "./hash.ts";
|
|
31
|
+
import { planPull, planPush, takeRemoteForConflicts } from "./plan.ts";
|
|
32
|
+
import {
|
|
33
|
+
AGENT_PATH_PREFIX,
|
|
34
|
+
MANIFEST_PATH,
|
|
35
|
+
MANIFEST_TOOL,
|
|
36
|
+
SECRETS_PATH,
|
|
37
|
+
SyncError,
|
|
38
|
+
} from "./types.ts";
|
|
39
|
+
import type {
|
|
40
|
+
PullResult,
|
|
41
|
+
PushResult,
|
|
42
|
+
RemoteFile,
|
|
43
|
+
RemoteSnapshot,
|
|
44
|
+
SyncConfig,
|
|
45
|
+
SyncManifest,
|
|
46
|
+
SyncState,
|
|
47
|
+
SyncUi,
|
|
48
|
+
} from "./types.ts";
|
|
49
|
+
|
|
50
|
+
const SETTINGS_REMOTE_PATH = `${AGENT_PATH_PREFIX}settings.json`;
|
|
51
|
+
const PULL_CONFLICT_OPTIONS = [
|
|
52
|
+
"Use remote for all",
|
|
53
|
+
"Keep local for all",
|
|
54
|
+
"Choose per file",
|
|
55
|
+
"Cancel",
|
|
56
|
+
];
|
|
57
|
+
|
|
58
|
+
export interface PullOptions {
|
|
59
|
+
force?: boolean;
|
|
60
|
+
silent?: boolean;
|
|
61
|
+
allowSecrets?: boolean;
|
|
62
|
+
quiet?: boolean;
|
|
63
|
+
signal?: AbortSignal;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
export interface PushOptions {
|
|
67
|
+
force?: boolean;
|
|
68
|
+
yes?: boolean;
|
|
69
|
+
signal?: AbortSignal;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
interface Target {
|
|
73
|
+
owner: string;
|
|
74
|
+
name: string;
|
|
75
|
+
repo: github.RepoInfo | null;
|
|
76
|
+
branch: string;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
interface SecretResult {
|
|
80
|
+
applied: boolean;
|
|
81
|
+
skipped: boolean;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
interface SecretBuild {
|
|
85
|
+
bytes: Buffer | undefined;
|
|
86
|
+
changed: boolean;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
let cachedPassphrase: string | null = null;
|
|
90
|
+
|
|
91
|
+
export function clearPassphraseCache(): void {
|
|
92
|
+
cachedPassphrase = null;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
export async function runLogin(ui: SyncUi, agentDir: string): Promise<void> {
|
|
96
|
+
const config = await loadConfig(agentDir);
|
|
97
|
+
const clientId = config.clientId.trim();
|
|
98
|
+
if (clientId.length === 0) {
|
|
99
|
+
throw new SyncError("No OAuth client ID configured. Run /sync setup <client-id> first.");
|
|
100
|
+
}
|
|
101
|
+
const device = await github.requestDeviceCode(clientId);
|
|
102
|
+
ui.copyText(device.userCode);
|
|
103
|
+
ui.notify(
|
|
104
|
+
`Open ${device.verificationUri} and enter code ${device.userCode}. `
|
|
105
|
+
+ "The code was copied to the clipboard. Waiting for authorization...",
|
|
106
|
+
"info",
|
|
107
|
+
);
|
|
108
|
+
ui.setStatus("config-sync", "Waiting for GitHub authorization...");
|
|
109
|
+
try {
|
|
110
|
+
const token = await github.pollDeviceToken(clientId, device, () => undefined);
|
|
111
|
+
const viewer = await github.getViewer(token);
|
|
112
|
+
const { state, warning } = await loadState(agentDir);
|
|
113
|
+
if (warning !== undefined) ui.notify(warning, "warning");
|
|
114
|
+
if (state.login.length > 0 && state.login !== viewer.login) state.base = null;
|
|
115
|
+
state.login = viewer.login;
|
|
116
|
+
state.scopes = viewer.scopes;
|
|
117
|
+
await saveState(agentDir, state);
|
|
118
|
+
await storeSyncToken(agentDir, token);
|
|
119
|
+
if (viewer.scopes.length > 0 && !viewer.scopes.includes("repo")) {
|
|
120
|
+
ui.notify(
|
|
121
|
+
`Signed in as ${viewer.login}, but the token scopes (${viewer.scopes.join(", ")}) `
|
|
122
|
+
+ "do not include repo. Private repositories may be inaccessible.",
|
|
123
|
+
"warning",
|
|
124
|
+
);
|
|
125
|
+
} else {
|
|
126
|
+
ui.notify(`Signed in as ${viewer.login}.`, "info");
|
|
127
|
+
}
|
|
128
|
+
} finally {
|
|
129
|
+
ui.setStatus("config-sync", undefined);
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
export async function runLogout(ui: SyncUi, agentDir: string): Promise<void> {
|
|
134
|
+
const token = await readSyncToken(agentDir);
|
|
135
|
+
if (token === null) {
|
|
136
|
+
ui.notify("Not signed in.", "info");
|
|
137
|
+
return;
|
|
138
|
+
}
|
|
139
|
+
const ok = await ui.confirm(
|
|
140
|
+
"Log out",
|
|
141
|
+
"Remove the GitHub token from auth.json? Synced repository contents and backups are kept.",
|
|
142
|
+
);
|
|
143
|
+
if (!ok) return;
|
|
144
|
+
await storeSyncToken(agentDir, null);
|
|
145
|
+
const { state } = await loadState(agentDir);
|
|
146
|
+
state.login = "";
|
|
147
|
+
state.scopes = [];
|
|
148
|
+
await saveState(agentDir, state);
|
|
149
|
+
clearPassphraseCache();
|
|
150
|
+
ui.notify("Signed out of GitHub.", "info");
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
export async function runSetup(
|
|
154
|
+
ui: SyncUi,
|
|
155
|
+
agentDir: string,
|
|
156
|
+
clientIdArg: string,
|
|
157
|
+
): Promise<void> {
|
|
158
|
+
const answer = clientIdArg.trim().length > 0
|
|
159
|
+
? clientIdArg.trim()
|
|
160
|
+
: await ui.askText("GitHub OAuth App client ID");
|
|
161
|
+
if (answer === undefined || answer.trim().length === 0) {
|
|
162
|
+
ui.notify("Setup cancelled.", "info");
|
|
163
|
+
return;
|
|
164
|
+
}
|
|
165
|
+
const clientId = answer.trim();
|
|
166
|
+
if (!/^[A-Za-z0-9_]{8,128}$/.test(clientId)) {
|
|
167
|
+
throw new SyncError("GitHub OAuth client IDs contain only letters, digits and underscores.");
|
|
168
|
+
}
|
|
169
|
+
const config = await loadConfig(agentDir);
|
|
170
|
+
config.clientId = clientId;
|
|
171
|
+
await saveConfig(agentDir, config);
|
|
172
|
+
ui.notify("OAuth client ID saved. Run /sync login to authorize this device.", "info");
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
export async function runSetRepo(
|
|
176
|
+
ui: SyncUi,
|
|
177
|
+
agentDir: string,
|
|
178
|
+
repoArg: string,
|
|
179
|
+
): Promise<void> {
|
|
180
|
+
const answer = repoArg.trim().length > 0
|
|
181
|
+
? repoArg.trim()
|
|
182
|
+
: await ui.askText("Repository (owner/name), or 'default' to clear");
|
|
183
|
+
if (answer === undefined || answer.trim().length === 0) {
|
|
184
|
+
ui.notify("Repository unchanged.", "info");
|
|
185
|
+
return;
|
|
186
|
+
}
|
|
187
|
+
const config = await loadConfig(agentDir);
|
|
188
|
+
if (answer.trim() === "default") {
|
|
189
|
+
config.repo = "";
|
|
190
|
+
} else {
|
|
191
|
+
const { owner, name } = github.splitRepo(answer.trim());
|
|
192
|
+
config.repo = `${owner}/${name}`;
|
|
193
|
+
}
|
|
194
|
+
await saveConfig(agentDir, config);
|
|
195
|
+
await resetBase(agentDir);
|
|
196
|
+
ui.notify(
|
|
197
|
+
config.repo.length === 0
|
|
198
|
+
? "Repository reset to the default <login>/pi-config."
|
|
199
|
+
: `Repository set to ${config.repo}.`,
|
|
200
|
+
"info",
|
|
201
|
+
);
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
export async function runSetBranch(
|
|
205
|
+
ui: SyncUi,
|
|
206
|
+
agentDir: string,
|
|
207
|
+
branchArg: string,
|
|
208
|
+
): Promise<void> {
|
|
209
|
+
const answer = branchArg.trim().length > 0
|
|
210
|
+
? branchArg.trim()
|
|
211
|
+
: await ui.askText("Branch name, or 'default' to use the repository default");
|
|
212
|
+
if (answer === undefined || answer.trim().length === 0) {
|
|
213
|
+
ui.notify("Branch unchanged.", "info");
|
|
214
|
+
return;
|
|
215
|
+
}
|
|
216
|
+
const config = await loadConfig(agentDir);
|
|
217
|
+
config.branch = answer.trim() === "default" ? "" : github.assertSafeBranch(answer.trim());
|
|
218
|
+
await saveConfig(agentDir, config);
|
|
219
|
+
await resetBase(agentDir);
|
|
220
|
+
ui.notify(
|
|
221
|
+
config.branch.length === 0
|
|
222
|
+
? "Branch reset to the repository default."
|
|
223
|
+
: `Branch set to ${config.branch}.`,
|
|
224
|
+
"info",
|
|
225
|
+
);
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
export async function runSetAuto(
|
|
229
|
+
ui: SyncUi,
|
|
230
|
+
agentDir: string,
|
|
231
|
+
valueArg: string,
|
|
232
|
+
): Promise<void> {
|
|
233
|
+
const value = valueArg.trim().toLowerCase();
|
|
234
|
+
if (value !== "on" && value !== "off") {
|
|
235
|
+
throw new SyncError("Usage: /sync auto on|off");
|
|
236
|
+
}
|
|
237
|
+
const config = await loadConfig(agentDir);
|
|
238
|
+
config.autoPull = value === "on";
|
|
239
|
+
await saveConfig(agentDir, config);
|
|
240
|
+
ui.notify(`Automatic pull on startup is ${config.autoPull ? "on" : "off"}.`, "info");
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
export async function runStatus(ui: SyncUi, agentDir: string): Promise<void> {
|
|
244
|
+
const config = await loadConfig(agentDir);
|
|
245
|
+
const { state, warning } = await loadState(agentDir);
|
|
246
|
+
if (warning !== undefined) ui.notify(warning, "warning");
|
|
247
|
+
const token = await readSyncToken(agentDir);
|
|
248
|
+
const repo = config.repo.length > 0
|
|
249
|
+
? config.repo
|
|
250
|
+
: state.login.length > 0
|
|
251
|
+
? `${state.login}/pi-config`
|
|
252
|
+
: "(not configured)";
|
|
253
|
+
const last = state.lastSync;
|
|
254
|
+
const lines = [
|
|
255
|
+
`GitHub: ${token === null ? "not signed in" : `signed in${state.login ? ` as ${state.login}` : ""}`}`,
|
|
256
|
+
`Repository: ${repo}`,
|
|
257
|
+
`Branch: ${config.branch.length > 0 ? config.branch : "(repository default)"}`,
|
|
258
|
+
`Auto pull: ${config.autoPull ? "on" : "off"}`,
|
|
259
|
+
`Credential filter: ${config.includeAuthProviders.length > 0
|
|
260
|
+
? `only ${config.includeAuthProviders.join(", ")}`
|
|
261
|
+
: "API-key providers only"}`,
|
|
262
|
+
`Last sync: ${last === null
|
|
263
|
+
? "never"
|
|
264
|
+
: `${last.action} at ${last.at} (${last.files} files, commit ${shortSha(last.commit)})`}`,
|
|
265
|
+
`Config: ${syncDir(agentDir)}`,
|
|
266
|
+
];
|
|
267
|
+
ui.notify(lines.join("\n"), "info");
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
export async function runPull(
|
|
271
|
+
ui: SyncUi,
|
|
272
|
+
agentDir: string,
|
|
273
|
+
options: PullOptions = {},
|
|
274
|
+
): Promise<PullResult> {
|
|
275
|
+
const config = await loadConfig(agentDir);
|
|
276
|
+
const token = await readSyncToken(agentDir);
|
|
277
|
+
if (token === null) throw new SyncError("Not signed in. Run /sync login first.");
|
|
278
|
+
const { state, warning } = await loadState(agentDir);
|
|
279
|
+
if (warning !== undefined) ui.notify(warning, "warning");
|
|
280
|
+
const target = await resolveTarget(token, config, state, options.signal);
|
|
281
|
+
if (target.repo === null) {
|
|
282
|
+
if (!options.quiet) {
|
|
283
|
+
ui.notify(
|
|
284
|
+
`Repository ${target.owner}/${target.name} does not exist yet. `
|
|
285
|
+
+ "Push from a configured device first.",
|
|
286
|
+
"warning",
|
|
287
|
+
);
|
|
288
|
+
}
|
|
289
|
+
return emptyPullResult();
|
|
290
|
+
}
|
|
291
|
+
const snapshot = await github.fetchSnapshot(
|
|
292
|
+
token,
|
|
293
|
+
target.owner,
|
|
294
|
+
target.name,
|
|
295
|
+
target.branch,
|
|
296
|
+
options.signal,
|
|
297
|
+
);
|
|
298
|
+
if (snapshot.foreign) {
|
|
299
|
+
throw new SyncError(
|
|
300
|
+
`Repository ${target.owner}/${target.name} is not managed by config-sync; refusing to pull.`,
|
|
301
|
+
);
|
|
302
|
+
}
|
|
303
|
+
if (snapshot.commit.length === 0) {
|
|
304
|
+
if (!options.quiet) {
|
|
305
|
+
ui.notify(
|
|
306
|
+
`Repository ${target.owner}/${target.name} has no commits on ${target.branch} yet.`,
|
|
307
|
+
"info",
|
|
308
|
+
);
|
|
309
|
+
}
|
|
310
|
+
return emptyPullResult();
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
const remoteHashes = remoteHashMap(snapshot.files);
|
|
314
|
+
const localHashes = await collectLocalHashes(agentDir, config.localSettingsKeys);
|
|
315
|
+
const previousBase = state.base?.hashes ?? {};
|
|
316
|
+
const plan = planPull(withoutSecrets(previousBase), localHashes, withoutSecrets(remoteHashes));
|
|
317
|
+
const conflictCount = plan.conflicts.length;
|
|
318
|
+
if (conflictCount > 0) {
|
|
319
|
+
if (options.force) {
|
|
320
|
+
takeRemoteForConflicts(plan, remoteHashes, plan.conflicts);
|
|
321
|
+
} else if (options.silent) {
|
|
322
|
+
throw new SyncError(
|
|
323
|
+
`${conflictCount} file(s) changed both locally and remotely: `
|
|
324
|
+
+ `${previewPaths(plan.conflicts)}. Run /sync pull to resolve them.`,
|
|
325
|
+
);
|
|
326
|
+
} else {
|
|
327
|
+
const useRemote = await resolvePullConflicts(ui, plan.conflicts);
|
|
328
|
+
if (useRemote === undefined) throw new SyncError("Pull cancelled.");
|
|
329
|
+
takeRemoteForConflicts(plan, remoteHashes, useRemote);
|
|
330
|
+
}
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
const settingsText = await prepareMergedSettings(ui, agentDir, config, snapshot, plan.write);
|
|
334
|
+
const backupPaths = [...new Set([...plan.write, ...plan.delete].map(agentRelative))].sort();
|
|
335
|
+
const backup = await backupLocalFiles(agentDir, backupPaths);
|
|
336
|
+
let changed = 0;
|
|
337
|
+
for (const remotePath of plan.write) {
|
|
338
|
+
const file = snapshot.files.get(remotePath);
|
|
339
|
+
if (file === undefined) throw new SyncError(`Remote file disappeared during pull: ${remotePath}`);
|
|
340
|
+
const localPath = remoteToLocalPath(agentDir, remotePath);
|
|
341
|
+
if (remotePath === SETTINGS_REMOTE_PATH) {
|
|
342
|
+
if (settingsText === undefined) {
|
|
343
|
+
throw new SyncError("Remote settings.json could not be merged.");
|
|
344
|
+
}
|
|
345
|
+
await writeBytesFile(localPath, Buffer.from(settingsText, "utf8"));
|
|
346
|
+
} else {
|
|
347
|
+
await writeBytesFile(localPath, file.bytes);
|
|
348
|
+
}
|
|
349
|
+
changed++;
|
|
350
|
+
}
|
|
351
|
+
let deleted = 0;
|
|
352
|
+
for (const remotePath of plan.delete) {
|
|
353
|
+
await rm(remoteToLocalPath(agentDir, remotePath), { force: true, maxRetries: 3 });
|
|
354
|
+
deleted++;
|
|
355
|
+
}
|
|
356
|
+
|
|
357
|
+
const remoteSecrets = snapshot.files.get(SECRETS_PATH);
|
|
358
|
+
const baseSecrets = previousBase[SECRETS_PATH];
|
|
359
|
+
const secretResult = options.allowSecrets === false
|
|
360
|
+
? {
|
|
361
|
+
applied: false,
|
|
362
|
+
skipped: remoteSecrets !== undefined && remoteSecrets.sha256 !== baseSecrets,
|
|
363
|
+
}
|
|
364
|
+
: await applyRemoteSecrets(ui, agentDir, snapshot, previousBase, options);
|
|
365
|
+
|
|
366
|
+
const nextBaseHashes = { ...remoteHashes };
|
|
367
|
+
if (secretResult.skipped && remoteSecrets !== undefined) {
|
|
368
|
+
if (baseSecrets === undefined) delete nextBaseHashes[SECRETS_PATH];
|
|
369
|
+
else nextBaseHashes[SECRETS_PATH] = baseSecrets;
|
|
370
|
+
}
|
|
371
|
+
state.base = { commit: snapshot.commit, hashes: nextBaseHashes };
|
|
372
|
+
state.lastSync = {
|
|
373
|
+
action: "pull",
|
|
374
|
+
at: new Date().toISOString(),
|
|
375
|
+
commit: snapshot.commit,
|
|
376
|
+
files: changed + deleted,
|
|
377
|
+
conflicts: conflictCount,
|
|
378
|
+
};
|
|
379
|
+
await saveState(agentDir, state);
|
|
380
|
+
|
|
381
|
+
if (!options.quiet) {
|
|
382
|
+
const parts = [
|
|
383
|
+
`Pulled ${changed} file(s) and deleted ${deleted} file(s) from `
|
|
384
|
+
+ `${target.owner}/${target.name}@${target.branch}.`,
|
|
385
|
+
];
|
|
386
|
+
if (conflictCount > 0) parts.push(`${conflictCount} conflict(s) handled.`);
|
|
387
|
+
if (backup !== null) parts.push(`Backup: ${backup}`);
|
|
388
|
+
if (secretResult.applied) parts.push("Updated credentials in auth.json.");
|
|
389
|
+
if (secretResult.skipped) {
|
|
390
|
+
parts.push("Credentials were not updated; run /sync pull again to retry.");
|
|
391
|
+
}
|
|
392
|
+
ui.notify(parts.join(" "), secretResult.skipped ? "warning" : "info");
|
|
393
|
+
}
|
|
394
|
+
|
|
395
|
+
return {
|
|
396
|
+
changed,
|
|
397
|
+
deleted,
|
|
398
|
+
conflicts: conflictCount,
|
|
399
|
+
secretsApplied: secretResult.applied,
|
|
400
|
+
secretsSkipped: secretResult.skipped,
|
|
401
|
+
commit: snapshot.commit,
|
|
402
|
+
};
|
|
403
|
+
}
|
|
404
|
+
|
|
405
|
+
export async function runPush(
|
|
406
|
+
ui: SyncUi,
|
|
407
|
+
agentDir: string,
|
|
408
|
+
options: PushOptions = {},
|
|
409
|
+
): Promise<PushResult> {
|
|
410
|
+
const config = await loadConfig(agentDir);
|
|
411
|
+
const token = await readSyncToken(agentDir);
|
|
412
|
+
if (token === null) throw new SyncError("Not signed in. Run /sync login first.");
|
|
413
|
+
const { state, warning } = await loadState(agentDir);
|
|
414
|
+
if (warning !== undefined) ui.notify(warning, "warning");
|
|
415
|
+
const target = await resolveTarget(token, config, state, options.signal);
|
|
416
|
+
let owner = target.owner;
|
|
417
|
+
let repo = target.repo;
|
|
418
|
+
if (repo === null) {
|
|
419
|
+
const viewer = await github.getViewer(token, options.signal);
|
|
420
|
+
if (state.login.length > 0 && state.login !== viewer.login) state.base = null;
|
|
421
|
+
state.login = viewer.login;
|
|
422
|
+
state.scopes = viewer.scopes;
|
|
423
|
+
if (target.owner.toLowerCase() !== viewer.login.toLowerCase()) {
|
|
424
|
+
throw new SyncError(
|
|
425
|
+
`Repository ${target.owner}/${target.name} does not exist, and it is not owned by `
|
|
426
|
+
+ `${viewer.login}. Create it on GitHub first.`,
|
|
427
|
+
);
|
|
428
|
+
}
|
|
429
|
+
if (!options.yes) {
|
|
430
|
+
const ok = await ui.confirm(
|
|
431
|
+
"Create repository",
|
|
432
|
+
`Create private repository ${viewer.login}/${target.name}?`,
|
|
433
|
+
);
|
|
434
|
+
if (!ok) throw new SyncError("Push cancelled.");
|
|
435
|
+
}
|
|
436
|
+
repo = await github.createRepo(token, target.name, options.signal);
|
|
437
|
+
owner = viewer.login;
|
|
438
|
+
}
|
|
439
|
+
if (!repo.private && !config.allowPublicRepo) {
|
|
440
|
+
throw new SyncError(
|
|
441
|
+
`Repository ${repo.fullName} is public. config-sync only pushes to private repositories.`,
|
|
442
|
+
);
|
|
443
|
+
}
|
|
444
|
+
const branch = github.assertSafeBranch(config.branch || repo.defaultBranch);
|
|
445
|
+
const repoName = repo.name;
|
|
446
|
+
let snapshot = await github.fetchSnapshot(
|
|
447
|
+
token,
|
|
448
|
+
owner,
|
|
449
|
+
repoName,
|
|
450
|
+
branch,
|
|
451
|
+
options.signal,
|
|
452
|
+
);
|
|
453
|
+
const initializedRepo = snapshot.commit.length === 0;
|
|
454
|
+
if (initializedRepo) {
|
|
455
|
+
await github.initializeRepo(token, owner, repoName, branch, options.signal);
|
|
456
|
+
snapshot = await github.fetchSnapshot(token, owner, repoName, branch, options.signal);
|
|
457
|
+
}
|
|
458
|
+
if (snapshot.foreign && !options.yes && !initializedRepo) {
|
|
459
|
+
const ok = await ui.confirm(
|
|
460
|
+
"Replace repository contents",
|
|
461
|
+
`Repository ${owner}/${repoName} contains files that were not written by config-sync. `
|
|
462
|
+
+ "Pushing replaces its entire file tree. Continue?",
|
|
463
|
+
);
|
|
464
|
+
if (!ok) throw new SyncError("Push cancelled.");
|
|
465
|
+
}
|
|
466
|
+
|
|
467
|
+
const collected = await collectAgentFiles(agentDir, {
|
|
468
|
+
localSettingsKeys: config.localSettingsKeys,
|
|
469
|
+
auditPlaintext: true,
|
|
470
|
+
});
|
|
471
|
+
const localHashes: Record<string, string> = {};
|
|
472
|
+
for (const file of collected.files) localHashes[file.remotePath] = file.sha256;
|
|
473
|
+
const remoteHashes = initializedRepo ? {} : remoteHashMap(snapshot.files);
|
|
474
|
+
const plan = planPush(
|
|
475
|
+
withoutSecrets(state.base?.hashes ?? {}),
|
|
476
|
+
localHashes,
|
|
477
|
+
withoutSecrets(remoteHashes),
|
|
478
|
+
);
|
|
479
|
+
if (plan.conflicts.length > 0 && !options.force) {
|
|
480
|
+
throw new SyncError(
|
|
481
|
+
`${plan.conflicts.length} remote file(s) changed since the last pull: `
|
|
482
|
+
+ `${previewPaths(plan.conflicts)}. Run /sync pull first, or /sync push --force to overwrite them.`,
|
|
483
|
+
);
|
|
484
|
+
}
|
|
485
|
+
|
|
486
|
+
const secrets = await buildSecrets(ui, agentDir, config, snapshot);
|
|
487
|
+
const upToDate = snapshot.manifest !== null
|
|
488
|
+
&& plan.write.length === 0
|
|
489
|
+
&& plan.delete.length === 0
|
|
490
|
+
&& plan.conflicts.length === 0
|
|
491
|
+
&& !secrets.changed;
|
|
492
|
+
if (upToDate) {
|
|
493
|
+
ui.notify(`Already up to date with ${owner}/${repoName}@${branch}.`, "info");
|
|
494
|
+
return { files: 0, commit: snapshot.commit, conflicts: 0, forced: false };
|
|
495
|
+
}
|
|
496
|
+
|
|
497
|
+
const files = collected.files
|
|
498
|
+
.map((file) => ({ path: file.remotePath, bytes: file.bytes }))
|
|
499
|
+
.sort((left, right) => left.path.localeCompare(right.path));
|
|
500
|
+
if (secrets.bytes !== undefined) files.push({ path: SECRETS_PATH, bytes: secrets.bytes });
|
|
501
|
+
files.sort((left, right) => left.path.localeCompare(right.path));
|
|
502
|
+
const manifest = buildManifest(files, state.deviceId);
|
|
503
|
+
files.push({ path: MANIFEST_PATH, bytes: manifest.manifestBytes });
|
|
504
|
+
const syncedCount = collected.files.length + (secrets.bytes === undefined ? 0 : 1);
|
|
505
|
+
|
|
506
|
+
if (!options.yes) {
|
|
507
|
+
const lines = [
|
|
508
|
+
`Push ${syncedCount} file(s) to ${owner}/${repoName}@${branch}?`,
|
|
509
|
+
];
|
|
510
|
+
if (secrets.bytes !== undefined) lines.push("Encrypted credentials are included.");
|
|
511
|
+
if (plan.delete.length > 0) {
|
|
512
|
+
lines.push(`${plan.delete.length} remote file(s) will be deleted.`);
|
|
513
|
+
}
|
|
514
|
+
if (plan.conflicts.length > 0) {
|
|
515
|
+
lines.push(`${plan.conflicts.length} remote change(s) will be overwritten.`);
|
|
516
|
+
}
|
|
517
|
+
if (collected.warnings.length > 0) {
|
|
518
|
+
lines.push(`Skipped:\n${collected.warnings.map((item) => `- ${item}`).join("\n")}`);
|
|
519
|
+
}
|
|
520
|
+
const ok = await ui.confirm("Push configuration", lines.join("\n"));
|
|
521
|
+
if (!ok) throw new SyncError("Push cancelled.");
|
|
522
|
+
}
|
|
523
|
+
|
|
524
|
+
const commit = await github.pushSnapshot({
|
|
525
|
+
token,
|
|
526
|
+
owner,
|
|
527
|
+
name: repoName,
|
|
528
|
+
branch,
|
|
529
|
+
expectedCommit: snapshot.commit.length > 0 ? snapshot.commit : null,
|
|
530
|
+
message: "Sync Pi configuration",
|
|
531
|
+
files,
|
|
532
|
+
signal: options.signal,
|
|
533
|
+
});
|
|
534
|
+
state.base = { commit, hashes: manifest.hashes };
|
|
535
|
+
state.lastSync = {
|
|
536
|
+
action: "push",
|
|
537
|
+
at: new Date().toISOString(),
|
|
538
|
+
commit,
|
|
539
|
+
files: syncedCount,
|
|
540
|
+
conflicts: plan.conflicts.length,
|
|
541
|
+
};
|
|
542
|
+
await saveState(agentDir, state);
|
|
543
|
+
|
|
544
|
+
const parts = [`Pushed ${syncedCount} file(s) to ${owner}/${repoName}@${branch}.`];
|
|
545
|
+
if (plan.conflicts.length > 0) {
|
|
546
|
+
parts.push(`Overwrote ${plan.conflicts.length} remote change(s).`);
|
|
547
|
+
}
|
|
548
|
+
if (collected.warnings.length > 0) {
|
|
549
|
+
parts.push(`Skipped: ${collected.warnings.join(" ")}`);
|
|
550
|
+
}
|
|
551
|
+
ui.notify(parts.join(" "), collected.warnings.length > 0 ? "warning" : "info");
|
|
552
|
+
return {
|
|
553
|
+
files: syncedCount,
|
|
554
|
+
commit,
|
|
555
|
+
conflicts: plan.conflicts.length,
|
|
556
|
+
forced: plan.conflicts.length > 0,
|
|
557
|
+
};
|
|
558
|
+
}
|
|
559
|
+
|
|
560
|
+
export async function runAutoPull(ui: SyncUi, agentDir: string): Promise<void> {
|
|
561
|
+
const config = await loadConfig(agentDir);
|
|
562
|
+
if (!config.autoPull) return;
|
|
563
|
+
const token = await readSyncToken(agentDir);
|
|
564
|
+
if (token === null) return;
|
|
565
|
+
try {
|
|
566
|
+
const result = await runPull(ui, agentDir, {
|
|
567
|
+
silent: true,
|
|
568
|
+
quiet: true,
|
|
569
|
+
allowSecrets: false,
|
|
570
|
+
});
|
|
571
|
+
const updated = result.changed + result.deleted;
|
|
572
|
+
if (updated > 0) {
|
|
573
|
+
ui.notify(
|
|
574
|
+
`config-sync updated ${updated} file(s) from GitHub. Run /reload to apply the changes.`,
|
|
575
|
+
"info",
|
|
576
|
+
);
|
|
577
|
+
}
|
|
578
|
+
if (result.secretsSkipped) {
|
|
579
|
+
ui.notify(
|
|
580
|
+
"config-sync: synced credentials changed on GitHub. Run /sync pull to update them.",
|
|
581
|
+
"warning",
|
|
582
|
+
);
|
|
583
|
+
}
|
|
584
|
+
} catch (error) {
|
|
585
|
+
ui.notify(`config-sync auto pull skipped: ${errorMessage(error)}`, "warning");
|
|
586
|
+
}
|
|
587
|
+
}
|
|
588
|
+
|
|
589
|
+
async function resolveTarget(
|
|
590
|
+
token: string,
|
|
591
|
+
config: SyncConfig,
|
|
592
|
+
state: SyncState,
|
|
593
|
+
signal: AbortSignal | undefined,
|
|
594
|
+
): Promise<Target> {
|
|
595
|
+
let fullName = config.repo;
|
|
596
|
+
if (fullName.length === 0) {
|
|
597
|
+
if (state.login.length === 0) {
|
|
598
|
+
const viewer = await github.getViewer(token, signal);
|
|
599
|
+
state.login = viewer.login;
|
|
600
|
+
state.scopes = viewer.scopes;
|
|
601
|
+
}
|
|
602
|
+
fullName = `${state.login}/pi-config`;
|
|
603
|
+
}
|
|
604
|
+
const { owner, name } = github.splitRepo(fullName);
|
|
605
|
+
const repo = await github.getRepo(token, owner, name, signal);
|
|
606
|
+
const branch = github.assertSafeBranch(config.branch || repo?.defaultBranch || "main");
|
|
607
|
+
return { owner, name, repo, branch };
|
|
608
|
+
}
|
|
609
|
+
|
|
610
|
+
async function resetBase(agentDir: string): Promise<void> {
|
|
611
|
+
const { state } = await loadState(agentDir);
|
|
612
|
+
state.base = null;
|
|
613
|
+
await saveState(agentDir, state);
|
|
614
|
+
}
|
|
615
|
+
|
|
616
|
+
async function resolvePullConflicts(
|
|
617
|
+
ui: SyncUi,
|
|
618
|
+
conflicts: string[],
|
|
619
|
+
): Promise<string[] | undefined> {
|
|
620
|
+
const choice = await ui.select(
|
|
621
|
+
`config-sync: ${conflicts.length} conflicting file(s)`,
|
|
622
|
+
PULL_CONFLICT_OPTIONS,
|
|
623
|
+
);
|
|
624
|
+
if (choice === undefined || choice === "Cancel") return undefined;
|
|
625
|
+
if (choice === "Use remote for all") return [...conflicts];
|
|
626
|
+
if (choice === "Keep local for all") return [];
|
|
627
|
+
const useRemote: string[] = [];
|
|
628
|
+
for (const path of conflicts) {
|
|
629
|
+
const answer = await ui.select(`Conflict: ${path}`, ["Use remote", "Keep local"]);
|
|
630
|
+
if (answer === undefined) return undefined;
|
|
631
|
+
if (answer === "Use remote") useRemote.push(path);
|
|
632
|
+
}
|
|
633
|
+
return useRemote;
|
|
634
|
+
}
|
|
635
|
+
|
|
636
|
+
async function prepareMergedSettings(
|
|
637
|
+
ui: SyncUi,
|
|
638
|
+
agentDir: string,
|
|
639
|
+
config: SyncConfig,
|
|
640
|
+
snapshot: RemoteSnapshot,
|
|
641
|
+
writes: string[],
|
|
642
|
+
): Promise<string | undefined> {
|
|
643
|
+
if (!writes.includes(SETTINGS_REMOTE_PATH)) return undefined;
|
|
644
|
+
const remote = snapshot.files.get(SETTINGS_REMOTE_PATH);
|
|
645
|
+
if (remote === undefined) throw new SyncError("Remote settings.json is missing.");
|
|
646
|
+
try {
|
|
647
|
+
const localText = await readTextFile(remoteToLocalPath(agentDir, SETTINGS_REMOTE_PATH));
|
|
648
|
+
return mergeSettings(remote.bytes.toString("utf8"), localText, config.localSettingsKeys);
|
|
649
|
+
} catch (error) {
|
|
650
|
+
ui.notify(`Remote settings.json could not be merged: ${errorMessage(error)}`, "error");
|
|
651
|
+
throw error;
|
|
652
|
+
}
|
|
653
|
+
}
|
|
654
|
+
|
|
655
|
+
async function applyRemoteSecrets(
|
|
656
|
+
ui: SyncUi,
|
|
657
|
+
agentDir: string,
|
|
658
|
+
snapshot: RemoteSnapshot,
|
|
659
|
+
previousBase: Record<string, string>,
|
|
660
|
+
options: PullOptions,
|
|
661
|
+
): Promise<SecretResult> {
|
|
662
|
+
const remote = snapshot.files.get(SECRETS_PATH);
|
|
663
|
+
if (remote === undefined) return { applied: false, skipped: false };
|
|
664
|
+
if (previousBase[SECRETS_PATH] === remote.sha256) return { applied: false, skipped: false };
|
|
665
|
+
if (options.silent) return { applied: false, skipped: true };
|
|
666
|
+
let passphrase = await getCachedPassphrase(ui, "Passphrase for synced credentials");
|
|
667
|
+
if (passphrase === undefined) return { applied: false, skipped: true };
|
|
668
|
+
let decrypted: unknown = undefined;
|
|
669
|
+
try {
|
|
670
|
+
decrypted = await decryptJson(remote.bytes.toString("utf8"), passphrase);
|
|
671
|
+
} catch {
|
|
672
|
+
clearPassphraseCache();
|
|
673
|
+
passphrase = await getCachedPassphrase(ui, "Wrong passphrase. Try again");
|
|
674
|
+
if (passphrase === undefined) return { applied: false, skipped: true };
|
|
675
|
+
try {
|
|
676
|
+
decrypted = await decryptJson(remote.bytes.toString("utf8"), passphrase);
|
|
677
|
+
} catch (error) {
|
|
678
|
+
clearPassphraseCache();
|
|
679
|
+
ui.notify(errorMessage(error), "error");
|
|
680
|
+
return { applied: false, skipped: true };
|
|
681
|
+
}
|
|
682
|
+
}
|
|
683
|
+
if (!isRecord(decrypted)) {
|
|
684
|
+
ui.notify("Synced credentials have an unsupported shape.", "error");
|
|
685
|
+
return { applied: false, skipped: true };
|
|
686
|
+
}
|
|
687
|
+
let applied = 0;
|
|
688
|
+
try {
|
|
689
|
+
await updateAuthEntries(agentDir, (auth) => {
|
|
690
|
+
const merged = mergeAuthEntries(auth, decrypted);
|
|
691
|
+
for (const key of Object.keys(auth)) delete auth[key];
|
|
692
|
+
Object.assign(auth, merged.auth);
|
|
693
|
+
applied = merged.applied;
|
|
694
|
+
});
|
|
695
|
+
} catch (error) {
|
|
696
|
+
ui.notify(`Could not update auth.json: ${errorMessage(error)}`, "error");
|
|
697
|
+
return { applied: false, skipped: true };
|
|
698
|
+
}
|
|
699
|
+
return { applied: applied > 0, skipped: false };
|
|
700
|
+
}
|
|
701
|
+
|
|
702
|
+
async function buildSecrets(
|
|
703
|
+
ui: SyncUi,
|
|
704
|
+
agentDir: string,
|
|
705
|
+
config: SyncConfig,
|
|
706
|
+
snapshot: RemoteSnapshot,
|
|
707
|
+
): Promise<SecretBuild> {
|
|
708
|
+
const auth = await readAuthObject(agentDir);
|
|
709
|
+
const filtered = filterAuthEntries(auth, {
|
|
710
|
+
include: config.includeAuthProviders,
|
|
711
|
+
exclude: config.excludeAuthProviders,
|
|
712
|
+
});
|
|
713
|
+
const remote = snapshot.files.get(SECRETS_PATH);
|
|
714
|
+
if (Object.keys(filtered).length === 0) {
|
|
715
|
+
if (remote === undefined) return { bytes: undefined, changed: false };
|
|
716
|
+
return { bytes: remote.bytes, changed: false };
|
|
717
|
+
}
|
|
718
|
+
if (remote === undefined) {
|
|
719
|
+
const passphrase = await chooseNewPassphrase(ui);
|
|
720
|
+
if (passphrase === undefined) {
|
|
721
|
+
throw new SyncError("Push cancelled: no passphrase was provided for the credentials.");
|
|
722
|
+
}
|
|
723
|
+
return { bytes: await encryptBytes(filtered, passphrase), changed: true };
|
|
724
|
+
}
|
|
725
|
+
const passphrase = await getCachedPassphrase(
|
|
726
|
+
ui,
|
|
727
|
+
"Passphrase for the existing synced credentials",
|
|
728
|
+
);
|
|
729
|
+
if (passphrase === undefined) {
|
|
730
|
+
throw new SyncError("Push cancelled: no passphrase was provided for the credentials.");
|
|
731
|
+
}
|
|
732
|
+
let decrypted: unknown = undefined;
|
|
733
|
+
try {
|
|
734
|
+
decrypted = await decryptJson(remote.bytes.toString("utf8"), passphrase);
|
|
735
|
+
} catch (error) {
|
|
736
|
+
clearPassphraseCache();
|
|
737
|
+
throw new SyncError(`Could not open the existing synced credentials: ${errorMessage(error)}`);
|
|
738
|
+
}
|
|
739
|
+
if (!isRecord(decrypted)) {
|
|
740
|
+
throw new SyncError("Existing synced credentials have an unsupported shape.");
|
|
741
|
+
}
|
|
742
|
+
if (authEntriesEqual(decrypted, filtered)) return { bytes: remote.bytes, changed: false };
|
|
743
|
+
return { bytes: await encryptBytes(filtered, passphrase), changed: true };
|
|
744
|
+
}
|
|
745
|
+
|
|
746
|
+
async function encryptBytes(value: Record<string, unknown>, passphrase: string): Promise<Buffer> {
|
|
747
|
+
return Buffer.from(await encryptJson(value, passphrase), "utf8");
|
|
748
|
+
}
|
|
749
|
+
|
|
750
|
+
async function getCachedPassphrase(ui: SyncUi, title: string): Promise<string | undefined> {
|
|
751
|
+
if (cachedPassphrase !== null) return cachedPassphrase;
|
|
752
|
+
const value = await ui.askPassphrase(title);
|
|
753
|
+
if (value === undefined) return undefined;
|
|
754
|
+
cachedPassphrase = value;
|
|
755
|
+
return value;
|
|
756
|
+
}
|
|
757
|
+
|
|
758
|
+
async function chooseNewPassphrase(ui: SyncUi): Promise<string | undefined> {
|
|
759
|
+
const first = await ui.askPassphrase(
|
|
760
|
+
"Choose a passphrase for synced credentials (at least 8 characters)",
|
|
761
|
+
);
|
|
762
|
+
if (first === undefined) return undefined;
|
|
763
|
+
const problem = validatePassphrase(first);
|
|
764
|
+
if (problem !== null) throw new SyncError(problem);
|
|
765
|
+
const second = await ui.askPassphrase("Confirm the passphrase");
|
|
766
|
+
if (second === undefined) return undefined;
|
|
767
|
+
if (first !== second) throw new SyncError("Passphrases do not match.");
|
|
768
|
+
cachedPassphrase = first;
|
|
769
|
+
return first;
|
|
770
|
+
}
|
|
771
|
+
|
|
772
|
+
function buildManifest(
|
|
773
|
+
files: Array<{ path: string; bytes: Buffer }>,
|
|
774
|
+
deviceId: string,
|
|
775
|
+
): { hashes: Record<string, string>; manifestBytes: Buffer } {
|
|
776
|
+
const hashes: Record<string, string> = {};
|
|
777
|
+
for (const file of files) hashes[file.path] = sha256Hex(file.bytes);
|
|
778
|
+
const manifest: SyncManifest = {
|
|
779
|
+
version: 1,
|
|
780
|
+
tool: MANIFEST_TOOL,
|
|
781
|
+
deviceId,
|
|
782
|
+
pushedAt: new Date().toISOString(),
|
|
783
|
+
files: hashes,
|
|
784
|
+
};
|
|
785
|
+
return {
|
|
786
|
+
hashes,
|
|
787
|
+
manifestBytes: Buffer.from(`${JSON.stringify(manifest, null, 2)}\n`, "utf8"),
|
|
788
|
+
};
|
|
789
|
+
}
|
|
790
|
+
|
|
791
|
+
function remoteHashMap(files: Map<string, RemoteFile>): Record<string, string> {
|
|
792
|
+
const hashes: Record<string, string> = {};
|
|
793
|
+
for (const [path, file] of files) hashes[path] = file.sha256;
|
|
794
|
+
return hashes;
|
|
795
|
+
}
|
|
796
|
+
|
|
797
|
+
function withoutSecrets(hashes: Record<string, string>): Record<string, string> {
|
|
798
|
+
const result = { ...hashes };
|
|
799
|
+
delete result[SECRETS_PATH];
|
|
800
|
+
return result;
|
|
801
|
+
}
|
|
802
|
+
|
|
803
|
+
function agentRelative(remotePath: string): string {
|
|
804
|
+
if (!remotePath.startsWith(AGENT_PATH_PREFIX)) {
|
|
805
|
+
throw new SyncError(`Unexpected remote path: ${remotePath}`);
|
|
806
|
+
}
|
|
807
|
+
return normalizeRelativePath(remotePath.slice(AGENT_PATH_PREFIX.length));
|
|
808
|
+
}
|
|
809
|
+
|
|
810
|
+
function previewPaths(paths: string[]): string {
|
|
811
|
+
const limit = 5;
|
|
812
|
+
const shown = paths.slice(0, limit).join(", ");
|
|
813
|
+
return paths.length > limit ? `${shown}, ...` : shown;
|
|
814
|
+
}
|
|
815
|
+
|
|
816
|
+
function shortSha(commit: string): string {
|
|
817
|
+
return commit.length > 7 ? commit.slice(0, 7) : commit || "(none)";
|
|
818
|
+
}
|
|
819
|
+
|
|
820
|
+
function emptyPullResult(): PullResult {
|
|
821
|
+
return {
|
|
822
|
+
changed: 0,
|
|
823
|
+
deleted: 0,
|
|
824
|
+
conflicts: 0,
|
|
825
|
+
secretsApplied: false,
|
|
826
|
+
secretsSkipped: false,
|
|
827
|
+
commit: "",
|
|
828
|
+
};
|
|
829
|
+
}
|