diff options
| author | Jeff Carr <[email protected]> | 2025-10-27 00:26:10 -0500 |
|---|---|---|
| committer | Jeff Carr <[email protected]> | 2025-10-27 00:26:10 -0500 |
| commit | dbe78b785508d8ef665d8b9c4d61e4dd8eb40e51 (patch) | |
| tree | e25ef3eb97a435318f6e52a3709e3625ed20561c | |
| parent | 4d0828def125f96d376cc1bd018bd3d8b0997901 (diff) | |
add some more details. not much more to do here.
| -rw-r--r-- | etc.go | 1 | ||||
| -rw-r--r-- | formatENV.go | 6 | ||||
| -rw-r--r-- | init.go | 40 | ||||
| -rw-r--r-- | key.proto | 1 |
4 files changed, 41 insertions, 7 deletions
@@ -54,6 +54,7 @@ func addEtcFile(global string, data string) { } varname := parts[0] value := parts[1] + value = strings.Trim(value, "'\"") SetGlobal(global, varname, value) } } diff --git a/formatENV.go b/formatENV.go index ea2931c..6a2ab79 100644 --- a/formatENV.go +++ b/formatENV.go @@ -3,6 +3,7 @@ package env import ( "errors" "fmt" + "path/filepath" "strings" ) @@ -39,6 +40,11 @@ func formatENV() (string, error) { // don't write out anything defined globally continue } + // turns "/home/user/" into ~/ + relpath, err := filepath.Rel(envPB.HomeDir, c.Value) + if err == nil { + c.Value = filepath.Join("~", relpath) + } line := fmt.Sprintf("%s=%s", c.Var, c.Value) out += line + "\n" } @@ -24,6 +24,7 @@ func Init(appname, version, buildtime string, fromargv []string, goodFunc func(s } envPB = NewKeys() envPB.Init = true + envPB.HomeDir, _ = os.UserHomeDir() loadAppENV() @@ -34,16 +35,14 @@ func Init(appname, version, buildtime string, fromargv []string, goodFunc func(s if err == nil { SetGlobal("lib/env", "username", usr.Username) } - homeDir, err := os.UserHomeDir() + homedir, err := os.UserHomeDir() if err == nil { - SetGlobal("lib/env", "homeDir", homeDir) + SetGlobal("lib/env", "homedir", homedir) } } // if it exists, loads ~/.config/<appname>/<appname>rc func loadAppENV() error { - saveMu.Lock() - saveMu.Unlock() configDir, err := os.UserConfigDir() if err != nil { return err @@ -74,11 +73,19 @@ func InitValid() bool { return envPB.Init } +// only allow super simple files func parseENV(data string) { + var helptext string // fmt.Println("loadENV()", filename) for _, line := range strings.Split(data, "\n") { line = strings.TrimSpace(line) if line == "" { + // ignore empty lines + continue + } + if strings.HasPrefix(line, "#") { + // save notes & instructions from the config file + helptext += line + "\n" continue } parts := strings.Split(line, "=") @@ -86,10 +93,29 @@ func parseENV(data string) { // fmt.Println("INVALID LINE:", i, line) continue } + varname := parts[0] + varname = strings.TrimSpace(varname) + if varname == "" { + // ignore empty varname + continue + } + + // check for duplicates here + found := envPB.FindByVar(varname) + if found != nil { + // varname alrady set + continue + } + + value := parts[1] + value = strings.Trim(value, "'\"") + saveMu.Lock() + saveMu.Unlock() c := new(Key) - c.Var = parts[0] - c.Value = parts[1] + c.Var = varname + c.Value = value + c.Help = helptext + helptext = "" envPB.Append(c) - // fmt.Printf("ENV LINE: (%v)\n", c) } } @@ -17,4 +17,5 @@ message Keys { // `autogenpb:marshal` `autogenpb:nomu repeated Key keys = 3; string filename = 4; // can store where the filename is so that saves can be automated bool init = 5; // can store where the filename is so that saves can be automated + string homeDir = 6; // always need this. Converts "~" into "/home/turing" } |
