diff options
| author | Jeff Carr <[email protected]> | 2025-10-20 05:50:38 -0500 |
|---|---|---|
| committer | Jeff Carr <[email protected]> | 2025-10-20 05:50:38 -0500 |
| commit | 8a24584262a90956e012cee7b03c8c2b9f4b794c (patch) | |
| tree | 8b7d0afd04e717d7c07da5eb2894debd74ec2307 /config.Load.go | |
| parent | b6e93c08d601a7a6c27a0fdcdf98f6cb7dc9ccd8 (diff) | |
reworking this to make it more sane. hopefully.
Diffstat (limited to 'config.Load.go')
| -rw-r--r-- | config.Load.go | 81 |
1 files changed, 81 insertions, 0 deletions
diff --git a/config.Load.go b/config.Load.go new file mode 100644 index 0000000..b960508 --- /dev/null +++ b/config.Load.go @@ -0,0 +1,81 @@ +package config + +import ( + "errors" + "os" + "os/user" + "path/filepath" +) + +// loads your applications config file from +// ~/.config/<appname>/config.text +func (pb *Config) Load() error { + appname, err := GetAppname() // already configured by your application + if err != nil { + return err + } + + configdir, err := getConfigDir() + if err != nil { + return err + } + + filename := filepath.Join(configdir, appname+".text") + _, err = SetFilename(pb, filename) + if err != nil { + return err + } + + saveMu.Lock() + defer saveMu.Unlock() + err = loadTEXT(pb, filename) + return err +} + +func GetAppname() (string, error) { + if APPNAME != "" { + return APPNAME, nil + } + return "", errors.New("your application must setup config.Init()") +} + +func GetUsername() string { + if Get("username") != "" { + return Get("username") + } + usr, _ := user.Current() + if usr.Username != "" { + return usr.Username + } + return "notsure" // OS Idiocracy +} + +func getCacheDir() (string, error) { + if Get("cacheDir") != "" { + return Get("cacheDir"), nil + } + + cacheDir, _ := os.UserCacheDir() + + appname, err := GetAppname() // application should have already configured this + if err != nil { + return cacheDir, err + } + + return filepath.Join(cacheDir, appname), nil +} + +func getConfigDir() (string, error) { + if Get("configDir") != "" { + return Get("configDir"), nil + } + + configDir, _ := os.UserConfigDir() + + appname, err := GetAppname() // application should have already configured this + if err != nil { + return configDir, err + } + + return filepath.Join(configDir, appname), nil +} |
