summaryrefslogtreecommitdiff
path: root/load.go
diff options
context:
space:
mode:
Diffstat (limited to 'load.go')
-rw-r--r--load.go77
1 files changed, 61 insertions, 16 deletions
diff --git a/load.go b/load.go
index 48b6d4e..fc9a5a0 100644
--- a/load.go
+++ b/load.go
@@ -10,14 +10,15 @@ import (
"path/filepath"
"strings"
+ "go.wit.com/log"
"google.golang.org/protobuf/encoding/protojson"
"google.golang.org/protobuf/encoding/prototext"
"google.golang.org/protobuf/proto"
)
/*
-// loads a file from ~/.config/<argname>/
-func Load(argname string) ([]byte, string) {
+// loads a file from ~/.config/<appname>/
+func Load(appname string) ([]byte, string) {
}
*/
@@ -25,20 +26,30 @@ var ErrEmpty error = fmt.Errorf("config file was empty")
var ErrMarshal error = fmt.Errorf("protobuf parse error")
// returns:
-// - Full path to the config file. usually: ~/.config/<argname>
+// - Full path to the config file. usually: ~/.config/<appname>
// - []byte : the contents of the file
// - error on read
-func ConfigLoad(pb proto.Message, argname string, protoname string) error {
- var fullname string
+func ConfigLoad(pb proto.Message, appname string, protoname string) error {
+ // Get ~/.config/appname/protoname.text
+ fullname := GetConfigFilename(appname, protoname)
+
+ var pbFilenameSupport bool
var err error
- configDir, err := os.UserConfigDir()
+ curfilename, err := GetFilename(pb)
if err != nil {
- return err
+ log.Info("This protobuf doesn't support pb.Filename")
+ // make note this protobuf doesn't support Filenames
+ } else {
+ pbFilenameSupport = true
}
+ // panic(curfilename)
- fullname = filepath.Join(configDir, argname, protoname+".text")
- SetFilename(pb, fullname)
-
+ // potential syntax with this GO library is starting to look like:
+ //
+ // if errors.Is(err, config.ErrEmpty)
+ //
+ // if errors.Is(err, config.VersionMismatch)
+ //
// if both don't exist or both are empty, return known errors
// these can be used to detect if the user is new to the application
if err := missingConfig(fullname); err != nil {
@@ -52,11 +63,33 @@ func ConfigLoad(pb proto.Message, argname string, protoname string) error {
}
if err = loadTEXT(pb, fullname); err == nil {
+ if pbFilenameSupport {
+ // 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)
+ panic("something is wrong in lib/config")
+ }
+ }
+ }
return 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 pbFilenameSupport {
+ 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)
+ panic("something is wrong in lib/config")
+ }
+ }
+ }
return nil
}
}
@@ -64,14 +97,26 @@ func ConfigLoad(pb proto.Message, argname 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
// .cache files are treated as such, a "cache" file. don't keep important
// things in here. argv stores the information here for autodelete
-func LoadCache(pb proto.Message, argname string, protoname string) error {
+func LoadCache(pb proto.Message, appname string, protoname string) error {
cacheDir, _ := os.UserCacheDir()
- fullpath := filepath.Join(cacheDir, argname)
+ fullpath := filepath.Join(cacheDir, appname)
os.MkdirAll(fullpath, os.ModePerm)
fullname := filepath.Join(fullpath, protoname+".pb")
_, err := SetFilename(pb, fullname)
@@ -198,17 +243,17 @@ func loadPB(pb proto.Message, fullname string) error {
return nil
}
-func LoadConfigPB(pb proto.Message, argname string, protoname string) (string, error) {
+func LoadConfigPB(pb proto.Message, appname string, protoname string) (string, error) {
var fullname string
- if strings.HasPrefix(argname, "/") {
- fullname = filepath.Join(argname, protoname+".pb")
+ if strings.HasPrefix(appname, "/") {
+ fullname = filepath.Join(appname, protoname+".pb")
} else {
configDir, err := os.UserConfigDir()
if err != nil {
return "", err
}
- fullname = filepath.Join(configDir, argname, protoname+".pb")
+ fullname = filepath.Join(configDir, appname, protoname+".pb")
}
data, err := loadFile(fullname)