commit 49c2852099d474614d276f7aa70563b076ccb733
parent 85d2d8ee9f09c7805dc1858385480f41ab6e48cb
Author: Sean Enck <sean@ttypty.com>
Date: Fri, 28 Jul 2023 21:22:34 -0400
handle better set/unset variables
Diffstat:
4 files changed, 77 insertions(+), 10 deletions(-)
diff --git a/cmd/main.go b/cmd/main.go
@@ -61,12 +61,20 @@ func run() error {
return err
}
for k, v := range found {
- os.Setenv(k, os.Expand(v, func(in string) string {
- if val, ok := found[in]; ok {
- return val
+ ok, err := config.IsUnset(k, v)
+ if err != nil {
+ return err
+ }
+ if !ok {
+ if err := os.Setenv(k, os.Expand(v, func(in string) string {
+ if val, ok := found[in]; ok {
+ return val
+ }
+ return os.Getenv(in)
+ })); err != nil {
+ return err
}
- return os.Getenv(in)
- }))
+ }
}
break
}
diff --git a/internal/app/rekey.go b/internal/app/rekey.go
@@ -79,7 +79,9 @@ func ReKey(cmd CommandOptions, r Keyer) error {
if !cmd.Confirm("proceed with rekey") {
return nil
}
- config.EnvJSONDataOutput.Set(string(config.JSONDataOutputRaw))
+ if err := config.EnvJSONDataOutput.Set(string(config.JSONDataOutputRaw)); err != nil {
+ return err
+ }
entries, err := r.JSON()
if err != nil {
return err
diff --git a/internal/config/core.go b/internal/config/core.go
@@ -168,9 +168,16 @@ func (e environmentBase) KeyValue(value string) string {
return fmt.Sprintf("%s=%s", e.key, value)
}
-// Set will do an environment set for the value to key
-func (e environmentBase) Set(value string) {
- os.Setenv(e.key, value)
+// Setenv will do an environment set for the value to key
+func (e environmentBase) Set(value string) error {
+ unset, err := IsUnset(e.key, value)
+ if err != nil {
+ return err
+ }
+ if unset {
+ return nil
+ }
+ return os.Setenv(e.key, value)
}
// Get will retrieve the value with the formatted input included
@@ -287,7 +294,9 @@ func NewEnvFiles() ([]string, error) {
if v == "" || v == noEnvironment {
return []string{}, nil
}
- EnvConfig.Set("")
+ if err := EnvConfig.Set(noEnvironment); err != nil {
+ return nil, err
+ }
if v != detectEnvironment {
return []string{v}, nil
}
@@ -301,3 +310,11 @@ func NewEnvFiles() ([]string, error) {
}
return results, nil
}
+
+// IsUnset will indicate if a variable is an unset (and unset it) or return that it isn't
+func IsUnset(k, v string) (bool, error) {
+ if strings.TrimSpace(v) == "" {
+ return true, os.Unsetenv(k)
+ }
+ return false, nil
+}
diff --git a/internal/config/core_test.go b/internal/config/core_test.go
@@ -1,7 +1,9 @@
package config_test
import (
+ "fmt"
"os"
+ "strings"
"testing"
"github.com/enckse/lockbox/internal/config"
@@ -13,6 +15,15 @@ func TestPlatformSet(t *testing.T) {
}
}
+func isSet(key string) bool {
+ for _, item := range os.Environ() {
+ if strings.HasPrefix(item, fmt.Sprintf("%s=", key)) {
+ return true
+ }
+ }
+ return false
+}
+
func TestSet(t *testing.T) {
os.Clearenv()
defer os.Clearenv()
@@ -20,6 +31,13 @@ func TestSet(t *testing.T) {
if config.EnvStore.Get() != "TEST" {
t.Errorf("invalid set/get")
}
+ if !isSet("LOCKBOX_STORE") {
+ t.Error("should be set")
+ }
+ config.EnvStore.Set("")
+ if isSet("LOCKBOX_STORE") {
+ t.Error("should be set")
+ }
}
func TestKeyValue(t *testing.T) {
@@ -101,3 +119,25 @@ func TestNewEnvFiles(t *testing.T) {
t.Errorf("invalid files: %v %v", f, err)
}
}
+
+func TestIsUnset(t *testing.T) {
+ os.Clearenv()
+ o, err := config.IsUnset("test", " ")
+ if err != nil || !o {
+ t.Error("was unset")
+ }
+ o, err = config.IsUnset("test", "")
+ if err != nil || !o {
+ t.Error("was unset")
+ }
+ o, err = config.IsUnset("test", "a")
+ if err != nil || o {
+ t.Error("was set")
+ }
+ os.Setenv("UNSET_TEST", "abc")
+ defer os.Clearenv()
+ config.IsUnset("UNSET_TEST", "")
+ if isSet("UNSET_TEST") {
+ t.Error("found unset var")
+ }
+}