mirror of
https://github.com/openziti/zrok.git
synced 2024-12-23 07:09:12 +01:00
zrokdir versioning (#134)
This commit is contained in:
parent
38132f2e7c
commit
b8f566bf6a
47
zrokdir/version.go
Normal file
47
zrokdir/version.go
Normal 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
|
||||
}
|
@ -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 {
|
||||
|
Loading…
Reference in New Issue
Block a user