commit 899195ad02cd51dcf2cb190849947cb206d8b0e6
parent 214932508ac5a1e32a621758f3c12c38be296d2b
Author: Sean Enck <sean@ttypty.com>
Date: Sun, 6 Oct 2024 19:53:26 -0400
move around conditional shell statement creation
Diffstat:
5 files changed, 20 insertions(+), 10 deletions(-)
diff --git a/internal/app/completions.go b/internal/app/completions.go
@@ -54,7 +54,7 @@ type (
var shell embed.FS
func (e emptyShellPreparer) ShellIsNotConditional(s string) string {
- return fmt.Sprintf(config.ShellIsNotConditional, "1", s)
+ return config.ShellIsNotConditional("1", s)
}
func newGenOptions(defaults []string, kv map[string]shellPreparer) []CompletionOption {
@@ -97,7 +97,7 @@ func GenerateCompletions(completionType, exe string) ([]string, error) {
c.Conditionals.Not.ReadOnly = config.EnvReadOnly.ShellIsNotConditional(config.YesValue)
c.Conditionals.Not.NoClip = config.EnvNoClip.ShellIsNotConditional(config.YesValue)
c.Conditionals.Not.NoTOTP = config.EnvNoTOTP.ShellIsNotConditional(config.YesValue)
- c.Conditionals.Not.AskMode = config.KeyModeAskConditional()
+ c.Conditionals.Not.AskMode = config.KeyModeIsNotAskConditional()
c.Options = newGenOptions([]string{EnvCommand, HelpCommand, ListCommand, ShowCommand, VersionCommand, JSONCommand},
map[string]shellPreparer{
diff --git a/internal/config/core.go b/internal/config/core.go
@@ -34,8 +34,6 @@ const (
YesValue = yes
// TemplateVariable is used to handle '$' in shell vars (due to expansion)
TemplateVariable = "[%]"
- // ShellIsNotConditional is the simple shell conditional statement used for env compares in any shell
- ShellIsNotConditional = "[ \"%s\" != \"%s\" ]"
)
var (
@@ -510,7 +508,12 @@ func newDefaultedEnvironment[T any](val T, base environmentBase) environmentDefa
return obj
}
-// ShellIsNotConditional will produces a shell-ready conditional statement
+// ShellIsNotConditional will produce a shell-ready conditional statement for an environment variable
func (e environmentBase) ShellIsNotConditional(compareTo string) string {
- return fmt.Sprintf(ShellIsNotConditional, fmt.Sprintf("$%s", e.key()), compareTo)
+ return ShellIsNotConditional(fmt.Sprintf("$%s", e.key()), compareTo)
+}
+
+// ShellIsNotConditional will produce a shell-ready conditional statement
+func ShellIsNotConditional(key, compareTo string) string {
+ return fmt.Sprintf("[ \"%s\" != \"%s\" ]", key, compareTo)
}
diff --git a/internal/config/core_test.go b/internal/config/core_test.go
@@ -52,13 +52,20 @@ func TestKeyValue(t *testing.T) {
}
}
-func TestShellConditional(t *testing.T) {
+func TestShellIsNotConditionalEnv(t *testing.T) {
val := config.EnvStore.ShellIsNotConditional("x")
if val != `[ "$LOCKBOX_STORE" != "x" ]` {
t.Errorf("invalid conditiona: %s", val)
}
}
+func TestShellIsNotConditional(t *testing.T) {
+ val := config.ShellIsNotConditional("y", "x")
+ if val != `[ "y" != "x" ]` {
+ t.Errorf("invalid conditiona: %s", val)
+ }
+}
+
func TestNewPlatform(t *testing.T) {
for _, item := range config.Platforms.List() {
os.Setenv("LOCKBOX_PLATFORM", item)
diff --git a/internal/config/key.go b/internal/config/key.go
@@ -118,7 +118,7 @@ func (k Key) Read(ask AskPassword) (string, error) {
return key, nil
}
-// KeyModeAskConditional will get the key mode for 'ask' as a shell conditional
-func KeyModeAskConditional() string {
+// KeyModeIsNotAskConditional will get the key mode for 'ask' as a shell conditional
+func KeyModeIsNotAskConditional() string {
return envKeyMode.ShellIsNotConditional(string(askKeyMode))
}
diff --git a/internal/config/key_test.go b/internal/config/key_test.go
@@ -204,7 +204,7 @@ func TestCommandKey(t *testing.T) {
}
func TestKeyModeAskConditional(t *testing.T) {
- val := config.KeyModeAskConditional()
+ val := config.KeyModeIsNotAskConditional()
if val != `[ "$LOCKBOX_KEYMODE" != "ask" ]` {
t.Errorf("invalid value: %s", val)
}