commit fa5d68adb4ee5b8470f10b1003f27c0d1c6d254f
parent d63ff393612e66398ce8442281aeb2e8e069ef12
Author: Sean Enck <sean@ttypty.com>
Date: Fri, 3 Mar 2023 18:56:02 -0500
refactor insert to take functions
Diffstat:
3 files changed, 22 insertions(+), 9 deletions(-)
diff --git a/cmd/main.go b/cmd/main.go
@@ -78,7 +78,13 @@ func run() error {
case cli.MoveCommand:
return app.Move(t, sub, confirm)
case cli.InsertCommand:
- return app.Insert(os.Stdout, t, sub, confirm)
+ insert := app.InsertOptions{}
+ insert.Confirm = confirm
+ insert.IsPipe = inputs.IsInputFromPipe
+ insert.IsNoTOTP = inputs.IsNoTOTP
+ insert.TOTPToken = inputs.TOTPToken
+ insert.Input = inputs.GetUserInputPassword
+ return app.Insert(os.Stdout, t, sub, insert)
case cli.RemoveCommand:
return app.Remove(os.Stdout, t, sub, confirm)
case cli.StatsCommand:
diff --git a/internal/app/core.go b/internal/app/core.go
@@ -4,4 +4,12 @@ package app
type (
// Confirm user inputs
Confirm func(string) bool
+ // InsertOptions are functions required for insert
+ InsertOptions struct {
+ IsNoTOTP func() (bool, error)
+ IsPipe func() bool
+ TOTPToken func() string
+ Input func(bool, bool) ([]byte, error)
+ Confirm Confirm
+ }
)
diff --git a/internal/app/insert.go b/internal/app/insert.go
@@ -9,7 +9,6 @@ import (
"github.com/enckse/lockbox/internal/backend"
"github.com/enckse/lockbox/internal/cli"
- "github.com/enckse/lockbox/internal/inputs"
"github.com/enckse/lockbox/internal/totp"
)
@@ -19,7 +18,7 @@ func insertError(message string, err error) error {
// Insert will insert new entries
// NOTE: almost entirely tested via regresssion due to complexities around piping/inputs
-func Insert(w io.Writer, t *backend.Transaction, args []string, confirm Confirm) error {
+func Insert(w io.Writer, t *backend.Transaction, args []string, cmd InsertOptions) error {
multi := false
isTOTP := false
idx := 0
@@ -33,7 +32,7 @@ func Insert(w io.Writer, t *backend.Transaction, args []string, confirm Confirm)
case cli.InsertMultiCommand:
multi = true
case cli.InsertTOTPCommand:
- off, err := inputs.IsNoTOTP()
+ off, err := cmd.IsNoTOTP()
if err != nil {
return err
}
@@ -49,10 +48,10 @@ func Insert(w io.Writer, t *backend.Transaction, args []string, confirm Confirm)
default:
return errors.New("too many arguments")
}
- isPipe := inputs.IsInputFromPipe()
+ isPipe := cmd.IsPipe()
entry := args[idx]
if isTOTP {
- totpToken := inputs.TOTPToken()
+ totpToken := cmd.TOTPToken()
if !strings.HasSuffix(entry, backend.NewSuffix(totpToken)) {
entry = backend.NewPath(entry, totpToken)
}
@@ -63,12 +62,12 @@ func Insert(w io.Writer, t *backend.Transaction, args []string, confirm Confirm)
}
if existing != nil {
if !isPipe {
- if !confirm("overwrite existing") {
+ if !cmd.Confirm("overwrite existing") {
return nil
}
}
}
- password, err := inputs.GetUserInputPassword(isPipe, multi)
+ password, err := cmd.Input(isPipe, multi)
if err != nil {
return insertError("invalid input", err)
}
@@ -77,7 +76,7 @@ func Insert(w io.Writer, t *backend.Transaction, args []string, confirm Confirm)
return insertError("failed to insert", err)
}
if !isPipe {
- fmt.Println()
+ fmt.Fprintln(w)
}
return nil
}