commit cb0c04f912d3403a769151c787ea3afde5316d5a
parent 7d244f150cfba3306788cf66857b24cd565b534c
Author: Sean Enck <sean@ttypty.com>
Date: Fri, 3 Mar 2023 18:35:16 -0500
this can be merged back
Diffstat:
4 files changed, 119 insertions(+), 127 deletions(-)
diff --git a/Makefile b/Makefile
@@ -7,7 +7,7 @@ all: $(TARGET)
build: $(TARGET)
$(TARGET): cmd/main.go internal/**/*.go go.* internal/cli/completions*
- ./scripts/version/configure internal/app/vers.txt
+ ./scripts/version/configure cmd/vers.txt
go build $(GOFLAGS) -o $@ cmd/main.go
unittest:
diff --git a/cmd/main.go b/cmd/main.go
@@ -2,12 +2,128 @@
package main
import (
- "github.com/enckse/lockbox/internal/app"
+ _ "embed"
+ "errors"
+ "fmt"
+ "os"
+ "os/exec"
+ "strings"
+ "time"
+
+ "github.com/enckse/lockbox/internal/backend"
+ "github.com/enckse/lockbox/internal/cli"
+ "github.com/enckse/lockbox/internal/commands"
+ "github.com/enckse/lockbox/internal/inputs"
+ "github.com/enckse/lockbox/internal/platform"
+ "github.com/enckse/lockbox/internal/totp"
"github.com/enckse/lockbox/internal/util"
)
+//go:embed "vers.txt"
+var version string
+
func main() {
- if err := app.Run(); err != nil {
+ if err := run(); err != nil {
util.Die(err)
}
}
+
+func handleEarly(command string, args []string) (bool, error) {
+ ok, err := commands.Info(os.Stdout, command, args)
+ if err != nil {
+ return false, err
+ }
+ if ok {
+ return true, nil
+ }
+ switch command {
+ case cli.VersionCommand:
+ fmt.Printf("version: %s\n", version)
+ return true, nil
+ case cli.TOTPCommand:
+ return true, totp.Call(args)
+ case cli.HashCommand:
+ return true, commands.Hash(os.Stdout, args)
+ case cli.ClearCommand:
+ return true, clearClipboard(args)
+ }
+ return false, nil
+}
+
+func run() error {
+ args := os.Args
+ if len(args) < 2 {
+ return errors.New("requires subcommand")
+ }
+ command := args[1]
+ sub := args[2:]
+ ok, err := handleEarly(command, sub)
+ if err != nil {
+ return err
+ }
+ if ok {
+ return nil
+ }
+ t, err := backend.NewTransaction()
+ if err != nil {
+ return fmt.Errorf("unable to build transaction model: %w", err)
+ }
+ switch command {
+ case cli.ReKeyCommand:
+ if confirm("proceed with rekey") {
+ return t.ReKey()
+ }
+ case cli.ListCommand, cli.FindCommand:
+ return commands.ListFind(t, os.Stdout, command == cli.FindCommand, sub)
+ case cli.MoveCommand:
+ return commands.Move(t, sub, confirm)
+ case cli.InsertCommand:
+ return commands.Insert(os.Stdout, t, sub, confirm)
+ case cli.RemoveCommand:
+ return commands.Remove(os.Stdout, t, sub, confirm)
+ case cli.StatsCommand:
+ return commands.Stats(os.Stdout, t, sub)
+ case cli.ShowCommand, cli.ClipCommand:
+ return commands.ShowClip(os.Stdout, t, command == cli.ShowCommand, sub)
+ default:
+ return fmt.Errorf("unknown command: %s", command)
+ }
+ return nil
+}
+
+func clearClipboard(args []string) error {
+ idx := 0
+ val, err := inputs.Stdin(false)
+ if err != nil {
+ return err
+ }
+ clipboard, err := platform.NewClipboard()
+ if err != nil {
+ return err
+ }
+ pCmd, pArgs, valid := clipboard.Args(false)
+ if !valid {
+ return nil
+ }
+ val = strings.TrimSpace(val)
+ for idx < clipboard.MaxTime {
+ idx++
+ time.Sleep(1 * time.Second)
+ out, err := exec.Command(pCmd, pArgs...).Output()
+ if err != nil {
+ continue
+ }
+ if strings.TrimSpace(string(out)) != val {
+ return nil
+ }
+ }
+ return clipboard.CopyTo("")
+}
+
+func confirm(prompt string) bool {
+ yesNo, err := inputs.ConfirmYesNoPrompt(prompt)
+ if err != nil {
+ util.Dief("failed to read stdin for confirmation: %v", err)
+ }
+ return yesNo
+}
diff --git a/internal/app/vers.txt b/cmd/vers.txt
diff --git a/internal/app/core.go b/internal/app/core.go
@@ -1,124 +0,0 @@
-// Package app runs the commands/ui
-package app
-
-import (
- _ "embed"
- "errors"
- "fmt"
- "os"
- "os/exec"
- "strings"
- "time"
-
- "github.com/enckse/lockbox/internal/backend"
- "github.com/enckse/lockbox/internal/cli"
- "github.com/enckse/lockbox/internal/commands"
- "github.com/enckse/lockbox/internal/inputs"
- "github.com/enckse/lockbox/internal/platform"
- "github.com/enckse/lockbox/internal/totp"
- "github.com/enckse/lockbox/internal/util"
-)
-
-//go:embed "vers.txt"
-var version string
-
-func handleEarly(command string, args []string) (bool, error) {
- ok, err := commands.Info(os.Stdout, command, args)
- if err != nil {
- return false, err
- }
- if ok {
- return true, nil
- }
- switch command {
- case cli.VersionCommand:
- fmt.Printf("version: %s\n", version)
- return true, nil
- case cli.TOTPCommand:
- return true, totp.Call(args)
- case cli.HashCommand:
- return true, commands.Hash(os.Stdout, args)
- case cli.ClearCommand:
- return true, clearClipboard(args)
- }
- return false, nil
-}
-
-// Run invokes the app
-func Run() error {
- args := os.Args
- if len(args) < 2 {
- return errors.New("requires subcommand")
- }
- command := args[1]
- sub := args[2:]
- ok, err := handleEarly(command, sub)
- if err != nil {
- return err
- }
- if ok {
- return nil
- }
- t, err := backend.NewTransaction()
- if err != nil {
- return fmt.Errorf("unable to build transaction model: %w", err)
- }
- switch command {
- case cli.ReKeyCommand:
- if confirm("proceed with rekey") {
- return t.ReKey()
- }
- case cli.ListCommand, cli.FindCommand:
- return commands.ListFind(t, os.Stdout, command == cli.FindCommand, sub)
- case cli.MoveCommand:
- return commands.Move(t, sub, confirm)
- case cli.InsertCommand:
- return commands.Insert(os.Stdout, t, sub, confirm)
- case cli.RemoveCommand:
- return commands.Remove(os.Stdout, t, sub, confirm)
- case cli.StatsCommand:
- return commands.Stats(os.Stdout, t, sub)
- case cli.ShowCommand, cli.ClipCommand:
- return commands.ShowClip(os.Stdout, t, command == cli.ShowCommand, sub)
- default:
- return fmt.Errorf("unknown command: %s", command)
- }
- return nil
-}
-
-func clearClipboard(args []string) error {
- idx := 0
- val, err := inputs.Stdin(false)
- if err != nil {
- return err
- }
- clipboard, err := platform.NewClipboard()
- if err != nil {
- return err
- }
- pCmd, pArgs, valid := clipboard.Args(false)
- if !valid {
- return nil
- }
- val = strings.TrimSpace(val)
- for idx < clipboard.MaxTime {
- idx++
- time.Sleep(1 * time.Second)
- out, err := exec.Command(pCmd, pArgs...).Output()
- if err != nil {
- continue
- }
- if strings.TrimSpace(string(out)) != val {
- return nil
- }
- }
- return clipboard.CopyTo("")
-}
-
-func confirm(prompt string) bool {
- yesNo, err := inputs.ConfirmYesNoPrompt(prompt)
- if err != nil {
- util.Dief("failed to read stdin for confirmation: %v", err)
- }
- return yesNo
-}