@@ -3,23 +3,31 @@ package main
33import (
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
147157func panicHandler () {
0 commit comments