-
Notifications
You must be signed in to change notification settings - Fork 295
Expand file tree
/
Copy pathroot.go
More file actions
208 lines (181 loc) · 5.62 KB
/
root.go
File metadata and controls
208 lines (181 loc) · 5.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
package cmd
import (
"errors"
"fmt"
"io"
"os"
"reflect"
"regexp"
"strings"
"time"
"github.com/urfave/cli"
"github.com/smallstep/cli-utils/command"
"github.com/smallstep/cli-utils/errs"
"github.com/smallstep/cli-utils/fileutil"
"github.com/smallstep/cli-utils/step"
"github.com/smallstep/cli-utils/ui"
"github.com/smallstep/cli-utils/usage"
"github.com/smallstep/cli/command/version"
"github.com/smallstep/cli/internal/plugin"
"go.step.sm/crypto/jose"
"go.step.sm/crypto/pemutil"
// Enabled cas interfaces.
_ "github.com/smallstep/certificates/cas/cloudcas"
_ "github.com/smallstep/certificates/cas/softcas"
_ "github.com/smallstep/certificates/cas/stepcas"
// Enabled commands
_ "github.com/smallstep/cli/command/api"
_ "github.com/smallstep/cli/command/base64"
_ "github.com/smallstep/cli/command/beta"
_ "github.com/smallstep/cli/command/ca"
_ "github.com/smallstep/cli/command/certificate"
_ "github.com/smallstep/cli/command/completion"
_ "github.com/smallstep/cli/command/context"
_ "github.com/smallstep/cli/command/crl"
_ "github.com/smallstep/cli/command/crypto"
_ "github.com/smallstep/cli/command/fileserver"
_ "github.com/smallstep/cli/command/oauth"
_ "github.com/smallstep/cli/command/path"
_ "github.com/smallstep/cli/command/ssh"
)
func Run() {
os.Exit(run())
}
func run() int {
defer panicHandler()
// initialize step environment.
if err := step.Init(); err != nil {
fmt.Fprintln(os.Stderr, err.Error())
return 1
}
// create new instance of app
app := newApp(os.Stdout, os.Stderr)
if err := app.Run(os.Args); err != nil {
var messenger interface {
Message() string
}
if errors.As(err, &messenger) {
if os.Getenv("STEPDEBUG") == "1" {
fmt.Fprintf(os.Stderr, "%+v\n\n%s", err, messenger.Message())
} else {
fmt.Fprintln(os.Stderr, messenger.Message())
fmt.Fprintln(os.Stderr, "Re-run with STEPDEBUG=1 for more info.")
}
} else {
if os.Getenv("STEPDEBUG") == "1" {
fmt.Fprintf(os.Stderr, "%+v\n", err)
} else {
fmt.Fprintln(os.Stderr, err)
}
}
return 1
}
return 0
}
var stepAppName = "step"
func SetName(appName string) {
if appName != "" {
stepAppName = appName
}
}
func newApp(stdout, stderr io.Writer) *cli.App {
// Define default file writers and prompters for go.step.sm/crypto
pemutil.WriteFile = fileutil.WriteFile
pemutil.PromptPassword = func(msg string) ([]byte, error) {
if !ui.CanPrompt() {
return nil, errs.NewRequiredInputError("password", "password-file")
}
return ui.PromptPassword(msg)
}
jose.PromptPassword = func(msg string) ([]byte, error) {
if !ui.CanPrompt() {
return nil, errs.NewRequiredInputError("password", "password-file")
}
return ui.PromptPassword(msg)
}
// Override global framework components
cli.VersionPrinter = func(c *cli.Context) {
version.Command(c)
}
cli.AppHelpTemplate = usage.AppHelpTemplate
cli.SubcommandHelpTemplate = usage.SubcommandHelpTemplate
cli.CommandHelpTemplate = usage.CommandHelpTemplate
cli.HelpPrinter = usage.HelpPrinter
cli.FlagNamePrefixer = usage.FlagNamePrefixer
cli.FlagStringer = stringifyFlag
// Configure cli app
app := cli.NewApp()
app.Name = stepAppName // "step" by default
app.HelpName = stepAppName // "step" by default
app.Usage = "plumbing for distributed systems"
app.Version = step.Version()
app.Commands = command.Retrieve()
app.Flags = append(app.Flags, cli.HelpFlag)
app.EnableBashCompletion = true
app.Copyright = fmt.Sprintf("(c) 2018-%d Smallstep Labs, Inc.", time.Now().Year())
// Flag of custom configuration flag
app.Flags = append(app.Flags, cli.StringFlag{
Name: "config",
Usage: "path to the config file to use for CLI flags",
})
// Flag to disable interactive prompts
app.Flags = append(app.Flags, cli.BoolFlag{
Name: "non-interactive",
Usage: "disable interactive prompts; commands will fail if required input is missing",
EnvVar: "STEP_NON_INTERACTIVE",
})
// Action runs on `step` or `step <command>` if the command is not enabled.
app.Action = func(ctx *cli.Context) error {
args := ctx.Args()
if name := args.First(); name != "" {
if file, err := plugin.LookPath(name); err == nil {
return plugin.Run(ctx, file)
}
if u := plugin.GetURL(name); u != "" {
//nolint:staticcheck // this is a top level error - capitalization is ok
return fmt.Errorf("The plugin %q was not found on this system.\nDownload it from %s", name, u)
}
return cli.ShowCommandHelp(ctx, name)
}
return cli.ShowAppHelp(ctx)
}
// All non-successful output should be written to stderr
app.Writer = stdout
app.ErrWriter = stderr
return app
}
func panicHandler() {
if r := recover(); r != nil {
if os.Getenv("STEPDEBUG") == "1" {
fmt.Fprintf(os.Stderr, "%s\n", step.Version())
fmt.Fprintf(os.Stderr, "Release Date: %s\n\n", step.ReleaseDate())
panic(r)
}
fmt.Fprintln(os.Stderr, "Something unexpected happened.")
fmt.Fprintln(os.Stderr, "If you want to help us debug the problem, please run:")
fmt.Fprintf(os.Stderr, "STEPDEBUG=1 %s\n", strings.Join(os.Args, " "))
fmt.Fprintln(os.Stderr, "and send the output to info@smallstep.com")
os.Exit(2)
}
}
func flagValue(f cli.Flag) reflect.Value {
fv := reflect.ValueOf(f)
for fv.Kind() == reflect.Ptr {
fv = reflect.Indirect(fv)
}
return fv
}
var placeholderString = regexp.MustCompile(`<.*?>`)
func stringifyFlag(f cli.Flag) string {
fv := flagValue(f)
usg := fv.FieldByName("Usage").String()
placeholder := placeholderString.FindString(usg)
if placeholder == "" {
switch f.(type) {
case cli.BoolFlag, cli.BoolTFlag:
default:
placeholder = "<value>"
}
}
return cli.FlagNamePrefixer(fv.FieldByName("Name").String(), placeholder) + "\t" + usg
}