Skip to content

Commit e3c3f20

Browse files
authored
Merge pull request #1273 from smallstep/herman/use-renamed-cli-utils-module
Use `github.com/smallstep/cli-utils` and reorder imports
2 parents bec3072 + 24f94b0 commit e3c3f20

File tree

136 files changed

+785
-511
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

136 files changed

+785
-511
lines changed

cmd/step/main.go

Lines changed: 54 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,31 @@ package main
33
import (
44
"errors"
55
"fmt"
6+
"io"
67
"os"
78
"reflect"
89
"regexp"
910
"strings"
1011
"time"
1112

13+
"github.com/urfave/cli"
14+
1215
"github.com/smallstep/certificates/ca"
16+
"github.com/smallstep/cli-utils/command"
17+
"github.com/smallstep/cli-utils/step"
18+
"github.com/smallstep/cli-utils/ui"
19+
"github.com/smallstep/cli-utils/usage"
20+
"go.step.sm/crypto/jose"
21+
"go.step.sm/crypto/pemutil"
22+
1323
"github.com/smallstep/cli/command/version"
1424
"github.com/smallstep/cli/internal/plugin"
1525
"github.com/smallstep/cli/utils"
16-
"github.com/urfave/cli"
17-
"go.step.sm/cli-utils/command"
18-
"go.step.sm/cli-utils/step"
19-
"go.step.sm/cli-utils/ui"
20-
"go.step.sm/cli-utils/usage"
21-
"go.step.sm/crypto/jose"
22-
"go.step.sm/crypto/pemutil"
26+
27+
// Enabled cas interfaces.
28+
_ "github.com/smallstep/certificates/cas/cloudcas"
29+
_ "github.com/smallstep/certificates/cas/softcas"
30+
_ "github.com/smallstep/certificates/cas/stepcas"
2331

2432
// Enabled commands
2533
_ "github.com/smallstep/cli/command/api"
@@ -35,11 +43,6 @@ import (
3543
_ "github.com/smallstep/cli/command/oauth"
3644
_ "github.com/smallstep/cli/command/path"
3745
_ "github.com/smallstep/cli/command/ssh"
38-
39-
// Enabled cas interfaces.
40-
_ "github.com/smallstep/certificates/cas/cloudcas"
41-
_ "github.com/smallstep/certificates/cas/softcas"
42-
_ "github.com/smallstep/certificates/cas/stepcas"
4346
)
4447

4548
// Version is set by an LDFLAG at build time representing the git tag or commit
@@ -64,6 +67,42 @@ func main() {
6467

6568
defer panicHandler()
6669

70+
// create new instance of app
71+
app := newApp(os.Stdout, os.Stderr)
72+
73+
if err := app.Run(os.Args); err != nil {
74+
var messenger interface {
75+
Message() string
76+
}
77+
if errors.As(err, &messenger) {
78+
if os.Getenv("STEPDEBUG") == "1" {
79+
fmt.Fprintf(os.Stderr, "%+v\n\n%s", err, messenger.Message())
80+
} else {
81+
fmt.Fprintln(os.Stderr, messenger.Message())
82+
fmt.Fprintln(os.Stderr, "Re-run with STEPDEBUG=1 for more info.")
83+
}
84+
} else {
85+
if os.Getenv("STEPDEBUG") == "1" {
86+
fmt.Fprintf(os.Stderr, "%+v\n", err)
87+
} else {
88+
fmt.Fprintln(os.Stderr, err)
89+
}
90+
}
91+
//nolint:gocritic // ignore exitAfterDefer error because the defer is required for recovery.
92+
os.Exit(1)
93+
}
94+
}
95+
96+
func newApp(stdout, stderr io.Writer) *cli.App {
97+
// Define default file writers and prompters for go.step.sm/crypto
98+
pemutil.WriteFile = utils.WriteFile
99+
pemutil.PromptPassword = func(msg string) ([]byte, error) {
100+
return ui.PromptPassword(msg)
101+
}
102+
jose.PromptPassword = func(msg string) ([]byte, error) {
103+
return ui.PromptPassword(msg)
104+
}
105+
67106
// Override global framework components
68107
cli.VersionPrinter = func(c *cli.Context) {
69108
version.Command(c)
@@ -109,39 +148,10 @@ func main() {
109148
}
110149

111150
// All non-successful output should be written to stderr
112-
app.Writer = os.Stdout
113-
app.ErrWriter = os.Stderr
151+
app.Writer = stdout
152+
app.ErrWriter = stderr
114153

115-
// Define default file writers and prompters for go.step.sm/crypto
116-
pemutil.WriteFile = utils.WriteFile
117-
pemutil.PromptPassword = func(msg string) ([]byte, error) {
118-
return ui.PromptPassword(msg)
119-
}
120-
jose.PromptPassword = func(msg string) ([]byte, error) {
121-
return ui.PromptPassword(msg)
122-
}
123-
124-
if err := app.Run(os.Args); err != nil {
125-
var messenger interface {
126-
Message() string
127-
}
128-
if errors.As(err, &messenger) {
129-
if os.Getenv("STEPDEBUG") == "1" {
130-
fmt.Fprintf(os.Stderr, "%+v\n\n%s", err, messenger.Message())
131-
} else {
132-
fmt.Fprintln(os.Stderr, messenger.Message())
133-
fmt.Fprintln(os.Stderr, "Re-run with STEPDEBUG=1 for more info.")
134-
}
135-
} else {
136-
if os.Getenv("STEPDEBUG") == "1" {
137-
fmt.Fprintf(os.Stderr, "%+v\n", err)
138-
} else {
139-
fmt.Fprintln(os.Stderr, err)
140-
}
141-
}
142-
//nolint:gocritic // ignore exitAfterDefer error because the defer is required for recovery.
143-
os.Exit(1)
144-
}
154+
return app
145155
}
146156

147157
func panicHandler() {

cmd/step/main_test.go

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package main
2+
3+
import (
4+
"bytes"
5+
"regexp"
6+
"testing"
7+
8+
"github.com/stretchr/testify/require"
9+
)
10+
11+
func TestAppHasAllCommands(t *testing.T) {
12+
app := newApp(&bytes.Buffer{}, &bytes.Buffer{})
13+
require.NotNil(t, app)
14+
15+
require.Equal(t, "step", app.Name)
16+
require.Equal(t, "step", app.HelpName)
17+
18+
var names = make([]string, 0, len(app.Commands))
19+
for _, c := range app.Commands {
20+
names = append(names, c.Name)
21+
}
22+
require.Equal(t, []string{
23+
"help", "api", "path", "base64", "fileserver",
24+
"certificate", "completion", "context", "crl",
25+
"crypto", "oauth", "version", "ca", "beta", "ssh",
26+
}, names)
27+
}
28+
29+
const ansi = "[\u001B\u009B][[\\]()#;?]*(?:(?:(?:[a-zA-Z\\d]*(?:;[a-zA-Z\\d]*)*)?\u0007)|(?:(?:\\d{1,4}(?:;\\d{0,4})*)?[\\dA-PRZcf-ntqry=><~]))"
30+
31+
var ansiRegex = regexp.MustCompile(ansi)
32+
33+
func TestAppRuns(t *testing.T) {
34+
stdout := &bytes.Buffer{}
35+
stderr := &bytes.Buffer{}
36+
37+
app := newApp(stdout, stderr)
38+
require.NotNil(t, app)
39+
40+
err := app.Run([]string{"step"})
41+
require.NoError(t, err)
42+
require.Empty(t, stderr.Bytes())
43+
44+
output := ansiRegex.ReplaceAllString(stdout.String(), "")
45+
require.Contains(t, output, "step -- plumbing for distributed systems")
46+
}

command/README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ should exist within its own package if possible. For example, `version` and
1212
Any package used by a command but does not contain explicit business logic
1313
directly related to the command should exist in the top-level of this
1414
repository. For example, the `github.com/smallstep/cli/flags` and
15-
`go.step.sm/cli-utils/errs` package are used by many different commands and
15+
`github.com/smallstep/cli-utils/errs` package are used by many different commands and
1616
contain functionality for defining flags and creating/manipulating errors.
1717

1818
### Adding a Command
@@ -70,8 +70,8 @@ There are three packages which contain functionality to make writing commands ea
7070

7171
- `github.com/smallstep/cli/flags`
7272
- `github.com/smallstep/cli/prompts`
73-
- `go.step.sm/cli-utils/errs`
74-
- `go.step.sm/cli-utils/usage`
73+
- `github.com/smallstep/cli-utils/errs`
74+
- `github.com/smallstep/cli-utils/usage`
7575

7676
The usage package is used to extend the default documentation provided by
7777
`urfave/cli` by enabling us to document arguments, whether they are optional or

command/api/api.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
package api
22

33
import (
4-
"github.com/smallstep/cli/command/api/token"
54
"github.com/urfave/cli"
6-
"go.step.sm/cli-utils/command"
5+
6+
"github.com/smallstep/cli-utils/command"
7+
8+
"github.com/smallstep/cli/command/api/token"
79
)
810

911
func init() {

command/api/token/create.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,9 @@ import (
1212

1313
"github.com/google/uuid"
1414
"github.com/urfave/cli"
15-
"go.step.sm/cli-utils/errs"
16-
"go.step.sm/cli-utils/ui"
15+
16+
"github.com/smallstep/cli-utils/errs"
17+
"github.com/smallstep/cli-utils/ui"
1718
)
1819

1920
func createCommand() cli.Command {

command/base64/base64.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,11 @@ import (
88
"strings"
99

1010
"github.com/pkg/errors"
11-
"github.com/smallstep/cli/utils"
1211
"github.com/urfave/cli"
13-
"go.step.sm/cli-utils/command"
12+
13+
"github.com/smallstep/cli-utils/command"
14+
15+
"github.com/smallstep/cli/utils"
1416
)
1517

1618
func init() {

command/beta/beta.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
package beta
22

33
import (
4-
"github.com/smallstep/cli/command/ca"
54
"github.com/urfave/cli"
6-
"go.step.sm/cli-utils/command"
5+
6+
"github.com/smallstep/cli-utils/command"
7+
8+
"github.com/smallstep/cli/command/ca"
79
)
810

911
// init creates and registers the ca command

command/ca/acme/eab/add.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@ import (
77
"github.com/pkg/errors"
88
"github.com/urfave/cli"
99

10-
"go.step.sm/cli-utils/errs"
11-
1210
adminAPI "github.com/smallstep/certificates/authority/admin/api"
11+
"github.com/smallstep/cli-utils/errs"
12+
1313
"github.com/smallstep/cli/flags"
1414
"github.com/smallstep/cli/utils/cautils"
1515
)

command/ca/acme/eab/list.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,10 @@ import (
88

99
"github.com/pkg/errors"
1010
"github.com/urfave/cli"
11-
"go.step.sm/cli-utils/errs"
1211

1312
"github.com/smallstep/certificates/ca"
13+
"github.com/smallstep/cli-utils/errs"
14+
1415
"github.com/smallstep/cli/flags"
1516
"github.com/smallstep/cli/utils/cautils"
1617
)

command/ca/acme/eab/remove.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import (
66
"github.com/pkg/errors"
77
"github.com/urfave/cli"
88

9-
"go.step.sm/cli-utils/errs"
9+
"github.com/smallstep/cli-utils/errs"
1010

1111
"github.com/smallstep/cli/flags"
1212
"github.com/smallstep/cli/utils/cautils"

0 commit comments

Comments
 (0)