diff options
| author | Jeff Carr <[email protected]> | 2025-10-14 00:15:41 -0500 |
|---|---|---|
| committer | Jeff Carr <[email protected]> | 2025-10-14 00:15:41 -0500 |
| commit | fc17dc394aedbc5fd636676202bad40c1f98c1de (patch) | |
| tree | 197ff50da15f186c38d50a71fede7b0c160ee57f | |
| parent | 3b4821a3af62376d675699493e01cd07891ad69c (diff) | |
implement application specific Config files (and Verbose())
| -rw-r--r-- | config.Save.go | 16 | ||||
| -rw-r--r-- | config.proto | 8 | ||||
| -rw-r--r-- | flags.go | 23 | ||||
| -rw-r--r-- | init.go | 76 | ||||
| -rw-r--r-- | load.go | 18 | ||||
| -rw-r--r-- | lookupPB.go (renamed from findFilename.go) | 0 | ||||
| -rw-r--r-- | save.go | 13 | ||||
| -rw-r--r-- | verbose.go | 26 |
8 files changed, 167 insertions, 13 deletions
diff --git a/config.Save.go b/config.Save.go new file mode 100644 index 0000000..d7d0bbf --- /dev/null +++ b/config.Save.go @@ -0,0 +1,16 @@ +package config + +import ( + "os" + "path/filepath" +) + +// saves your applications config file +func Save() error { + basedir, _ := filepath.Split(configPB.Filename) + if err := os.MkdirAll(basedir, os.ModePerm); err != nil { + return err + } + err := SavePB(configPB) + return err +} diff --git a/config.proto b/config.proto index b86de8f..2716967 100644 --- a/config.proto +++ b/config.proto @@ -7,14 +7,16 @@ package config; import "google/protobuf/timestamp.proto"; // Import the well-known type for Timestamp message Config { // - string name = 1; // a map for what thing? - map<string, string> vals = 2; // a simple map + string key = 1; // config key name `autogenpb:unique` `autogenpb:sort` + string value = 2; // config value name google.protobuf.Timestamp ctime = 3; // create time of the patch + map<string, string> vals = 4; // a simple map } message Configs { // `autogenpb:marshal` `autogenpb:nomutex` string uuid = 1; // `autogenpb:uuid:3135d0f9-82a9-40b6-8aa1-b683ebe7bedd` - string version = 2; // `autogenpb:version:v0.0.1 go.wit.com/lib/config` + string version = 2; // `autogenpb:version:v0.0.2 go.wit.com/lib/config` repeated Config configs = 3; string filename = 4; // can store where the filename is so that saves can be automated + map<string, string> flags = 5; // a simple map } diff --git a/flags.go b/flags.go new file mode 100644 index 0000000..d827f7e --- /dev/null +++ b/flags.go @@ -0,0 +1,23 @@ +package config + +func Get(flag string) string { + if configPB == nil { + return "" + } + found := configPB.FindByKey(flag) + if found == nil { + return "" + } + return found.Value +} + +func GetError(flag string) error { + return nil +} + +func Set(flag string) { +} + +func SetError(flag string) error { + return nil +} @@ -0,0 +1,76 @@ +package config + +// this is an experiment at this point to +// see how this turns out + +import ( + "errors" + "fmt" + "os" + "path/filepath" +) + +var configPB *Configs + +// these are normally what are sent from ldflags +var APPNAME string +var BUILDTIME string +var VERSION string + +var argv []string + +func Init(appname, version, buildtime string, fromargv []string) error { + APPNAME = appname + VERSION = version + BUILDTIME = buildtime + argv = fromargv + + configDir, err := os.UserConfigDir() + if err != nil { + fmt.Println("OS isn't returning UserConfigDir()", err) + return err + } + fullname := filepath.Join(configDir, appname, "config.text") + configPB = NewConfigs() + err = loadTEXT(configPB, fullname) + if err == nil { + return nil + } + + if errors.Is(err, os.ErrNotExist) { + // file doesn't exist, make a new file + return makeNewConfigFile(appname) + } + if errors.Is(err, ErrEmpty) { + fmt.Printf("config file size was empty. out of diskspace? %s\n", fullname) + return err + } + fmt.Println("config.Init()", err) + // panic("config") + return err +} + +func makeNewConfigFile(appname string) error { + configDir, err := os.UserConfigDir() + if err != nil { + fmt.Println("OS isn't returning UserConfigDir()", err) + return err + } + fullname := filepath.Join(configDir, appname, "config.text") + + configPB = NewConfigs() + configPB.Filename = fullname + + newvar := new(Config) + newvar.Key = "example config var" + newvar.Value = "protobufs are neat" + configPB.Clone(newvar) + + newvar.Key = "Verbose" + newvar.Value = "true" + configPB.Clone(newvar) + + // writes the config file to disk + err = Save() + return err +} @@ -31,12 +31,12 @@ var ErrMarshal error = fmt.Errorf("protobuf parse error") func ConfigLoad(pb proto.Message, argname string, protoname string) error { var fullname string var err error - homeDir, err := os.UserHomeDir() + configDir, err := os.UserConfigDir() if err != nil { return err } - fullname = filepath.Join(homeDir, ".config", argname, protoname+".text") + fullname = filepath.Join(configDir, argname, protoname+".text") SetFilename(pb, fullname) // if both don't exist or both are empty, return known errors @@ -72,11 +72,11 @@ func LoadCache(pb proto.Message, argname string, protoname string) error { os.MkdirAll(fullpath, os.ModePerm) fullname := filepath.Join(fullpath, protoname+".pb") _, err := SetFilename(pb, fullname) - return errors.Join(err, Load(pb)) + return errors.Join(err, LoadPB(pb)) } // this logic isn't great yet -func Load(pb proto.Message) error { +func LoadPB(pb proto.Message) error { fullname, err := GetFilename(pb) if err != nil { return err @@ -125,6 +125,10 @@ func Load(pb proto.Message) error { return nil } +func LoadFromFilename(pb proto.Message, fullname string) error { + return LoadFile(pb, fullname) +} + func LoadFile(pb proto.Message, fullname string) error { if strings.HasSuffix(fullname, ".text") { return loadTEXT(pb, fullname) @@ -153,17 +157,17 @@ func loadPB(pb proto.Message, fullname string) error { return nil } -func LoadPB(pb proto.Message, argname string, protoname string) (string, error) { +func LoadConfigPB(pb proto.Message, argname string, protoname string) (string, error) { var fullname string if strings.HasPrefix(argname, "/") { fullname = filepath.Join(argname, protoname+".pb") } else { - homeDir, err := os.UserHomeDir() + configDir, err := os.UserConfigDir() if err != nil { return "", err } - fullname = filepath.Join(homeDir, ".config", argname, protoname+".pb") + fullname = filepath.Join(configDir, argname, protoname+".pb") } data, err := loadFile(fullname) diff --git a/findFilename.go b/lookupPB.go index 9fb73a2..9fb73a2 100644 --- a/findFilename.go +++ b/lookupPB.go @@ -17,15 +17,22 @@ func ConfigSave(pb proto.Message) error { return saveTEXT(pb, "") } -func Save(pb proto.Message) error { +// writes the protobuf to disk +// uses the already configured Filename +func SavePB(pb proto.Message) error { fullname, err := GetFilename(pb) if err != nil { return err } - return SavePB(pb, fullname) + return SaveToFilename(pb, fullname) } -func SavePB(pb proto.Message, fullname string) error { +// writes the protobuf to disk (sets Filename if PB has 'Filename') +func SaveToFilename(pb proto.Message, fullname string) error { + basedir, _ := filepath.Split(fullname) + if err := os.MkdirAll(basedir, os.ModePerm); err != nil { + return err + } if strings.HasSuffix(fullname, ".pb") { return saveProto(pb, fullname) } diff --git a/verbose.go b/verbose.go new file mode 100644 index 0000000..4658dcf --- /dev/null +++ b/verbose.go @@ -0,0 +1,26 @@ +package config + +// this is an experiment at this point to +// see how this turns out + +func Verbose() bool { + // always use the config file value first + if configPB != nil { + found := configPB.FindByKey("Verbose") + + if found != nil { + if found.Value == "true" { + return true + } + return false + } + } + + // nothing in the config file. check argv + for _, v := range argv { + if v == "--verbose" { + return true + } + } + return false +} |
