From 5ae5010b004c97a5bfc8e5a3f9990af6ecc04290 Mon Sep 17 00:00:00 2001 From: Jeff Carr Date: Fri, 12 Sep 2025 02:03:07 -0500 Subject: [PATCH] cleaning up ENV handling --- configureENV.go | 120 ++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 111 insertions(+), 9 deletions(-) diff --git a/configureENV.go b/configureENV.go index 31973e2..156ed43 100644 --- a/configureENV.go +++ b/configureENV.go @@ -7,6 +7,27 @@ import ( "go.wit.com/log" ) +// BEFORE YOU READ ANYTHING UNDERSTAND THIS IS THE DEFAULT: +// +// ~/.config/ # put things you might care about here +// ~/.cache/ # this is never imporant and can be deleted at any time +// +// +// Additionally: +// +// NEVER WRITE OUT ANYTHING TO ~/ EVER. The ONLY DIRECTORIES YOU ARE EVER ALLOWED TO USE ARE ~/.config and ~/.cache/ (maybe ~/.local but really, why?) +// +// There are no exceptions to this unless you are a jerk or don't know better (in which you will be easily forgiven -- don't worry. there isn't a manual) +// +// +// ADVICE FOR ALL FUTURE PROGRAMMERS: +// +// +// Config files are a perfect way to stop things from proceeding. You do NOT want options in your programs. Configuration options, if you do +// things correctly, should never actually be needed. Nonetheless, there are initial states. ISOLATE ALL OPTIONS TO THE ITIAL STATES. +// then, never let anthing change anything. If your code is correct, then all is good. +// +// // This defines the "default" behavior for forge when doing GO lang development // // Since this code is common, it's in a seperate package so it can be used elsewhere @@ -19,7 +40,7 @@ import ( // This routine ensures the following ENV vars are set: // // FORGE_CONFIG == where forge's configs are stored (normally ~/.config/forge) -// FORGE_REPOPB == where the repos.pb protobuf cache file is stored (normally ~/.cache/forge/repos.pb) +// FORGE_REPOSPB == where the repos.pb protobuf cache file is stored (normally ~/.cache/forge/repos.pb) // FORGE_GOSRC == based on the path, what the user probably want for developing in GO (Defaults to ~/go/src) // FORGE_GOWORK == true or false depending on the GOSRC result // @@ -32,13 +53,15 @@ import ( func ConfigureENV() error { err := doConfigureENV() if os.Getenv("FORGE_VERBOSE") == "true" { - log.Printf("FORGE_CONFIG = %s\n", os.Getenv("FORGE_CONFIG")) - log.Printf("FORGE_REPOPB = %s\n", os.Getenv("FORGE_REPOPB")) - log.Printf("FORGE_PATCHDIR = %s\n", os.Getenv("FORGE_PATCHDIR")) - log.Printf("FORGE_URL = %s\n", os.Getenv("FORGE_URL")) - log.Printf("FORGE_GOWORK = %v\n", os.Getenv("FORGE_GOWORK")) - log.Printf("FORGE_VERBOSE = %s\n", os.Getenv("FORGE_VERBOSE")) - log.Printf("HOSTNAME = %s\n", os.Getenv("HOSTNAME")) + log.Printf("fhelp init: FORGE_CONFIG = %s\n", os.Getenv("FORGE_CONFIG")) + log.Printf("fhelp init: FORGE_REPOSDIR = %s\n", os.Getenv("FORGE_REPOSDIR")) + log.Printf("fhelp init: FORGE_REPOSPB = %s\n", os.Getenv("FORGE_REPOSPB")) + log.Printf("fhelp init: FORGE_PATCHDIR = %s\n", os.Getenv("FORGE_PATCHDIR")) + log.Printf("fhelp init: FORGE_URL = %s\n", os.Getenv("FORGE_URL")) + log.Printf("fhelp init: FORGE_GOWORK = %v\n", os.Getenv("FORGE_GOWORK")) + log.Printf("fhelp init: FORGE_VERBOSE = %s\n", os.Getenv("FORGE_VERBOSE")) + log.Printf("fhelp init: HOSTNAME = %s\n", os.Getenv("HOSTNAME")) + os.Exit(-1) } return err } @@ -47,9 +70,12 @@ func ConfigureENV() error { // always set them to _something_ even when everything seems to be failing func doConfigureENV() error { var anyerr error + if os.Getenv("FORGE_CONFIG") != "" { + return nil + } // setup the forge config dir if os.Getenv("FORGE_CONFIG") == "" { - homeDir, err := os.UserHomeDir() + homeDir, err := getConfigDir() if err == nil { fullpath := filepath.Join(homeDir, ".config/forge") os.MkdirAll(fullpath, os.ModePerm) @@ -61,6 +87,26 @@ func doConfigureENV() error { } } + if os.Getenv("FORGE_REPOSDIR") == "" { + reposDir, err := getReposDir() + if err != nil { + return err + } + os.Setenv("FORGE_REPOSDIR", reposDir) + } + + if os.Getenv("FORGE_REPOSPB") == "" { + pbdir, err := getCacheDir() + if err != nil { + return err + } + os.Setenv("FORGE_REPOSPB", filepath.Join(pbdir, "repos.pb")) + } + + if os.Getenv("FORGE_PATCHDIR") == "" { + os.Setenv("FORGE_PATCHDIR", os.Getenv("FORGE_REPOSDIR")) + } + // setting FORGE_URL if os.Getenv("FORGE_URL") == "" { os.Setenv("FORGE_URL", "https://forge.wit.com/") @@ -77,6 +123,62 @@ func doConfigureENV() error { return anyerr } +func getConfigDir() (string, error) { + homeDir, err := os.UserHomeDir() + if err != nil { + return "/tmp", err + } + fullpath := filepath.Join(homeDir, ".config/forge") + err = os.MkdirAll(fullpath, os.ModePerm) + if err != nil { + return "/tmp", err + } + + return fullpath, nil +} + +func getCacheDir() (string, error) { + homeDir, err := os.UserHomeDir() + if err != nil { + return "/tmp", err + } + fullpath := filepath.Join(homeDir, ".cache/forge") + err = os.MkdirAll(fullpath, os.ModePerm) + if err != nil { + return "/tmp", err + } + + return fullpath, nil +} + +func getReposDir() (string, error) { + homeDir, err := os.UserHomeDir() + if err != nil { + return "/tmp", err + } + fullpath := filepath.Join(homeDir, "go/src") + err = os.MkdirAll(fullpath, os.ModePerm) + if err != nil { + return "/tmp", err + } + + return fullpath, nil +} + +func getCachegDir() (string, error) { + homeDir, err := os.UserHomeDir() + if err != nil { + return "/tmp", err + } + fullpath := filepath.Join(homeDir, ".cache/forge") + err = os.MkdirAll(fullpath, os.ModePerm) + if err != nil { + return "/tmp", err + } + + return fullpath, nil +} + // this is the 'old way" and works fine for me. I use it because I like the ~/go/src directory // because I know exactly what is in it: GO stuff & nothing else func useGoSrc() (string, error) {