Skip to content

Commit 02c8d5e

Browse files
committed
Re-write CLI
1 parent f5744e6 commit 02c8d5e

36 files changed

+1488
-639
lines changed

.golangci.yml

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
linters:
2+
disable-all: true
3+
enable:
4+
- deadcode
5+
- errcheck
6+
- gosimple
7+
- govet
8+
- ineffassign
9+
- staticcheck
10+
- structcheck
11+
- typecheck
12+
- unused
13+
- varcheck
14+
- asciicheck
15+
- bodyclose
16+
- dogsled
17+
- exhaustive
18+
- exportloopref
19+
- gocognit
20+
- goconst
21+
- gofmt
22+
- goheader
23+
- goimports
24+
- gosec
25+
- misspell
26+
- nakedret
27+
- nestif
28+
- noctx
29+
- rowserrcheck
30+
- sqlclosecheck
31+
- unconvert
32+
- unparam
33+
- whitespace
34+
35+
issues:
36+
exclude:
37+
- "composite literal uses unkeyed fields"
38+
exclude-rules:
39+
- path: _test\.go
40+
linters:
41+
- gosec
42+
- noctx
43+
- unparam
44+
- bodyclose

Makefile

+15-1
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,18 @@ proto:
88

99
.PHONY: install
1010
install:
11-
go install .
11+
go install .
12+
13+
14+
.PHONY: lint
15+
lint:
16+
golangci-lint run
17+
18+
.PHONY: test
19+
test:
20+
go test ./...
21+
22+
.PHONY: fmt
23+
fmt:
24+
goimports -local github.com/ThreeDotsLabs -l -w .
25+

