commit efba5f3f660f14a010fd52a34a08f981b60aedc4
parent 5721dd33a22da318fbba6deb92e4cd8164cb1888
Author: Sean Enck <sean@ttypty.com>
Date: Fri, 28 Jul 2023 19:39:35 -0400
expand env vars properly
Diffstat:
3 files changed, 21 insertions(+), 8 deletions(-)
diff --git a/cmd/main.go b/cmd/main.go
@@ -61,7 +61,12 @@ func run() error {
return err
}
for k, v := range found {
- os.Setenv(k, v)
+ os.Setenv(k, os.Expand(v, func(in string) string {
+ if val, ok := found[in]; ok {
+ return val
+ }
+ return os.Getenv(in)
+ }))
}
break
}
diff --git a/internal/config/core.go b/internal/config/core.go
@@ -90,8 +90,12 @@ func shlex(in string) ([]string, error) {
return shell.Fields(in, os.Getenv)
}
+func getExpand(key string) string {
+ return os.ExpandEnv(os.Getenv(key))
+}
+
func environOrDefault(envKey, defaultValue string) string {
- val := os.Getenv(envKey)
+ val := getExpand(envKey)
if strings.TrimSpace(val) == "" {
return defaultValue
}
@@ -100,7 +104,7 @@ func environOrDefault(envKey, defaultValue string) string {
// Get will get the boolean value for the setting
func (e EnvironmentBool) Get() (bool, error) {
- read := strings.ToLower(strings.TrimSpace(os.Getenv(e.key)))
+ read := strings.ToLower(strings.TrimSpace(getExpand(e.key)))
switch read {
case no:
return false, nil
@@ -116,7 +120,7 @@ func (e EnvironmentBool) Get() (bool, error) {
// Get will get the integer value for the setting
func (e EnvironmentInt) Get() (int, error) {
val := e.defaultValue
- use := os.Getenv(e.key)
+ use := getExpand(e.key)
if use != "" {
i, err := strconv.Atoi(use)
if err != nil {
@@ -144,7 +148,7 @@ func (e EnvironmentInt) Get() (int, error) {
// Get will read the string from the environment
func (e EnvironmentString) Get() string {
if !e.canDefault {
- return os.Getenv(e.key)
+ return getExpand(e.key)
}
return environOrDefault(e.key, e.defaultValue)
}
@@ -225,8 +229,8 @@ func NewPlatform() (SystemPlatform, error) {
if strings.Contains(raw, "microsoft-standard-wsl") {
return WindowsLinuxPlatform, nil
}
- if strings.TrimSpace(os.Getenv("WAYLAND_DISPLAY")) == "" {
- if strings.TrimSpace(os.Getenv("DISPLAY")) == "" {
+ if strings.TrimSpace(getExpand("WAYLAND_DISPLAY")) == "" {
+ if strings.TrimSpace(getExpand("DISPLAY")) == "" {
return unknownPlatform, errors.New("unable to detect linux clipboard mode")
}
return LinuxXPlatform, nil
diff --git a/tests/run.sh b/tests/run.sh
@@ -96,8 +96,12 @@ _unset() {
}
_config() {
- env | grep '^LOCKBOX' > "$ENV"
+ {
+ echo "PLAINTEXT=text"
+ env | grep '^LOCKBOX' | sed 's/plaintext/$LOCKBOX_FAKE_TEST$PLAINTEXT/g'
+ } > "$ENV"
_unset
+ export LOCKBOX_FAKE_TEST=plain
${LB_BINARY} ls
export LOCKBOX_ENV="$ENV"
${LB_BINARY} ls