summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJeff Carr <[email protected]>2025-10-27 00:26:10 -0500
committerJeff Carr <[email protected]>2025-10-27 00:26:10 -0500
commitdbe78b785508d8ef665d8b9c4d61e4dd8eb40e51 (patch)
treee25ef3eb97a435318f6e52a3709e3625ed20561c
parent4d0828def125f96d376cc1bd018bd3d8b0997901 (diff)
add some more details. not much more to do here.
-rw-r--r--etc.go1
-rw-r--r--formatENV.go6
-rw-r--r--init.go40
-rw-r--r--key.proto1
4 files changed, 41 insertions, 7 deletions
diff --git a/etc.go b/etc.go
index 339ba43..00ae426 100644
--- a/etc.go
+++ b/etc.go
@@ -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"
}
diff --git a/init.go b/init.go
index 2db551c..8868309 100644
--- a/init.go
+++ b/init.go
@@ -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)
}
}
diff --git a/key.proto b/key.proto
index 75921b1..187531a 100644
--- a/key.proto
+++ b/key.proto
@@ -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"
}