go.mod

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ require (
1010
github.com/hexops/gotextdiff v1.0.3
1111
github.com/pkg/errors v0.9.1
1212
github.com/sirupsen/logrus v1.8.1
13+
github.com/spf13/afero v1.6.0
1314
github.com/stretchr/testify v1.7.0
1415
github.com/urfave/cli/v2 v2.3.0
1516
google.golang.org/grpc v1.40.0

go.sum

+7
Original file line numberDiff line numberDiff line change
@@ -54,12 +54,15 @@ github.com/google/uuid v1.1.2/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+
5454
github.com/grpc-ecosystem/grpc-gateway v1.16.0/go.mod h1:BDjrQk3hbvj6Nolgz8mAMFbcEtjT1g+wF4CSlocrBnw=
5555
github.com/hexops/gotextdiff v1.0.3 h1:gitA9+qJrrTCsiCl7+kh75nPqQt1cx4ZkudSTLoUqJM=
5656
github.com/hexops/gotextdiff v1.0.3/go.mod h1:pSWU5MAI3yDq+fZBTazCSJysOMbxWL1BSow5/V2vxeg=
57+
github.com/kr/fs v0.1.0/go.mod h1:FFnZGqtBN9Gxj7eW1uZ42v5BccTP0vu6NEaFoC2HwRg=
5758
github.com/mattn/go-colorable v0.1.8 h1:c1ghPdyEDarC70ftn0y+A/Ee++9zz8ljHG1b13eJ0s8=
5859
github.com/mattn/go-colorable v0.1.8/go.mod h1:u6P/XSegPjTcexA+o6vUJrdnUu04hMope9wVRipJSqc=
5960
github.com/mattn/go-isatty v0.0.12 h1:wuysRhFDzyxgEmMf5xjvJ2M9dZoWAXNNr5LSBS7uHXY=
6061
github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU=
62+
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
6163
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
6264
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
65+
github.com/pkg/sftp v1.10.1/go.mod h1:lYOWFsE0bwd1+KfKJaKeuokY15vzFx25BLbzYYoAxZI=
6366
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
6467
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
6568
github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA=
@@ -71,8 +74,11 @@ github.com/shurcooL/sanitized_anchor_name v1.0.0/go.mod h1:1NzhyTcUVG4SuEtjjoZeV
7174
github.com/sirupsen/logrus v1.8.1 h1:dJKuHgqk1NNQlqoA6BTlM1Wf9DOH3NBjQyu0h9+AZZE=
7275
github.com/sirupsen/logrus v1.8.1/go.mod h1:yWOB1SBYBC5VeMP7gHvWumXLIWorT60ONWic61uBYv0=
7376
github.com/spaolacci/murmur3 v0.0.0-20180118202830-f09979ecbc72/go.mod h1:JwIasOWyU6f++ZhiEuf87xNszmSA2myDM2Kzu9HwQUA=
77+
github.com/spf13/afero v1.6.0 h1:xoax2sJ2DT8S8xA2paPFjDCScCNeWsg75VG0DLRreiY=
78+
github.com/spf13/afero v1.6.0/go.mod h1:Ai8FlHk4v/PARR026UzYexafAt9roJ7LcLMAmO6Z93I=
7479
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
7580
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
81+
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
7682
github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA=
7783
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
7884
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
@@ -81,6 +87,7 @@ github.com/urfave/cli/v2 v2.3.0/go.mod h1:LJmUH05zAU44vOAcrfzZQKsZbVcdbOG8rtL3/X
8187
github.com/yuin/goldmark v1.3.5/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k=
8288
go.opentelemetry.io/proto/otlp v0.7.0/go.mod h1:PqfVotwruBrMGOCsRd/89rSnXhoiJIqeYNgFYFoEGnI=
8389
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
90+
golang.org/x/crypto v0.0.0-20190820162420-60c769a6c586/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
8491
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
8592
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
8693
golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=

internal/confirm.go

+18-9
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,17 @@ import (
77
"strings"
88
)
99

10-
var stdin io.Reader = os.Stdin
11-
var stdout io.Writer = os.Stdout
12-
1310
func ConfirmPrompt(msg string) bool {
14-
defer fmt.Fprintln(stdout)
11+
return FConfirmPrompt(msg, os.Stdin, os.Stdout)
12+
}
13+
14+
func FConfirmPrompt(msg string, stdin io.Reader, stdout io.Writer) bool {
15+
defer func() {
16+
_, _ = fmt.Fprintln(stdout)
17+
}()
1518

1619
for {
17-
fmt.Fprintf(stdout, "%s [y/n]: ", msg)
20+
_, _ = fmt.Fprintf(stdout, "%s [y/n]: ", msg)
1821

1922
var input string
2023
_, err := fmt.Fscanln(stdin, &input)
@@ -37,19 +40,25 @@ func ConfirmPrompt(msg string) bool {
3740
}
3841

3942
func ConfirmPromptDefaultYes(msg string) bool {
40-
defer fmt.Println()
43+
return FConfirmPromptDefaultYes(msg, os.Stdin, os.Stdout)
44+
}
45+
46+
func FConfirmPromptDefaultYes(msg string, stdin io.Reader, stdout io.Writer) bool {
47+
defer func() {
48+
_, _ = fmt.Fprintln(stdout)
49+
}()
4150

4251
for {
43-
fmt.Fprintf(stdout, "%s [Y/n]: ", msg)
52+
_, _ = fmt.Fprintf(stdout, "%s [Y/n]: ", msg)
4453

4554
var input string
4655
_, _ = fmt.Fscanln(stdin, &input)
4756

4857
input = strings.ToLower(input)
4958
if input == "n" || input == "no" {
5059
return false
60+
} else {
61+
return true
5162
}
52-
53-
return true
5463
}
5564
}

internal/confirm_test.go

+8-19
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package internal
22

33
import (
44
"bytes"
5-
"os"
65
"testing"
76

87
"github.com/stretchr/testify/assert"
@@ -50,17 +49,12 @@ func TestConfirmPrompt(t *testing.T) {
5049
tc := testCases[i]
5150

5251
t.Run(tc.Input, func(t *testing.T) {
53-
defer func() {
54-
stdin = os.Stdin
55-
stdout = os.Stdout
56-
}()
52+
stdin := bytes.NewBufferString(tc.Input)
53+
stdout := bytes.NewBuffer(nil)
5754

58-
stdin = bytes.NewBufferString(tc.Input)
59-
stdout = bytes.NewBuffer(nil)
60-
61-
ok := ConfirmPrompt("some msg")
55+
ok := FConfirmPrompt("some msg", stdin, stdout)
6256
assert.Equal(t, tc.ExpectedResult, ok)
63-
assert.Equal(t, tc.ExpectedMessage, stdout.(*bytes.Buffer).String())
57+
assert.Equal(t, tc.ExpectedMessage, stdout.String())
6458
})
6559
}
6660
}
@@ -104,17 +98,12 @@ func TestConfirmPromptDefaultYes(t *testing.T) {
10498
tc := testCases[i]
10599

106100
t.Run(tc.Input, func(t *testing.T) {
107-
defer func() {
108-
stdin = os.Stdin
109-
stdout = os.Stdout
110-
}()
111-
112-
stdin = bytes.NewBufferString(tc.Input)
113-
stdout = bytes.NewBuffer(nil)
101+
stdin := bytes.NewBufferString(tc.Input)
102+
stdout := bytes.NewBuffer(nil)
114103

115-
ok := ConfirmPromptDefaultYes("some msg")
104+
ok := FConfirmPromptDefaultYes("some msg", stdin, stdout)
116105
assert.Equal(t, tc.ExpectedResult, ok)
117-
assert.Equal(t, "some msg [Y/n]: ", stdout.(*bytes.Buffer).String())
106+
assert.Equal(t, "some msg [Y/n]: \n", stdout.String())
118107
})
119108
}
120109
}

internal/print.go

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package internal
2+
3+
import "github.com/fatih/color"
4+
5+
func SprintCommand(cmd string) string {
6+
return color.CyanString(cmd)
7+
}

0 commit comments

Comments
 (0)