summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJeff Carr <[email protected]>2025-10-20 13:20:40 -0500
committerJeff Carr <[email protected]>2025-10-20 13:20:40 -0500
commit5c4c1fcaf4e724573dd39b4d4fa0d600a9870221 (patch)
treec50e35d812e30029a40132c7653504b79a0c3c9b
parentc99d5193053a9377acfb62677bf241e540f9f6ee (diff)
maybe better than before. notsure
-rw-r--r--loadByAppname.go45
-rw-r--r--loadCache.go49
-rw-r--r--loadConfig.go61
-rw-r--r--loadRaw.go14
-rw-r--r--makeFilenames.go31
5 files changed, 186 insertions, 14 deletions
diff --git a/loadByAppname.go b/loadByAppname.go
new file mode 100644
index 0000000..5ee640e
--- /dev/null
+++ b/loadByAppname.go
@@ -0,0 +1,45 @@
+package config
+
+import (
+ "errors"
+
+ "go.wit.com/log"
+ "google.golang.org/protobuf/proto"
+)
+
+// loads foo.proto from ~/.cache/<appname>/foo.text
+func LoadByAppname(pb proto.Message, appname string) error {
+ protoname, err := GetProtobufName(pb) // defined in the foo.proto file
+ if err != nil {
+ return err
+ }
+
+ // Get ~/.cache/appname/protoname.text
+ fullname := makeCacheFilename(appname, protoname)
+
+ // get the current filename in the protobuf file
+ curfilename, err := GetFilename(pb)
+ if err != nil {
+ return err
+ }
+ if curfilename == "" {
+ log.Printf("ConfigLoad() reading in %s\n", fullname)
+ }
+
+ err = loadPB(pb, fullname)
+ if err != nil {
+ return ErrMarshal
+ }
+
+ // If the cache file is new or has moved, this updates it to correct filename
+ // (the filename is what is used by pb.Save()
+ if curfilename != fullname {
+ _, err := SetFilename(pb, fullname)
+ if err != nil {
+ log.Info("FILENAME COULD NOT BE SET old=", curfilename)
+ log.Info("FILENAME COULD NOT BE SET new=", fullname)
+ return errors.Join(err, errors.New("something is wrong in lib/config"))
+ }
+ }
+ return nil
+}
diff --git a/loadCache.go b/loadCache.go
new file mode 100644
index 0000000..f7ab6ae
--- /dev/null
+++ b/loadCache.go
@@ -0,0 +1,49 @@
+package config
+
+import (
+ "errors"
+
+ "go.wit.com/log"
+ "google.golang.org/protobuf/proto"
+)
+
+// loads foo.proto from ~/.cache/<appname>/foo.text
+func CacheLoad(pb proto.Message) error {
+ appname, err := GetAppname() // already configured by your application
+ if err != nil {
+ return err
+ }
+ protoname, err := GetProtobufName(pb) // defined in the foo.proto file
+ if err != nil {
+ return err
+ }
+
+ // Get ~/.cache/appname/protoname.text
+ fullname := makeCacheFilename(appname, protoname)
+
+ // get the current filename in the protobuf file
+ curfilename, err := GetFilename(pb)
+ if err != nil {
+ return err
+ }
+ if curfilename == "" {
+ log.Printf("ConfigLoad() reading in %s\n", fullname)
+ }
+
+ err = loadPB(pb, fullname)
+ if err != nil {
+ return ErrMarshal
+ }
+
+ // If the cache file is new or has moved, this updates it to correct filename
+ // (the filename is what is used by pb.Save()
+ if curfilename != fullname {
+ _, err := SetFilename(pb, fullname)
+ if err != nil {
+ log.Info("FILENAME COULD NOT BE SET old=", curfilename)
+ log.Info("FILENAME COULD NOT BE SET new=", fullname)
+ return errors.Join(err, errors.New("something is wrong in lib/config"))
+ }
+ }
+ return nil
+}
diff --git a/loadConfig.go b/loadConfig.go
index 0c90a17..439e65c 100644
--- a/loadConfig.go
+++ b/loadConfig.go
@@ -25,7 +25,7 @@ func ConfigLoad(pb proto.Message) error {
}
// Get ~/.config/appname/protoname.text
- fullname := GetConfigFilename(appname, protoname)
+ fullname := makeConfigFilename(appname, protoname)
if err = loadTEXT(pb, fullname); err == nil {
// If the config is old or broken, this sets the filename
@@ -57,3 +57,62 @@ func ConfigLoad(pb proto.Message) error {
}
return ErrMarshal
}
+
+// loads foo.proto from ~/.config/<appname>/customName.text
+func ConfigLoadByName(pb proto.Message, customName string) (string, error) {
+ if customName == "" {
+ return "", errors.New("config.Load() customName can not be blank")
+ }
+ appname, err := GetAppname() // already configured by your application
+ if err != nil {
+ return "", err
+ }
+
+ // Get ~/.config/appname/customName.text
+ fullname := makeConfigFilename(appname, customName)
+
+ curfilename, err := GetFilename(pb)
+ if err != nil {
+ // pb doesn't have 'Filename'
+ // probably try to load anyway?
+ log.Info("ConfigLoadByName() FILENAME old=", curfilename)
+ log.Info("ConfigLoadByName() FILENAME new=", fullname)
+ log.Info("pb doesn't have 'Filename'")
+ log.Printf("err = (%v)\n", err)
+ panic("blah")
+ // return fullname, err
+ }
+ if curfilename == "" {
+ log.Printf("ConfigLoadByName() reading in %s\n", fullname)
+ }
+
+ if err = loadTEXT(pb, fullname); err == nil {
+ // If the config is old or broken, this sets the filename
+ if curfilename != fullname {
+ _, err := SetFilename(pb, fullname)
+ if err != nil {
+ log.Info("FILENAME COULD NOT BE SET old=", curfilename)
+ log.Info("FILENAME COULD NOT BE SET new=", fullname)
+ return fullname, errors.Join(err, errors.New("something is wrong in lib/config"))
+ }
+ }
+ return fullname, nil
+ } else {
+ if strings.HasSuffix(fullname, ".text") {
+ fulljson := fullname + ".json"
+ // If the config is old or broken, this sets the filename
+ if err := loadJSON(pb, fulljson); err == nil {
+ if curfilename != fullname {
+ _, err := SetFilename(pb, fullname)
+ if err != nil {
+ log.Info("FILENAME COULD NOT BE SET old=", curfilename)
+ log.Info("FILENAME COULD NOT BE SET new=", fullname)
+ return fullname, errors.Join(err, errors.New("something is wrong in lib/config"))
+ }
+ }
+ return fullname, nil
+ }
+ }
+ }
+ return fullname, ErrMarshal
+}
diff --git a/loadRaw.go b/loadRaw.go
index c09b3f5..3198783 100644
--- a/loadRaw.go
+++ b/loadRaw.go
@@ -31,7 +31,7 @@ var ErrMarshal error = fmt.Errorf("protobuf parse error")
// - error on read
func ConfigLoadRaw(pb proto.Message, appname string, protoname string) error {
// Get ~/.config/appname/protoname.text
- fullname := GetConfigFilename(appname, protoname)
+ fullname := makeConfigFilename(appname, protoname)
var pbFilenameSupport bool
var err error
@@ -77,18 +77,6 @@ func ConfigLoadRaw(pb proto.Message, appname string, protoname string) error {
return ErrMarshal
}
-// returns the default constructed filename:
-// ~/.config/appname/protoname.text
-func GetConfigFilename(appname string, protoname string) string {
- var err error
- configDir, err := os.UserConfigDir()
- if err != nil {
- // todo: get something better than /tmp/ if anyone cares
- return filepath.Join("/tmp", appname, protoname+".text")
- }
- return filepath.Join(configDir, appname, protoname+".text")
-}
-
// loads from the users .cache dir
// if the .proto file version changes, automatically delete the .pb
// file. This is important to avoid marshalling garbage data
diff --git a/makeFilenames.go b/makeFilenames.go
new file mode 100644
index 0000000..6ade093
--- /dev/null
+++ b/makeFilenames.go
@@ -0,0 +1,31 @@
+package config
+
+// config files are always human readable (foo.text)
+// cache files are always raw data (foo.pb)
+
+import (
+ "os"
+ "path/filepath"
+)
+
+// ~/.config/appname/protoname.text
+func makeConfigFilename(appname string, protoname string) string {
+ var err error
+ configDir, err := os.UserConfigDir()
+ if err != nil {
+ // todo: get something better than /tmp/ if anyone cares
+ return filepath.Join("/tmp", appname, protoname+".text")
+ }
+ return filepath.Join(configDir, appname, protoname+".text")
+}
+
+// ~/.cache/appname/protoname.pb
+func makeCacheFilename(appname string, protoname string) string {
+ var err error
+ cacheDir, err := os.UserCacheDir()
+ if err != nil {
+ // todo: get something better than /tmp/ if anyone cares
+ return filepath.Join("/tmp", appname, protoname+".pb")
+ }
+ return filepath.Join(cacheDir, appname, protoname+".pb")
+}