commit 1b7ccb533859eb7916d8d15cb68f013cec546842
parent a23102dede26293b2fd99bfeec565afe1911444c
Author: Sean Enck <sean@ttypty.com>
Date: Sat, 15 Oct 2022 10:43:17 -0400
restructure where CLI info lives
Diffstat:
3 files changed, 106 insertions(+), 84 deletions(-)
diff --git a/cmd/main.go b/cmd/main.go
@@ -11,28 +11,12 @@ import (
"time"
"github.com/enckse/lockbox/internal/backend"
+ "github.com/enckse/lockbox/internal/cli"
"github.com/enckse/lockbox/internal/inputs"
"github.com/enckse/lockbox/internal/platform"
"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"
- envCommand = "env"
- insertMultiCommand = "-multi"
-)
-
var (
//go:embed "vers.txt"
version string
@@ -42,49 +26,13 @@ type (
callbackFunction func([]string) error
)
-func printSubCommand(parent, name, args, desc string) {
- printCommandText(args, fmt.Sprintf("%s %s", parent, name), desc)
-}
-
-func printCommand(name, args, desc string) {
- printCommandText(args, name, desc)
-}
-
-func printCommandText(args, name, desc string) {
- arguments := ""
- if len(args) > 0 {
- arguments = fmt.Sprintf("[%s]", args)
- }
- fmt.Printf(" %-15s %-10s %s\n", name, arguments, desc)
-}
-
-func printUsage() {
- fmt.Println("lb usage:")
- printCommand(clipCommand, "entry", "copy the entry's value into the clipboard")
- printCommand(envCommand, "", "display environment variable information")
- printCommand(findCommand, "criteria", "perform a simplistic text search over the entry keys")
- printCommand(helpCommand, "", "show this usage information")
- printCommand(insertCommand, "entry", "insert a new entry into the store")
- printSubCommand(insertCommand, insertMultiCommand, "entry", "insert a multi-line entry")
- printCommand(listCommand, "", "list entries")
- printCommand(moveCommand, "src dst", "move an entry from one location to another with the store")
- printCommand(removeCommand, "entry", "remove an entry from the store")
- printCommand(showCommand, "entry", "show the entry's value")
- printCommand(totpCommand, "entry", "display an updating totp generated code")
- printSubCommand(totpCommand, totp.ClipCommand, "entry", "copy totp code to clipboard")
- printSubCommand(totpCommand, totp.ListCommand, "", "list entries with totp settings")
- printSubCommand(totpCommand, totp.OnceCommand, "entry", "display the first generated code")
- printSubCommand(totpCommand, totp.ShortCommand, "entry", "display the first generated code with no details")
- printCommand(versionCommand, "", "display version information")
-}
-
func internalCallback(name string) callbackFunction {
switch name {
- case totpCommand:
+ case cli.TOTPCommand:
return totp.Call
- case hashCommand:
+ case cli.HashCommand:
return hashText
- case clearCommand:
+ case cli.ClearCommand:
return clearClipboard
}
return nil
@@ -103,18 +51,18 @@ func main() {
func processInfoCommands(command string, args []string) (bool, error) {
switch command {
- case helpCommand:
- printUsage()
- case versionCommand:
+ case cli.HelpCommand:
+ cli.Usage()
+ case cli.VersionCommand:
fmt.Printf("version: %s\n", strings.TrimSpace(version))
- case envCommand:
+ case cli.EnvCommand:
printValues := true
invalid := false
switch len(args) {
case 2:
break
case 3:
- if args[2] == "-defaults" {
+ if args[2] == cli.EnvDefaultsCommand {
printValues = false
} else {
invalid = true
@@ -154,10 +102,10 @@ func run() error {
return wrapped("unable to build transaction model", err)
}
switch command {
- case listCommand, findCommand:
+ case cli.ListCommand, cli.FindCommand:
opts := backend.QueryOptions{}
opts.Mode = backend.ListMode
- if command == findCommand {
+ if command == cli.FindCommand {
opts.Mode = backend.FindMode
if len(args) < 3 {
return errors.New("find requires search term")
@@ -171,7 +119,7 @@ func run() error {
for _, f := range e {
fmt.Println(f.Path)
}
- case moveCommand:
+ case cli.MoveCommand:
if len(args) != 4 {
return errors.New("src/dst required for move")
}
@@ -196,7 +144,7 @@ func run() error {
if err := t.Move(*srcExists, dst); err != nil {
return wrapped("unable to move object", err)
}
- case insertCommand:
+ case cli.InsertCommand:
multi := false
idx := 2
switch len(args) {
@@ -204,7 +152,7 @@ func run() error {
return errors.New("insert requires an entry")
case 3:
case 4:
- if args[2] != insertMultiCommand {
+ if args[2] != cli.InsertMultiCommand {
return errors.New("unknown argument")
}
multi = true
@@ -234,7 +182,7 @@ func run() error {
return wrapped("failed to insert", err)
}
fmt.Println("")
- case removeCommand:
+ case cli.RemoveCommand:
if len(args) != 3 {
return errors.New("remove requires an entry")
}
@@ -258,13 +206,13 @@ func run() error {
return wrapped("unable to remove entry", err)
}
}
- case showCommand, clipCommand:
+ case cli.ShowCommand, cli.ClipCommand:
if len(args) != 3 {
return errors.New("entry required")
}
entry := args[2]
clipboard := platform.Clipboard{}
- isShow := command == showCommand
+ isShow := command == cli.ShowCommand
if !isShow {
clipboard, err = platform.NewClipboard()
if err != nil {
diff --git a/internal/cli/core.go b/internal/cli/core.go
@@ -0,0 +1,84 @@
+// Package cli handles CLI helpers/commands
+package cli
+
+import (
+ "fmt"
+)
+
+const (
+ // TOTPCommand is the parent of totp and by defaults generates a rotating token
+ TOTPCommand = "totp"
+ // HashCommand handles hashing the data store
+ HashCommand = "hash"
+ // ClearCommand is a callback to manage clipboard clearing
+ ClearCommand = "clear"
+ // ClipCommand will copy values to the clipboard
+ ClipCommand = "clip"
+ // FindCommand is for simplistic searching of entries
+ FindCommand = "find"
+ // InsertCommand adds a value
+ InsertCommand = "insert"
+ // ListCommand lists all entries
+ ListCommand = "ls"
+ // MoveCommand will move source to destination
+ MoveCommand = "mv"
+ // ShowCommand will show the value in an entry
+ ShowCommand = "show"
+ // VersionCommand displays version information
+ VersionCommand = "version"
+ // HelpCommand shows usage
+ HelpCommand = "help"
+ // RemoveCommand removes an entry
+ RemoveCommand = "rm"
+ // EnvCommand shows environment information used by lockbox
+ EnvCommand = "env"
+ // InsertMultiCommand handles multi-line inserts
+ InsertMultiCommand = "-multi"
+ // TOTPClipCommand is the argument for copying totp codes to clipboard
+ TOTPClipCommand = "-clip"
+ // TOTPShortCommand is the argument for getting the short version of a code
+ TOTPShortCommand = "-short"
+ // TOTPListCommand will list the totp-enabled entries
+ TOTPListCommand = "-list"
+ // TOTPOnceCommand will perform like a normal totp request but not refresh
+ TOTPOnceCommand = "-once"
+ // EnvDefaultsCommand will display the default env variables, not those set
+ EnvDefaultsCommand = "-defaults"
+)
+
+func subCommand(parent, name, args, desc string) string {
+ return commandText(args, fmt.Sprintf("%s %s", parent, name), desc)
+}
+
+func command(name, args, desc string) string {
+ return commandText(args, name, desc)
+}
+
+func commandText(args, name, desc string) string {
+ arguments := ""
+ if len(args) > 0 {
+ arguments = fmt.Sprintf("[%s]", args)
+ }
+ return fmt.Sprintf(" %-15s %-10s %s\n", name, arguments, desc)
+}
+
+// Usage return usage information
+func Usage() []string {
+ fmt.Println("lb usage:")
+ printCommand(ClipCommand, "entry", "copy the entry's value into the clipboard")
+ printCommand(EnvCommand, "", "display environment variable information")
+ printCommand(FindCommand, "criteria", "perform a simplistic text search over the entry keys")
+ printCommand(HelpCommand, "", "show this usage information")
+ printCommand(InsertCommand, "entry", "insert a new entry into the store")
+ printSubCommand(InsertCommand, InsertMultiCommand, "entry", "insert a multi-line entry")
+ printCommand(ListCommand, "", "list entries")
+ printCommand(MoveCommand, "src dst", "move an entry from one location to another with the store")
+ printCommand(RemoveCommand, "entry", "remove an entry from the store")
+ printCommand(ShowCommand, "entry", "show the entry's value")
+ printCommand(TOTPCommand, "entry", "display an updating totp generated code")
+ printSubCommand(TOTPCommand, TOTPClipCommand, "entry", "copy totp code to clipboard")
+ printSubCommand(TOTPCommand, TOTPListCommand, "", "list entries with totp settings")
+ printSubCommand(TOTPCommand, TOTPOnceCommand, "entry", "display the first generated code")
+ printSubCommand(TOTPCommand, TOTPShortCommand, "entry", "display the first generated code with no details")
+ printCommand(VersionCommand, "", "display version information")
+}
diff --git a/internal/totp/core.go b/internal/totp/core.go
@@ -10,6 +10,7 @@ import (
"time"
"github.com/enckse/lockbox/internal/backend"
+ "github.com/enckse/lockbox/internal/cli"
"github.com/enckse/lockbox/internal/colors"
"github.com/enckse/lockbox/internal/inputs"
"github.com/enckse/lockbox/internal/platform"
@@ -17,17 +18,6 @@ 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 (
arguments struct {
Clip bool
@@ -214,9 +204,9 @@ func Call(args []string) error {
func parseArgs(arg string) arguments {
args := arguments{}
- args.Clip = arg == ClipCommand
- args.Once = arg == OnceCommand
- args.Short = arg == ShortCommand
- args.List = arg == ListCommand
+ args.Clip = arg == cli.TOTPClipCommand
+ args.Once = arg == cli.TOTPOnceCommand
+ args.Short = arg == cli.TOTPShortCommand
+ args.List = arg == cli.TOTPListCommand
return args
}