zrok/zrokdir/version.go

54 lines
1001 B
Go
Raw Normal View History

2023-01-09 22:28:54 +01:00
package zrokdir
import (
"encoding/json"
"github.com/openziti/zrok/tui"
2023-01-09 22:28:54 +01:00
"github.com/pkg/errors"
"os"
2023-01-09 23:57:57 +01:00
"path/filepath"
2023-01-09 22:28:54 +01:00
)
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 {
2023-01-09 23:56:16 +01:00
tui.Warning("unable to open zrokdir metadata; ignoring\n")
return nil
2023-01-09 22:28:54 +01:00
}
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
}
2023-01-09 23:57:57 +01:00
if err := os.MkdirAll(filepath.Dir(mf), os.FileMode(0700)); err != nil {
return err
}
if err := os.WriteFile(mf, data, os.FileMode(0600)); err != nil {
2023-01-09 22:28:54 +01:00
return err
}
return nil
}