package env import ( "os" "os/user" "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 { // fmt.Println("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) usr, err := user.Current() if err == nil { SetGlobal("lib/env", "username", usr.Username) } homeDir, err := os.UserHomeDir() if err == nil { SetGlobal("lib/env", "homeDir", homeDir) } } // if it exists, loads ~/.config//rc func loadAppENV() error { saveMu.Lock() saveMu.Unlock() configDir, err := os.UserConfigDir() if err != nil { return err } { // old way. deprecate in 2026 envPB.Filename = filepath.Join(configDir, APPNAME, APPNAME+".ENV") data, err := os.ReadFile(envPB.Filename) if err == nil { parseENV(string(data)) } } envPB.Filename = filepath.Join(configDir, APPNAME, APPNAME+"rc") data, err := os.ReadFile(envPB.Filename) if err == nil { 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) { // fmt.Println("loadENV()", filename) for _, line := range strings.Split(data, "\n") { line = strings.TrimSpace(line) if line == "" { continue } parts := strings.Split(line, "=") if len(parts) != 2 { // fmt.Println("INVALID LINE:", i, line) continue } c := new(Key) c.Var = parts[0] c.Value = parts[1] envPB.Append(c) // fmt.Printf("ENV LINE: (%v)\n", c) } }