commit 9966e450120bb63fb87a07e784abb62c742b0228
parent 46b3081dec779154e1e7e384a5d2817c8cf10166
Author: Sean Enck <sean@ttypty.com>
Date: Sat, 16 Jul 2022 09:54:22 -0400
args is moved to a subdir
Diffstat:
7 files changed, 91 insertions(+), 84 deletions(-)
diff --git a/Makefile b/Makefile
@@ -4,14 +4,19 @@ BUILD := bin/
TARGETS := $(BUILD)lb $(BUILD)lb-rw $(BUILD)lb-rekey $(BUILD)lb-textconv $(BUILD)lb-totp
LIBEXEC := $(DESTDIR)libexec/lockbox/
MAIN := $(DESTDIR)bin/lb
+TESTDIR := $(shell find internal -type f -name "*test.go" -exec dirname {} \; | sort -u)
+
+.PHONY: $(TESTDIR)
all: $(TARGETS)
-$(TARGETS): cmd/$@/* internal/* go.*
+$(TARGETS): cmd/**/*.go internal/*.go internal/**/*.go go.*
go build -ldflags '-X main.version=$(VERSION) -X main.libExec=$(LIBEXEC) -X main.mainExe=$(MAIN)' -trimpath -buildmode=pie -mod=readonly -modcacherw -o $@ cmd/$(shell basename $@)/main.go
-check: $(TARGETS)
- cd internal && go test .
+$(TESTDIR): $(TARGETS)
+ cd $@ && go test
+
+check: $(TESTDIR)
cd tests && make BUILD=../$(BUILD)
clean:
diff --git a/cmd/lb-totp/main.go b/cmd/lb-totp/main.go
@@ -12,6 +12,7 @@ import (
"time"
"github.com/enckse/lockbox/internal"
+ "github.com/enckse/lockbox/internal/cli"
otp "github.com/pquerna/otp/totp"
)
@@ -68,7 +69,7 @@ func totpToken() string {
return t + internal.Extension
}
-func display(token string, args internal.Arguments) error {
+func display(token string, args cli.Arguments) error {
interactive, err := internal.IsInteractive()
if err != nil {
return err
@@ -183,7 +184,7 @@ func main() {
internal.Die("subkey required", errors.New("invalid arguments"))
}
cmd := args[1]
- options := internal.ParseArgs(cmd)
+ options := cli.ParseArgs(cmd)
if options.List {
result, err := list()
if err != nil {
diff --git a/cmd/lb/main.go b/cmd/lb/main.go
@@ -12,6 +12,7 @@ import (
"time"
"github.com/enckse/lockbox/internal"
+ "github.com/enckse/lockbox/internal/cli"
)
const (
@@ -78,14 +79,14 @@ func main() {
case "version":
fmt.Printf("version: %s\n", version)
case "insert":
- options := internal.Arguments{}
+ options := cli.Arguments{}
idx := 2
switch len(args) {
case 2:
internal.Die("insert missing required arguments", errors.New("entry required"))
case 3:
case 4:
- options = internal.ParseArgs(args[2])
+ options = cli.ParseArgs(args[2])
if !options.Multi {
internal.Die("multi-line insert must be after 'insert'", errors.New("invalid command"))
}
@@ -147,10 +148,10 @@ func main() {
case "show", "-c", "clip", "dump":
isDump := command == "dump"
startEntry := 2
- options := internal.Arguments{}
+ options := cli.Arguments{}
if isDump {
if len(args) > 2 {
- options = internal.ParseArgs(args[2])
+ options = cli.ParseArgs(args[2])
if options.Yes {
startEntry = 3
}
diff --git a/internal/args.go b/internal/args.go
@@ -1,25 +0,0 @@
-package internal
-
-type (
- // Arguments options for operations.
- Arguments struct {
- Clip bool
- Once bool
- Short bool
- List bool
- Multi bool
- Yes bool
- }
-)
-
-// ParseArgs parses CLI arguments.
-func ParseArgs(arg string) Arguments {
- args := Arguments{}
- args.Clip = arg == "-clip" || arg == "-c"
- args.Once = arg == "-once"
- args.Short = arg == "-short"
- args.List = arg == "-ls" || arg == "-list"
- args.Multi = arg == "-m" || arg == "-multi"
- args.Yes = arg == "-yes"
- return args
-}
diff --git a/internal/args_test.go b/internal/args_test.go
@@ -1,50 +0,0 @@
-package internal
-
-import (
- "testing"
-)
-
-func TestClipArg(t *testing.T) {
- for _, check := range []string{"-c", "-clip"} {
- options := ParseArgs(check)
- if !options.Clip {
- t.Error("clip should be set")
- }
- }
-}
-
-func TestMultiArg(t *testing.T) {
- for _, check := range []string{"-m", "-multi"} {
- options := ParseArgs(check)
- if !options.Multi {
- t.Error("multi should be set")
- }
- }
-}
-
-func TestListArg(t *testing.T) {
- for _, check := range []string{"-list", "-ls"} {
- options := ParseArgs(check)
- if !options.List {
- t.Error("list should be set")
- }
- }
-}
-
-func TestOnce(t *testing.T) {
- if options := ParseArgs("-once"); !options.Once {
- t.Error("once should be set")
- }
-}
-
-func TestShort(t *testing.T) {
- if options := ParseArgs("-short"); !options.Short {
- t.Error("short should be set")
- }
-}
-
-func TestYes(t *testing.T) {
- if options := ParseArgs("-yes"); !options.Yes {
- t.Error("yes should be set")
- }
-}
diff --git a/internal/cli/args.go b/internal/cli/args.go
@@ -0,0 +1,25 @@
+package cli
+
+type (
+ // Arguments options for operations.
+ Arguments struct {
+ Clip bool
+ Once bool
+ Short bool
+ List bool
+ Multi bool
+ Yes bool
+ }
+)
+
+// ParseArgs parses CLI arguments.
+func ParseArgs(arg string) Arguments {
+ args := Arguments{}
+ args.Clip = arg == "-clip" || arg == "-c"
+ args.Once = arg == "-once"
+ args.Short = arg == "-short"
+ args.List = arg == "-ls" || arg == "-list"
+ args.Multi = arg == "-m" || arg == "-multi"
+ args.Yes = arg == "-yes"
+ return args
+}
diff --git a/internal/cli/args_test.go b/internal/cli/args_test.go
@@ -0,0 +1,50 @@
+package cli
+
+import (
+ "testing"
+)
+
+func TestClipArg(t *testing.T) {
+ for _, check := range []string{"-c", "-clip"} {
+ options := ParseArgs(check)
+ if !options.Clip {
+ t.Error("clip should be set")
+ }
+ }
+}
+
+func TestMultiArg(t *testing.T) {
+ for _, check := range []string{"-m", "-multi"} {
+ options := ParseArgs(check)
+ if !options.Multi {
+ t.Error("multi should be set")
+ }
+ }
+}
+
+func TestListArg(t *testing.T) {
+ for _, check := range []string{"-list", "-ls"} {
+ options := ParseArgs(check)
+ if !options.List {
+ t.Error("list should be set")
+ }
+ }
+}
+
+func TestOnce(t *testing.T) {
+ if options := ParseArgs("-once"); !options.Once {
+ t.Error("once should be set")
+ }
+}
+
+func TestShort(t *testing.T) {
+ if options := ParseArgs("-short"); !options.Short {
+ t.Error("short should be set")
+ }
+}
+
+func TestYes(t *testing.T) {
+ if options := ParseArgs("-yes"); !options.Yes {
+ t.Error("yes should be set")
+ }
+}