UpdateStore is the load-modify-save for credentials.json, under the exclusive credentials lock (BUG-3649): two `pad auth login` runs for different servers each used to load the same snapshot and save it with their own entry, so the second dropped the first's. fn mutates the freshly loaded store; whe
(fn func(*CredentialStore) error)
| 553 | // too, since a writer that waited out the lock may be mid-save, and then |
| 554 | // treats one that stays empty as empty (BUG-3829, loadStoreLocked). |
| 555 | func UpdateStore(fn func(*CredentialStore) error) error { |
| 556 | return withCredentialsLock(true, func(path string, locked bool) error { |
| 557 | var store *CredentialStore |
| 558 | var err error |
| 559 | if locked { |
| 560 | store, err = loadStoreLocked(path) |
| 561 | } else { |
| 562 | store, err = loadStoreUnlocked(path) |
| 563 | } |
| 564 | if errors.Is(err, errCredentialsEmptyUnlocked) { |
| 565 | return fmt.Errorf("%w; nothing was saved, retry (if it persists with no other pad running, remove the empty %s)", err, path) |
| 566 | } |
| 567 | if err != nil { |
| 568 | return err |
| 569 | } |
| 570 | if err := fn(store); err != nil { |
| 571 | return err |
| 572 | } |
| 573 | return store.saveLocked(path) |
| 574 | }) |
| 575 | } |
| 576 | |
| 577 | // WipeCredentialsFile removes ~/.pad/credentials.json entirely. Distinct |
| 578 | // from CredentialStore.Delete (which removes a single server's entry) |