Use platform-specific atomic key replacement on Windows

Swap the keystore through a temp and .old file on Windows instead
of relying on os.Rename alone, which is not reliably atomic there.

Signed-off-by: Bryan Frimin <bryan@probo.com>
This commit is contained in:
Bryan Frimin
2026-05-26 09:34:51 -07:00
parent 0a4922f571
commit d310886560
3 changed files with 86 additions and 7 deletions

View File

@@ -48,13 +48,8 @@ func SaveAPIKey(dir, key string) error {
}
path := KeyPath(dir)
tmp := path + ".tmp"
if err := os.WriteFile(tmp, []byte(strings.TrimSpace(key)+"\n"), 0o600); err != nil {
return fmt.Errorf("cannot write key: %w", err)
}
if err := os.Rename(tmp, path); err != nil {
return fmt.Errorf("cannot atomically replace key: %w", err)
if err := replaceRegularFile(path, []byte(strings.TrimSpace(key)+"\n"), 0o600); err != nil {
return fmt.Errorf("cannot replace key: %w", err)
}
return nil