diff options
| author | Jeff Carr <[email protected]> | 2025-10-22 09:19:26 -0500 |
|---|---|---|
| committer | Jeff Carr <[email protected]> | 2025-10-22 09:19:26 -0500 |
| commit | 23b2d19bc03b4db253b15cc6fef7176ab31d4f18 (patch) | |
| tree | 275cb2d3fc92cb5af997582c715f4619eb1d68d2 /cacheDir.go | |
| parent | 820fa4b7e71690accbd17063894032140bd9c4b6 (diff) | |
finally can fix the Load() and Save() names
Diffstat (limited to 'cacheDir.go')
| -rw-r--r-- | cacheDir.go | 68 |
1 files changed, 68 insertions, 0 deletions
diff --git a/cacheDir.go b/cacheDir.go new file mode 100644 index 0000000..370b619 --- /dev/null +++ b/cacheDir.go @@ -0,0 +1,68 @@ +package config + +// loads from the users .cache/ dir +// +// GOAL: always work. When in doubt, delete things in ~/.cache/ +// RULE: always use raw .pb files, never human readable .text or .json files +// +// deletes files when versions don't match +// deletes files when filenames can't be set +// deletes anything that doesn't work +// +// This is important to avoid unmarshalling garbage protobuf data + +import ( + "errors" + "os" + + "go.wit.com/lib/ENV" + "go.wit.com/log" + "google.golang.org/protobuf/proto" +) + +// loads foo.proto from ~/.cache/<appname>/foo.pb +func LoadCacheDir(pb proto.Message) error { + appname, err := ENV.GetAppname() // already configured by your application + if err != nil { + return err + } + + err = LoadCacheDirByAppname(pb, appname) + return err +} + +// loads foo.proto from ~/.cache/<appname>/foo.pb +func LoadCacheDirByAppname(pb proto.Message, appname string) error { + protoname, err := GetProtobufName(pb) // defined in the foo.proto file + if err != nil { + return err + } + + err = CreateCacheDirPB(pb, appname, protoname) + return err +} + +// checks the UUID and Version of the .pb file +func CreateCacheDirPB(pb proto.Message, appname string, protoname string) error { + // Get ~/.cache/appname/protoname.text + fullname := MakeCacheFilename(appname, protoname) + + _, err := SetFilename(pb, fullname) + if err != nil { + log.Info("lib/config PB file does not support Filename") + } + newver, curver, err := VersionCheckFile(pb, fullname) + _, _ = newver, curver + if err == nil { + // everything is fine. Versions match. load file + err = LoadFromFilename(pb, fullname) + return err + } + if errors.Is(err, os.ErrNotExist) { + // file is new, create the file + err = SaveToFilename(pb, fullname) + return err + } + // some other bad error + return err +} |
