lockbox

password manager
Log | Files | Refs | README | LICENSE

commit 68b7a334fd5c47ffb4af0f5fac30ad2c9b4bb6e6
parent 8b9038cdab232b7ff143b106f5fb57d9605a85d7
Author: Sean Enck <sean@ttypty.com>
Date:   Sat, 29 Jul 2023 11:44:40 -0400

use a registry to limit env output

Diffstat:
Minternal/app/info_test.go | 2+-
Minternal/config/core.go | 11++++++++---
Minternal/config/core_test.go | 5+++--
Minternal/config/vars.go | 3++-
4 files changed, 14 insertions(+), 7 deletions(-)

diff --git a/internal/app/info_test.go b/internal/app/info_test.go @@ -82,7 +82,7 @@ func TestEnvInfo(t *testing.T) { if buf.String() != "" { t.Error("nothing written") } - os.Setenv("LOCKBOX_TEST", "1") + os.Setenv("LOCKBOX_STORE", "1") ok, err = app.Info(&buf, "env", []string{}) if !ok || err != nil { t.Errorf("invalid error: %v", err) diff --git a/internal/config/core.go b/internal/config/core.go @@ -329,11 +329,16 @@ func IsUnset(k, v string) (bool, error) { func Environ() []string { var results []string for _, k := range os.Environ() { - if strings.HasPrefix(k, prefixKey) { - if strings.HasPrefix(k, fmt.Sprintf("%s=", EnvConfig.key)) { + for _, r := range registry { + key := r.self().key + if key == EnvConfig.key { continue } - results = append(results, k) + key = fmt.Sprintf("%s=", key) + if strings.HasPrefix(k, key) { + results = append(results, k) + break + } } } sort.Strings(results) diff --git a/internal/config/core_test.go b/internal/config/core_test.go @@ -148,11 +148,12 @@ func TestEnviron(t *testing.T) { if len(e) != 0 { t.Error("invalid environ") } - os.Setenv("LOCKBOX_1", "1") + os.Setenv("LOCKBOX_STORE", "1") os.Setenv("LOCKBOX_2", "2") + os.Setenv("LOCKBOX_KEY", "2") os.Setenv("LOCKBOX_ENV", "2") e = config.Environ() - if len(e) != 2 || fmt.Sprintf("%v", e) != "[LOCKBOX_1=1 LOCKBOX_2=2]" { + if len(e) != 2 || fmt.Sprintf("%v", e) != "[LOCKBOX_KEY=2 LOCKBOX_STORE=1]" { t.Errorf("invalid environ: %v", e) } } diff --git a/internal/config/vars.go b/internal/config/vars.go @@ -32,6 +32,7 @@ const ( ) var ( + registry = []printer{EnvStore, envKeyMode, envKey, EnvNoClip, EnvNoColor, EnvInteractive, EnvReadOnly, EnvTOTPToken, EnvFormatTOTP, EnvMaxTOTP, EnvTOTPColorBetween, EnvClipPaste, EnvClipCopy, EnvClipMax, EnvPlatform, EnvNoTOTP, EnvHookDir, EnvClipOSC52, EnvKeyFile, EnvModTime, EnvJSONDataOutput, EnvHashLength, EnvConfig, envConfigExpands} // Platforms represent the platforms that lockbox understands to run on Platforms = []string{MacOSPlatform, WindowsLinuxPlatform, LinuxXPlatform, LinuxWaylandPlatform} // TOTPDefaultColorWindow is the default coloring rules for totp @@ -153,7 +154,7 @@ func GetKey() ([]byte, error) { // ListEnvironmentVariables will print information about env variables func ListEnvironmentVariables() []string { var results []string - for _, item := range []printer{EnvStore, envKeyMode, envKey, EnvNoClip, EnvNoColor, EnvInteractive, EnvReadOnly, EnvTOTPToken, EnvFormatTOTP, EnvMaxTOTP, EnvTOTPColorBetween, EnvClipPaste, EnvClipCopy, EnvClipMax, EnvPlatform, EnvNoTOTP, EnvHookDir, EnvClipOSC52, EnvKeyFile, EnvModTime, EnvJSONDataOutput, EnvHashLength, EnvConfig, envConfigExpands} { + for _, item := range registry { env := item.self() value, allow := item.values() if len(value) == 0 {