diff --git a/zrokdir/version.go b/zrokdir/version.go new file mode 100644 index 00000000..0f191cfb --- /dev/null +++ b/zrokdir/version.go @@ -0,0 +1,47 @@ +package zrokdir + +import ( + "encoding/json" + "github.com/pkg/errors" + "os" +) + +const V = "v0.3" + +type Metadata struct { + V string `json:"v"` +} + +func checkMetadata() error { + mf, err := metadataFile() + if err != nil { + return err + } + data, err := os.ReadFile(mf) + if err != nil { + return errors.Wrapf(err, "error reading metadata file '%v'", mf) + } + m := &Metadata{} + if err := json.Unmarshal(data, m); err != nil { + return errors.Wrapf(err, "error unmarshaling metadata file '%v'", mf) + } + if m.V != V { + return errors.Errorf("invalid zrokdir metadata version '%v'", m.V) + } + return nil +} + +func writeMetadata() error { + mf, err := metadataFile() + if err != nil { + return err + } + data, err := json.Marshal(&Metadata{V: V}) + if err != nil { + return err + } + if err := os.WriteFile(mf, data, os.FileMode(0400)); err != nil { + return err + } + return nil +} diff --git a/zrokdir/zrokdir.go b/zrokdir/zrokdir.go index 56913d2a..a81a433b 100644 --- a/zrokdir/zrokdir.go +++ b/zrokdir/zrokdir.go @@ -36,6 +36,10 @@ func Initialize() (*ZrokDir, error) { } func Load() (*ZrokDir, error) { + if err := checkMetadata(); err != nil { + return nil, err + } + zrd := &ZrokDir{} ids, err := listIdentities() @@ -72,6 +76,9 @@ func Load() (*ZrokDir, error) { } func (zrd *ZrokDir) Save() error { + if err := writeMetadata(); err != nil { + return errors.Wrap(err, "error saving metadata") + } if zrd.Env != nil { if err := saveEnvironment(zrd.Env); err != nil { return errors.Wrap(err, "error saving environment") @@ -147,6 +154,14 @@ func identitiesDir() (string, error) { return filepath.Join(zrd, "identities"), nil } +func metadataFile() (string, error) { + zrd, err := zrokDir() + if err != nil { + return "", err + } + return filepath.Join(zrd, "metadata.json"), nil +} + func zrokDir() (string, error) { home, err := os.UserHomeDir() if err != nil {