| 1 |
package main |
| 2 |
|
| 3 |
import ( |
| 4 |
"fmt" |
| 5 |
"log" |
| 6 |
"math/rand" |
| 7 |
"net/http" |
| 8 |
"os" |
| 9 |
"reflect" |
| 10 |
"regexp" |
| 11 |
"strings" |
| 12 |
"time" |
| 13 |
|
| 14 |
"github.com/smallstep/certificates/ca" |
| 15 |
"github.com/smallstep/cli/command" |
| 16 |
"github.com/smallstep/cli/command/version" |
| 17 |
"github.com/smallstep/cli/config" |
| 18 |
"github.com/smallstep/cli/errs" |
| 19 |
"github.com/smallstep/cli/usage" |
| 20 |
"github.com/urfave/cli" |
| 21 |
|
| 22 |
|
| 23 |
_ "github.com/smallstep/cli/command/base64" |
| 24 |
_ "github.com/smallstep/cli/command/ca" |
| 25 |
_ "github.com/smallstep/cli/command/certificate" |
| 26 |
_ "github.com/smallstep/cli/command/crypto" |
| 27 |
_ "github.com/smallstep/cli/command/fileserver" |
| 28 |
_ "github.com/smallstep/cli/command/oauth" |
| 29 |
_ "github.com/smallstep/cli/command/path" |
| 30 |
_ "github.com/smallstep/cli/command/ssh" |
| 31 |
|
| 32 |
|
| 33 |
_ "github.com/smallstep/certificates/cas/cloudcas" |
| 34 |
_ "github.com/smallstep/certificates/cas/softcas" |
| 35 |
|
| 36 |
|
| 37 |
_ "net/http/pprof" |
| 38 |
) |
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
var Version = "N/A" |
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
var BuildTime = "N/A" |
| 47 |
|
| 48 |
func init() { |
| 49 |
config.Set("Smallstep CLI", Version, BuildTime) |
| 50 |
ca.UserAgent = config.Version() |
| 51 |
rand.Seed(time.Now().UnixNano()) |
| 52 |
} |
| 53 |
|
| 54 |
func main() { |
| 55 |
defer panicHandler() |
| 56 |
|
| 57 |
cli.VersionPrinter = func(c *cli.Context) { |
| 58 |
version.Command(c) |
| 59 |
} |
| 60 |
cli.AppHelpTemplate = usage.AppHelpTemplate |
| 61 |
cli.SubcommandHelpTemplate = usage.SubcommandHelpTemplate |
| 62 |
cli.CommandHelpTemplate = usage.CommandHelpTemplate |
| 63 |
cli.HelpPrinter = usage.HelpPrinter |
| 64 |
cli.FlagNamePrefixer = usage.FlagNamePrefixer |
| 65 |
cli.FlagStringer = stringifyFlag |
| 66 |
|
| 67 |
|
| 68 |
app := cli.NewApp() |
| 69 |
app.Name = "step" |
| 70 |
app.HelpName = "step" |
| 71 |
app.Usage = "plumbing for distributed systems" |
| 72 |
app.Version = config.Version() |
| 73 |
app.Commands = command.Retrieve() |
| 74 |
app.Flags = append(app.Flags, cli.HelpFlag) |
| 75 |
app.EnableBashCompletion = true |
| 76 |
app.Copyright = "(c) 2018-2020 Smallstep Labs, Inc." |
| 77 |
|
| 78 |
|
| 79 |
app.Flags = append(app.Flags, cli.StringFlag{ |
| 80 |
Name: "config", |
| 81 |
Usage: "path to the config file to use for CLI flags", |
| 82 |
}) |
| 83 |
|
| 84 |
|
| 85 |
app.Writer = os.Stdout |
| 86 |
app.ErrWriter = os.Stderr |
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
debugProfAddr := os.Getenv("STEP_PROF_ADDR") |
| 91 |
if debugProfAddr != "" { |
| 92 |
go func() { |
| 93 |
log.Println(http.ListenAndServe(debugProfAddr, nil)) |
| 94 |
}() |
| 95 |
} |
| 96 |
|
| 97 |
if err := app.Run(os.Args); err != nil { |
| 98 |
if fe, ok := err.(errs.FriendlyError); ok { |
| 99 |
if os.Getenv("STEPDEBUG") == "1" { |
| 100 |
fmt.Fprintf(os.Stderr, "%+v\n\n%s", err, fe.Message()) |
| 101 |
} else { |
| 102 |
fmt.Fprintln(os.Stderr, fe.Message()) |
| 103 |
fmt.Fprintln(os.Stderr, "Re-run with STEPDEBUG=1 for more info.") |
| 104 |
} |
| 105 |
} else { |
| 106 |
if os.Getenv("STEPDEBUG") == "1" { |
| 107 |
fmt.Fprintf(os.Stderr, "%+v\n", err) |
| 108 |
} else { |
| 109 |
fmt.Fprintln(os.Stderr, err) |
| 110 |
} |
| 111 |
} |
| 112 |
os.Exit(1) |
| 113 |
} |
| 114 |
} |
| 115 |
|
| 116 |
func panicHandler() { |
| 117 |
if r := recover(); r != nil { |
| 118 |
if os.Getenv("STEPDEBUG") == "1" { |
| 119 |
fmt.Fprintf(os.Stderr, "%s\n", config.Version()) |
| 120 |
fmt.Fprintf(os.Stderr, "Release Date: %s\n\n", config.ReleaseDate()) |
| 121 |
panic(r) |
| 122 |
} else { |
| 123 |
fmt.Fprintln(os.Stderr, "Something unexpected happened.") |
| 124 |
fmt.Fprintln(os.Stderr, "If you want to help us debug the problem, please run:") |
| 125 |
fmt.Fprintf(os.Stderr, "STEPDEBUG=1 %s\n", strings.Join(os.Args, " ")) |
| 126 |
fmt.Fprintln(os.Stderr, "and send the output to [email protected]") |
| 127 |
os.Exit(2) |
| 128 |
} |
| 129 |
} |
| 130 |
} |
| 131 |
|
| 132 |
func flagValue(f cli.Flag) reflect.Value { |
| 133 |
fv := reflect.ValueOf(f) |
| 134 |
for fv.Kind() == reflect.Ptr { |
| 135 |
fv = reflect.Indirect(fv) |
| 136 |
} |
| 137 |
return fv |
| 138 |
} |
| 139 |
|
| 140 |
var placeholderString = regexp.MustCompile(`<.*?>`) |
| 141 |
|
| 142 |
func stringifyFlag(f cli.Flag) string { |
| 143 |
fv := flagValue(f) |
| 144 |
usage := fv.FieldByName("Usage").String() |
| 145 |
placeholder := placeholderString.FindString(usage) |
| 146 |
if placeholder == "" { |
| 147 |
switch f.(type) { |
| 148 |
case cli.BoolFlag, cli.BoolTFlag: |
| 149 |
default: |
| 150 |
placeholder = "<value>" |
| 151 |
} |
| 152 |
} |
| 153 |
return cli.FlagNamePrefixer(fv.FieldByName("Name").String(), placeholder) + "\t" + usage |
| 154 |
} |