summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJeff Carr <[email protected]>2025-10-21 07:52:05 -0500
committerJeff Carr <[email protected]>2025-10-21 07:52:05 -0500
commit61bad3fc7b6a866645e3d26defc9b6ba52ffe304 (patch)
tree4be3ec5d559907358067258bfeb9851c512a7e4f
parent5036409ef63bc4bf9b63d7dd0cf06cdcb2285ab9 (diff)
switch to this func
-rw-r--r--loadByAppname.go39
-rw-r--r--loadConfig.go57
2 files changed, 95 insertions, 1 deletions
diff --git a/loadByAppname.go b/loadByAppname.go
index 5ee640e..cfa22cf 100644
--- a/loadByAppname.go
+++ b/loadByAppname.go
@@ -7,7 +7,44 @@ import (
"google.golang.org/protobuf/proto"
)
-// loads foo.proto from ~/.cache/<appname>/foo.text
+// loads foo.proto from ~/.cache/<appname>/foo.pb
+func LoadAppnameCache(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
+}
+
+// deprecate
func LoadByAppname(pb proto.Message, appname string) error {
protoname, err := GetProtobufName(pb) // defined in the foo.proto file
if err != nil {
diff --git a/loadConfig.go b/loadConfig.go
index 7aec6a4..8e0f5ab 100644
--- a/loadConfig.go
+++ b/loadConfig.go
@@ -117,3 +117,60 @@ func ConfigLoadByName(pb proto.Message, customName string) (string, error) {
}
return fullname, ErrMarshal
}
+
+func LoadByAppName(pb proto.Message, customName string, appName string) (string, error) {
+ if customName == "" {
+ return "", errors.New("config.LoadByAppName() customName can not be blank")
+ }
+ if appName == "" {
+ return "", errors.New("config.LoadByAppName() appName can not be blank")
+ }
+
+ // 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
+}