zrokdir versioning (#134)

This commit is contained in:
Michael Quigley 2023-01-09 16:28:54 -05:00
parent 38132f2e7c
commit b8f566bf6a
No known key found for this signature in database
GPG Key ID: 9B60314A9DD20A62
2 changed files with 62 additions and 0 deletions

47
zrokdir/version.go Normal file
View File

@ -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
}

View File

@ -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 {