summaryrefslogtreecommitdiff
path: root/cacheDir.go
diff options
context:
space:
mode:
Diffstat (limited to 'cacheDir.go')
-rw-r--r--cacheDir.go68
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
+}