commit 3ff0b22e0ad31ac8998b19fff9144d3d14870cba
parent 7b35fc4b5fe720dd3e48acc53140f3eaaa892ecc
Author: Sean Enck <sean@ttypty.com>
Date: Wed, 2 Jul 2025 11:32:36 -0400
fixup prompt for inputs to ask for the requested field, removed unused code paths while at it
Diffstat:
6 files changed, 28 insertions(+), 35 deletions(-)
diff --git a/internal/app/core.go b/internal/app/core.go
@@ -23,7 +23,7 @@ type (
UserInputOptions interface {
CommandOptions
IsPipe() bool
- Input(bool) ([]byte, error)
+ Input(bool, string) ([]byte, error)
}
// DefaultCommand is the default CLI app type for actual execution
@@ -72,27 +72,12 @@ func Die(msg string) {
os.Exit(1)
}
-// SetArgs allow updating the command args
-func (a *DefaultCommand) SetArgs(args ...string) {
- a.args = args
-}
-
// IsPipe will indicate if we're receiving pipe input
func (a *DefaultCommand) IsPipe() bool {
return platform.IsInputFromPipe()
}
-// ReadLine handles a single stdin read
-func (a DefaultCommand) ReadLine() (string, error) {
- return platform.Stdin(true)
-}
-
-// Password is how a keyer gets the user's password for rekey
-func (a DefaultCommand) Password() (string, error) {
- return platform.ReadInteractivePassword()
-}
-
// Input will read user input
-func (a *DefaultCommand) Input(interactive bool) ([]byte, error) {
- return platform.GetUserInputPassword(interactive)
+func (a *DefaultCommand) Input(interactive bool, prompt string) ([]byte, error) {
+ return platform.GetUserInput(interactive, prompt)
}
diff --git a/internal/app/insert.go b/internal/app/insert.go
@@ -40,7 +40,7 @@ func Insert(cmd UserInputOptions) error {
}
}
}
- password, err := cmd.Input(!isPipe && !strings.EqualFold(base, kdbx.NotesField))
+ password, err := cmd.Input(!isPipe && !strings.EqualFold(base, kdbx.NotesField), base)
if err != nil {
return fmt.Errorf("invalid input: %w", err)
}
diff --git a/internal/app/insert_test.go b/internal/app/insert_test.go
@@ -17,6 +17,7 @@ type (
input func() ([]byte, error)
pipe func() bool
token func() string
+ prompt string
interactive bool
}
)
@@ -35,8 +36,9 @@ func (m *mockInsert) IsPipe() bool {
return m.pipe()
}
-func (m *mockInsert) Input(interactive bool) ([]byte, error) {
+func (m *mockInsert) Input(interactive bool, prompt string) ([]byte, error) {
m.interactive = interactive
+ m.prompt = prompt
return m.input()
}
@@ -119,6 +121,9 @@ func TestInsertDo(t *testing.T) {
if m.command.buf.String() == "" {
t.Error("invalid insert")
}
+ if m.prompt != "password" {
+ t.Error("invalid field prompt")
+ }
m.command.confirm = false
m.command.buf = bytes.Buffer{}
m.command.args = []string{"test/test2/test1/password"}
@@ -147,4 +152,7 @@ func TestInsertDo(t *testing.T) {
if m.command.buf.String() == "" || m.interactive {
t.Errorf("invalid insert %s %v", m.command.buf.String(), m.interactive)
}
+ if m.prompt != "notes" {
+ t.Error("invalid field prompt")
+ }
}
diff --git a/internal/app/rekey.go b/internal/app/rekey.go
@@ -24,7 +24,7 @@ func ReKey(cmd UserInputOptions) error {
}
var pass string
if !vars.NoKey {
- p, err := cmd.Input(!piping)
+ p, err := cmd.Input(!piping, "password")
if err != nil {
return err
}
diff --git a/internal/app/rekey_test.go b/internal/app/rekey_test.go
@@ -32,7 +32,7 @@ func (m *mockKeyer) Args() []string {
return m.args
}
-func (m *mockKeyer) Input(bool) ([]byte, error) {
+func (m *mockKeyer) Input(bool, string) ([]byte, error) {
return []byte(m.pass), nil
}
diff --git a/internal/platform/os.go b/internal/platform/os.go
@@ -41,26 +41,26 @@ func termEcho(on bool) {
}
}
-// GetUserInputPassword will read the user's input from stdin via multiple means.
-func GetUserInputPassword(interactive bool) ([]byte, error) {
- var password string
+// GetUserInput will read the user's input from stdin via multiple means.
+func GetUserInput(interactive bool, prompt string) ([]byte, error) {
+ var value string
if interactive {
- input, err := confirmInputsMatch()
+ input, err := confirmInputsMatch(prompt)
if err != nil {
return nil, err
}
- password = input
+ value = input
} else {
input, err := Stdin(false)
if err != nil {
return nil, err
}
- password = input
+ value = input
}
- if password == "" {
- return nil, errors.New("password can NOT be empty")
+ if value == "" {
+ return nil, fmt.Errorf("%s can NOT be empty", prompt)
}
- return []byte(password), nil
+ return []byte(value), nil
}
// ReadInteractivePassword will prompt for a single password for unlocking
@@ -73,23 +73,23 @@ func ReadInteractivePassword() (string, error) {
return Stdin(true)
}
-func confirmInputsMatch() (string, error) {
+func confirmInputsMatch(prompt string) (string, error) {
termEcho(false)
defer func() {
termEcho(true)
}()
- fmt.Print("please enter password: ")
+ fmt.Printf("please enter %s: ", prompt)
first, err := Stdin(true)
if err != nil {
return "", err
}
- fmt.Print("\nplease re-enter password: ")
+ fmt.Printf("\nplease re-enter %s: ", prompt)
second, err := Stdin(true)
if err != nil {
return "", err
}
if first != second {
- return "", errors.New("passwords do NOT match")
+ return "", fmt.Errorf("%s does NOT match", prompt)
}
return first, nil
}