package ENV import ( "os" "path/filepath" "strings" ) // this is an experiment at this point to // see how this turns out // normally called by "argv" (go.wit.com/lib/protobuf/argvpb) func Init(appname, version, buildtime string, fromargv []string, goodFunc func(string), badFunc func(string, error)) { APPNAME = appname VERSION = version BUILDTIME = buildtime argv = fromargv goodExit = goodFunc badExit = badFunc if envPB != nil { // log.Info("Init() already ran") return } envPB = NewKeys() envPB.Init = true loadAppENV() SetGlobal("lib/ENV", "APPNAME", APPNAME) SetGlobal("lib/ENV", "VERSION", VERSION) SetGlobal("lib/ENV", "BUILDTIME", BUILDTIME) } // if it exists, loads ~/.config//.ENV func loadAppENV() error { saveMu.Lock() saveMu.Unlock() configDir, err := os.UserConfigDir() if err != nil { return err } envPB.Filename = filepath.Join(configDir, APPNAME, APPNAME+".ENV") // log.Info("envPB.Filename", envPB.Filename) data, err := os.ReadFile(envPB.Filename) if err != nil { return err } parseENV(string(data)) return err } func InitValid() bool { if envPB == nil { // todo: track that the application did not init envPB = NewKeys() return false } return envPB.Init } func parseENV(data string) { // log.Info("loadENV()", filename) for _, line := range strings.Split(data, "\n") { line = strings.TrimSpace(line) if line == "" { continue } parts := strings.Split(line, "=") if len(parts) != 2 { // log.Info("INVALID LINE:", i, line) continue } c := new(Key) c.Var = parts[0] c.Value = parts[1] envPB.Append(c) // log.Printf("ENV LINE: (%v)\n", c) } }