commit a44cd1e54f9c133e7b6a8c469d272419016b7aac
parent 528387ed60fee83561e87d4681157ac1bb4599c5
Author: Sean Enck <sean@ttypty.com>
Date: Tue, 11 Oct 2022 18:27:45 -0400
output help
Diffstat:
4 files changed, 76 insertions(+), 17 deletions(-)
diff --git a/cmd/main.go b/cmd/main.go
@@ -16,6 +16,21 @@ import (
"github.com/enckse/lockbox/internal/totp"
)
+const (
+ totpCommand = "totp"
+ hashCommand = "hash"
+ clearCommand = "clear"
+ clipCommand = "clip"
+ findCommand = "find"
+ insertCommand = "insert"
+ listCommand = "ls"
+ moveCommand = "mv"
+ showCommand = "show"
+ versionCommand = "version"
+ helpCommand = "help"
+ removeCommand = "rm"
+)
+
var (
//go:embed "vers.txt"
version string
@@ -29,13 +44,44 @@ type (
}
)
+func printSubCommand(name, desc string) {
+ printCommandText(" ", name, desc)
+}
+
+func printCommand(name, desc string) {
+ printCommandText(" ", name, desc)
+}
+
+func printCommandText(offset, name, desc string) {
+ fmt.Printf("%s%10s %s\n", offset, name, desc)
+}
+
+func printUsage() {
+ fmt.Println("lb usage:")
+ printCommand(clipCommand, "copy the entry's value into the clipboard")
+ printCommand(findCommand, "perform a simplistic text search over the entry keys")
+ printCommand(helpCommand, "show this usage information")
+ printCommand(insertCommand, "insert a new entry into the store")
+ printCommand(listCommand, "list entries")
+ printCommand(moveCommand, "move an entry from one location to another with the store")
+ printCommand(removeCommand, "remove an entry from the store")
+ printCommand(showCommand, "show the entry's value")
+ printCommand(totpCommand, "display an updating totp generated code")
+ printSubCommand(totp.ClipCommand, "copy totp code to clipboard")
+ printSubCommand(totp.ListCommand, "list entries with totp settings")
+ printSubCommand(totp.OnceCommand, "display the first generated code")
+ printSubCommand(totp.ShortCommand, "display the first generated code with no details")
+ printCommand(versionCommand, "display version information")
+ os.Exit(0)
+}
+
func internalCallback(name string) callbackFunction {
switch name {
- case "totp":
+ case totpCommand:
return totp.Call
- case "hash":
+ case hashCommand:
return hashText
- case "clear":
+ case clearCommand:
return clearClipboard
}
return nil
@@ -71,10 +117,10 @@ func run() *programError {
}
command := args[1]
switch command {
- case "ls", "find":
+ case listCommand, findCommand:
opts := backend.QueryOptions{}
opts.Mode = backend.ListMode
- if command == "find" {
+ if command == findCommand {
opts.Mode = backend.FindMode
if len(args) < 3 {
return newError("find requires an argument to search for", errors.New("search term required"))
@@ -88,9 +134,9 @@ func run() *programError {
for _, f := range e {
fmt.Println(f.Path)
}
- case "version":
+ case versionCommand:
fmt.Printf("version: %s\n", strings.TrimSpace(version))
- case "mv":
+ case moveCommand:
if len(args) != 4 {
return newError("mv requires src and dst", errors.New("src/dst required"))
}
@@ -115,7 +161,7 @@ func run() *programError {
if err := t.Move(*srcExists, dst); err != nil {
return newError("unable to move object", err)
}
- case "insert":
+ case insertCommand:
multi := false
idx := 2
switch len(args) {
@@ -153,7 +199,7 @@ func run() *programError {
return newError("failed to insert", err)
}
fmt.Println("")
- case "rm":
+ case removeCommand:
if len(args) != 3 {
return newError("rm requires a single entry", errors.New("missing argument"))
}
@@ -177,13 +223,13 @@ func run() *programError {
return newError("unable to remove entry", err)
}
}
- case "show", "clip":
+ case showCommand, clipCommand:
if len(args) != 3 {
return newError("requires a single entry", fmt.Errorf("%s missing argument", command))
}
entry := args[2]
clipboard := platform.Clipboard{}
- isShow := command == "show"
+ isShow := command == showCommand
if !isShow {
clipboard, err = platform.NewClipboard()
if err != nil {
@@ -204,6 +250,8 @@ func run() *programError {
if err := clipboard.CopyTo(existing.Value); err != nil {
return newError("clipboard failed", err)
}
+ case helpCommand:
+ printUsage()
default:
if len(args) < 2 {
return newError("command missing required arguments", fmt.Errorf("%s missing argument", command))
diff --git a/cmd/vers.txt b/cmd/vers.txt
@@ -1 +1 @@
-v22.10.07
+v22.10.08
diff --git a/contrib/completions.bash b/contrib/completions.bash
@@ -24,7 +24,7 @@ _lb() {
fi
cur=${COMP_WORDS[COMP_CWORD]}
if [ "$COMP_CWORD" -eq 1 ]; then
- opts="version ls show totp$readwrite find$clip_enabled"
+ opts="version help ls show totp$readwrite find$clip_enabled"
# shellcheck disable=SC2207
COMPREPLY=( $(compgen -W "$opts" -- "$cur") )
else
diff --git a/internal/totp/core.go b/internal/totp/core.go
@@ -18,6 +18,17 @@ import (
otp "github.com/pquerna/otp/totp"
)
+const (
+ // ClipCommand is the argument for copying totp codes to clipboard
+ ClipCommand = "-clip"
+ // ShortCommand is the argument for getting the short version of a code
+ ShortCommand = "-short"
+ // ListCommand will list the totp-enabled entries
+ ListCommand = "-list"
+ // OnceCommand will perform like a normal totp request but not refresh
+ OnceCommand = "-once"
+)
+
type (
colorWhen struct {
start int
@@ -237,9 +248,9 @@ func Call(args []string) error {
func parseArgs(arg string) arguments {
args := arguments{}
- args.Clip = arg == "-clip"
- args.Once = arg == "-once"
- args.Short = arg == "-short"
- args.List = arg == "-list"
+ args.Clip = arg == ClipCommand
+ args.Once = arg == OnceCommand
+ args.Short = arg == ShortCommand
+ args.List = arg == ListCommand
return args
}