package config import ( "os" "path/filepath" "strings" "sync" ) // an experiment to see if this is useful var saveMu sync.RWMutex // returns true if config is working okay func InitValid() bool { saveMu.Lock() defer saveMu.Unlock() if configPB == nil { // todo: try to re-init it here return false } return true } // saves your applications config file func Save() error { saveMu.Lock() defer saveMu.Unlock() basedir, _ := filepath.Split(configPB.Filename) if err := os.MkdirAll(basedir, os.ModePerm); err != nil { return err } err := SavePB(configPB) return err } func Get(flag string) string { saveMu.Lock() defer saveMu.Unlock() if configPB == nil { return "" } c := findByLower(flag) if c == nil { return "" } return c.Value } func findByLower(lookingFor string) *Config { for c := range configPB.IterAll() { if strings.ToLower(c.Key) == strings.ToLower(lookingFor) { return c } } return nil } func True(flag string) bool { saveMu.Lock() defer saveMu.Unlock() if configPB == nil { return false } found := configPB.FindByKey(flag) if found == nil { return false } if strings.ToLower(found.Value) == "true" { return true } return false } func Set(key string, newValue string) error { saveMu.Lock() defer saveMu.Unlock() if configPB == nil { return NotInitialized } found := configPB.FindByKey(key) if found != nil { found.Value = newValue } newvar := new(Config) newvar.Key = key newvar.Value = newValue configPB.Append(newvar) return nil }