commit d84bc870a1f56c347517d8e57247f1b9daf44a1d
parent edd359f5f4096fc1921df6fda7ecb55e4f63e25e
Author: Sean Enck <sean@ttypty.com>
Date: Sat, 4 Mar 2023 06:38:44 -0500
Revert "this helper is not overly useful"
This reverts commit 4a2520c1be2666bbcf2150b9a7232dab099d1e23.
Diffstat:
8 files changed, 40 insertions(+), 8 deletions(-)
diff --git a/cmd/main.go b/cmd/main.go
@@ -81,6 +81,8 @@ func run() error {
insert := app.InsertOptions{}
insert.Confirm = confirm
insert.IsPipe = inputs.IsInputFromPipe
+ insert.IsNoTOTP = inputs.IsNoTOTP
+ insert.TOTPToken = inputs.TOTPToken
insert.Input = inputs.GetUserInputPassword
insertArgs, err := app.ParseInsertArgs(insert, sub)
if err != nil {
diff --git a/internal/app/core.go b/internal/app/core.go
@@ -6,14 +6,17 @@ type (
Confirm func(string) bool
// InsertOptions are functions required for insert
InsertOptions struct {
- IsPipe func() bool
- Input func(bool, bool) ([]byte, error)
- Confirm Confirm
+ IsNoTOTP func() (bool, error)
+ IsPipe func() bool
+ TOTPToken func() string
+ Input func(bool, bool) ([]byte, error)
+ Confirm Confirm
}
// InsertArgs are parsed insert settings for insert commands
InsertArgs struct {
Entry string
Multi bool
+ TOTP bool
Opts InsertOptions
}
)
diff --git a/internal/app/insert.go b/internal/app/insert.go
@@ -9,6 +9,7 @@ import (
"github.com/enckse/lockbox/internal/backend"
"github.com/enckse/lockbox/internal/cli"
+ "github.com/enckse/lockbox/internal/totp"
)
func insertError(message string, err error) error {
@@ -18,6 +19,7 @@ func insertError(message string, err error) error {
// ParseInsertArgs will parse the input args for insert commands
func ParseInsertArgs(cmd InsertOptions, args []string) (InsertArgs, error) {
multi := false
+ isTOTP := false
idx := 0
switch len(args) {
case 0:
@@ -28,6 +30,15 @@ func ParseInsertArgs(cmd InsertOptions, args []string) (InsertArgs, error) {
switch opt {
case cli.InsertMultiCommand:
multi = true
+ case cli.InsertTOTPCommand:
+ off, err := cmd.IsNoTOTP()
+ if err != nil {
+ return InsertArgs{}, err
+ }
+ if off {
+ return InsertArgs{}, totp.ErrNoTOTP
+ }
+ isTOTP = true
default:
return InsertArgs{}, errors.New("unknown argument")
}
@@ -36,11 +47,17 @@ func ParseInsertArgs(cmd InsertOptions, args []string) (InsertArgs, error) {
default:
return InsertArgs{}, errors.New("too many arguments")
}
- return InsertArgs{Opts: cmd, Multi: multi, Entry: args[idx]}, nil
+ return InsertArgs{Opts: cmd, Multi: multi, TOTP: isTOTP, Entry: args[idx]}, nil
}
// Do will execute an insert
func (args InsertArgs) Do(w io.Writer, t *backend.Transaction) error {
+ if args.TOTP {
+ totpToken := args.Opts.TOTPToken()
+ if !strings.HasSuffix(args.Entry, backend.NewSuffix(totpToken)) {
+ args.Entry = backend.NewPath(args.Entry, totpToken)
+ }
+ }
existing, err := t.Get(args.Entry, backend.BlankValue)
if err != nil {
return insertError("unable to check for existing entry", err)
diff --git a/internal/cli/completions.bash b/internal/cli/completions.bash
@@ -13,7 +13,7 @@ _{{ $.Executable }}() {
case ${COMP_WORDS[1]} in
{{ if not $.ReadOnly }}
"{{ $.InsertCommand }}")
- opts="{{ $.InsertMultiCommand }} $({{ $.DoList }})"
+ opts="{{ $.InsertMultiCommand }}{{ if $.CanTOTP }} {{ $.InsertTOTPCommand }}{{end}} $({{ $.DoList }})"
;;
"{{ $.HelpCommand }}")
opts="{{ $.HelpAdvancedCommand }}"
@@ -39,7 +39,7 @@ _{{ $.Executable }}() {
case "${COMP_WORDS[1]}" in
{{ if not $.ReadOnly }}
"{{ $.InsertCommand }}")
- if [ "${COMP_WORDS[2]}" == "{{ $.InsertMultiCommand }}" ]; then
+ if [ "${COMP_WORDS[2]}" == "{{ $.InsertMultiCommand }}" ] {{ if $.CanTOTP }}|| [ "${COMP_WORDS[2]}" == "{{ $.InsertTOTPCommand }}" ] {{end}}; then
opts=$({{ $.DoList }})
fi
;;
diff --git a/internal/cli/core.go b/internal/cli/core.go
@@ -47,6 +47,8 @@ const (
EnvCommand = "env"
// InsertMultiCommand handles multi-line inserts
InsertMultiCommand = "-multi"
+ // InsertTOTPCommand is a helper for totp inserts
+ InsertTOTPCommand = "-totp"
// TOTPClipCommand is the argument for copying totp codes to clipboard
TOTPClipCommand = "-clip"
// TOTPShortCommand is the argument for getting the short version of a code
@@ -85,6 +87,7 @@ type (
TOTPOnceCommand string
TOTPClipCommand string
InsertMultiCommand string
+ InsertTOTPCommand string
RemoveCommand string
ClipCommand string
ShowCommand string
@@ -142,6 +145,7 @@ func BashCompletions(defaults bool) ([]string, error) {
InsertMultiCommand: InsertMultiCommand,
HelpCommand: HelpCommand,
HelpAdvancedCommand: HelpAdvancedCommand,
+ InsertTOTPCommand: InsertTOTPCommand,
TOTPCommand: TOTPCommand,
MoveCommand: MoveCommand,
DoList: fmt.Sprintf("%s %s", name, ListCommand),
@@ -212,6 +216,7 @@ func Usage(verbose bool) ([]string, error) {
results = append(results, subCommand(HelpCommand, HelpAdvancedCommand, "", "display verbose help information"))
results = append(results, command(InsertCommand, "entry", "insert a new entry into the store"))
results = append(results, subCommand(InsertCommand, InsertMultiCommand, "entry", "insert a multi-line entry"))
+ results = append(results, subCommand(InsertCommand, InsertTOTPCommand, "entry", "insert a new totp entry"))
results = append(results, command(ListCommand, "", "list entries"))
results = append(results, command(MoveCommand, "src dst", "move an entry from source to destination"))
results = append(results, command(RemoveCommand, "entry", "remove an entry from the store"))
diff --git a/internal/cli/core_test.go b/internal/cli/core_test.go
@@ -10,11 +10,11 @@ import (
func TestUsage(t *testing.T) {
u, _ := cli.Usage(false)
- if len(u) != 21 {
+ if len(u) != 22 {
t.Errorf("invalid usage, out of date? %d", len(u))
}
u, _ = cli.Usage(true)
- if len(u) != 80 {
+ if len(u) != 81 {
t.Errorf("invalid verbose usage, out of date? %d", len(u))
}
for _, usage := range u {
diff --git a/scripts/testing/check.go b/scripts/testing/check.go
@@ -149,6 +149,10 @@ func execute() error {
show("keys/k/one2")
show("keys2/k/three")
runCommand([]string{"stats", "keys2/k/three"}, nil)
+ for _, k := range []string{"test/k", "test/k/totp"} {
+ runCommand([]string{"insert", "-totp", k}, []string{"5ae472abqdekjqykoyxk7hvc2leklq5n"})
+ }
+ totpList()
insert("test/k/totp", []string{"5ae472abqdekjqykoyxk7hvc2leklq5n"})
totpList()
runCommand([]string{"totp", "test/k"}, nil)
diff --git a/scripts/testing/expected.log b/scripts/testing/expected.log
@@ -20,6 +20,7 @@ test3
test4
modtime: XXXX-XX-XX
test/k
+test/k
XXXXXX
key/a/one:
modtime: XXXX-XX-XX