package config import ( "errors" "os" "os/user" "path/filepath" ) // loads your applications config file from // ~/.config//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 }