fix: unambiguous repo key names, clearer config and key names, and a newer lazysecrets's files no longer stop an older one (#66)
Changes that would break configs or links once people use lazysecrets, made while nobody does yet. - **Repo key names**: a dot or bracket inside a key is escaped (`app\.name`), so links to it resolve to that key and not to `app` → `name`. Dotenv files are flat, so their names stay as they are. - **Config**: `updates.updateSources` is now `updates.sources`. A negative provider timeout, or `--timeout` of 0 or less, is an error instead of making every request fail. - **Keybindings**: `lists.share` is now `lists.copyLink`, `lists.search` is `lists.goTo` and `lists.enter` is `lists.open`, matching what they do. - **Version skew**, e.g. a lazysecrets.nvim theme file made for a newer lazysecrets: - an unknown theme role is skipped with a warning instead of stopping startup; - a state file from a newer version is read but not saved to, with a warning; - a link with an unknown provider suggests `lazysecrets update`. Reviewed-on: #66 Co-authored-by: Karl-Einar Bergstrøm <karl-einarb@outlook.com>
This commit was merged in pull request #66.
This commit is contained in:
1 parent
e28328ef82
commit
a81fcaa5a6
16 files changed
+189
-58
No files matched your search
+13
-6
@@ -145,16 +145,23 @@ type Store struct {
|
||||
data file
|
||||
}
|
||||
|
||||
// ErrNewer is a state file written by a newer lazysecrets.
|
||||
var ErrNewer = errors.New("written by a newer lazysecrets")
|
||||
|
||||
// Load reads path; a missing file is an empty store. path "" gives a store
|
||||
// that never saves, for tests and --demo.
|
||||
// that never saves, for tests and --demo. A file from a newer lazysecrets
|
||||
// is read but never saved, so nothing it keeps is lost: Load returns the
|
||||
// store with an ErrNewer error.
|
||||
func Load(path string) (*Store, error) {
|
||||
s := &Store{path: path}
|
||||
d, err := read(path)
|
||||
if err != nil {
|
||||
if errors.Is(err, ErrNewer) {
|
||||
s.path = ""
|
||||
} else if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
s.data = d
|
||||
return s, nil
|
||||
return s, err
|
||||
}
|
||||
|
||||
func read(path string) (file, error) {
|
||||
@@ -172,12 +179,12 @@ func read(path string) (file, error) {
|
||||
if err := json.Unmarshal(b, &d); err != nil {
|
||||
return d, fmt.Errorf("%s: %w", path, err)
|
||||
}
|
||||
if d.Version > version {
|
||||
return d, fmt.Errorf("%s: written by a newer lazysecrets (version %d)", path, d.Version)
|
||||
}
|
||||
if d.Nodes == nil {
|
||||
d.Nodes = map[string]Node{}
|
||||
}
|
||||
if d.Version > version {
|
||||
return d, fmt.Errorf("%s: %w (version %d)", path, ErrNewer, d.Version)
|
||||
}
|
||||
return d, nil
|
||||
}
|
||||
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package store
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
@@ -65,11 +66,17 @@ func TestTwoWindowsDontLoseEachOthersChanges(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestNewerVersionRefused(t *testing.T) {
|
||||
func TestNewerVersionReadOnly(t *testing.T) {
|
||||
path := filepath.Join(t.TempDir(), "state.json")
|
||||
_ = os.WriteFile(path, []byte(`{"version": 99, "nodes": {}}`), 0o600)
|
||||
if _, err := Load(path); err == nil {
|
||||
t.Error("a newer file was accepted")
|
||||
newer := `{"version": 99, "nodes": {"one": {"note": "hi"}}}`
|
||||
_ = os.WriteFile(path, []byte(newer), 0o600)
|
||||
s, err := Load(path)
|
||||
if !errors.Is(err, ErrNewer) || s == nil || s.Node("one").Note != "hi" {
|
||||
t.Fatalf("Load = %v, %v", s, err)
|
||||
}
|
||||
_ = s.SetFlag("x", true)
|
||||
if b, _ := os.ReadFile(path); string(b) != newer {
|
||||
t.Errorf("the newer file was overwritten: %s", b)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user