lockbox

password manager
Log | Files | Refs | README | LICENSE

commit bfc86e0a6c62472c979febecd20da4166d9294d1
parent 06a829ca4c92785f0d5f49013b6befa4c9435d18
Author: Sean Enck <sean@ttypty.com>
Date:   Sat, 15 Oct 2022 16:26:12 -0400

do not rely on os.args

Diffstat:
Mcmd/main.go | 5++---
Minternal/program/core.go | 52+++++++++++++++++++++++++++++-----------------------
2 files changed, 31 insertions(+), 26 deletions(-)

diff --git a/cmd/main.go b/cmd/main.go @@ -19,7 +19,7 @@ func main() { if err != nil { exit(err) } - if err := p.Run(os.Args); err != nil { + if err := p.Run(os.Args[1:]); err != nil { exit(err) } -} -\ No newline at end of file +} diff --git a/internal/program/core.go b/internal/program/core.go @@ -1,3 +1,4 @@ +// Package program is the underlying full CLI representation to run lb package program import ( @@ -21,14 +22,18 @@ var ( ) type ( - ConfirmFunction func(string) (bool, error) + // ConfirmFunction is used to confirm the UI selections + ConfirmFunction func(string) (bool, error) + // ExitFunction is used to exit (unexpectedly) ExitFunction func(error) callbackFunction func([]string) error - Program struct { + // Program wraps calling/running the underlying core functionality + Program struct { confirm func(string) bool } ) +// NewProgram creates a new runnable program func NewProgram(confirmation ConfirmFunction, exit ExitFunction) (*Program, error) { if confirmation == nil { return nil, errors.New("confirmation function must be set") @@ -61,10 +66,10 @@ func getInfoDefault(args []string, possibleArg string) (bool, error) { defaults := false invalid := false switch len(args) { - case 2: + case 1: break - case 3: - if args[2] == possibleArg { + case 2: + if args[1] == possibleArg { defaults = true } else { invalid = true @@ -106,15 +111,16 @@ func wrapped(message string, err error) error { return fmt.Errorf("%s (%v)", message, err) } +// Run will run the program func (p *Program) Run(inArgs []string) error { if p.confirm == nil { return errors.New("invalid program, not setup properly") } args := inArgs - if len(args) < 2 { + if len(args) < 1 { return errors.New("requires subcommand") } - command := args[1] + command := args[0] info, err := processInfoCommands(command, args) if err != nil { return err @@ -133,10 +139,10 @@ func (p *Program) Run(inArgs []string) error { opts.Mode = backend.ListMode if command == cli.FindCommand { opts.Mode = backend.FindMode - if len(args) < 3 { + if len(args) < 2 { return errors.New("find requires search term") } - opts.Criteria = args[2] + opts.Criteria = args[1] } e, err := t.QueryCallback(opts) if err != nil { @@ -146,11 +152,11 @@ func (p *Program) Run(inArgs []string) error { fmt.Println(f.Path) } case cli.MoveCommand: - if len(args) != 4 { + if len(args) != 3 { return errors.New("src/dst required for move") } - src := args[2] - dst := args[3] + src := args[1] + dst := args[2] srcExists, err := t.Get(src, backend.SecretValue) if err != nil { return errors.New("unable to get source entry") @@ -173,13 +179,13 @@ func (p *Program) Run(inArgs []string) error { case cli.InsertCommand: multi := false isTOTP := false - idx := 2 + idx := 1 switch len(args) { - case 2: + case 1: return errors.New("insert requires an entry") + case 2: case 3: - case 4: - opt := args[2] + opt := args[1] switch opt { case cli.InsertMultiCommand: multi = true @@ -196,7 +202,7 @@ func (p *Program) Run(inArgs []string) error { return errors.New("unknown argument") } multi = true - idx = 3 + idx = 2 default: return errors.New("too many arguments") } @@ -229,10 +235,10 @@ func (p *Program) Run(inArgs []string) error { } fmt.Println("") case cli.RemoveCommand: - if len(args) != 3 { + if len(args) != 2 { return errors.New("remove requires an entry") } - deleting := args[2] + deleting := args[1] postfixRemove := "y" existings, err := t.MatchPath(deleting) if err != nil { @@ -253,10 +259,10 @@ func (p *Program) Run(inArgs []string) error { } } case cli.ShowCommand, cli.ClipCommand: - if len(args) != 3 { + if len(args) != 2 { return errors.New("entry required") } - entry := args[2] + entry := args[1] clipboard := platform.Clipboard{} isShow := command == cli.ShowCommand if !isShow { @@ -280,10 +286,10 @@ func (p *Program) Run(inArgs []string) error { return wrapped("clipboard operation failed", err) } default: - if len(args) < 2 { + if len(args) < 1 { return errors.New("missing required arguments") } - a := args[2:] + a := args[1:] callback := internalCallback(command) if callback != nil { if err := callback(a); err != nil {