Skip to content

Commit

Permalink
Add flag to print the steppath.
Browse files Browse the repository at this point in the history
  • Loading branch information
maraino committed Nov 16, 2018
1 parent 5db90e2 commit 7642eca
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 3 deletions.
16 changes: 16 additions & 0 deletions cmd/step/main.go
Expand Up @@ -62,15 +62,31 @@ func main() {
app.EnableBashCompletion = true
app.Copyright = "(c) 2018 Smallstep Labs, Inc."

// 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 for printing the step path
app.Flags = append(app.Flags, cli.BoolFlag{
Name: "steppath",
Usage: "print the configured step path and exit",
})

// All non-successful output should be written to stderr
app.Writer = os.Stdout
app.ErrWriter = os.Stderr

// Default action will print the steppath or help
app.Action = cli.ActionFunc(func(ctx *cli.Context) error {
if ctx.Bool("steppath") {
fmt.Println(config.StepPath())
return nil
}
return cli.HandleAction(usage.HelpCommandAction, ctx)
})

// Start the golang debug logger if environment variable is set.
// See https://golang.org/pkg/net/http/pprof/
debugProfAddr := os.Getenv("STEP_PROF_ADDR")
Expand Down
7 changes: 5 additions & 2 deletions config/config.go
Expand Up @@ -37,10 +37,13 @@ func init() {
stepPath = os.Getenv(StepPathEnv)
if stepPath == "" {
usr, err := user.Current()
if err != nil || usr.HomeDir == "" {
if err == nil && usr.HomeDir != "" {
stepPath = path.Join(usr.HomeDir, ".step")
} else if home := os.Getenv("HOME"); home != "" {
stepPath = path.Join(home, ".step")
} else {
l.Fatalf("Error obtaining home directory, please define environment variable %s.", StepPathEnv)
}
stepPath = path.Join(usr.HomeDir, ".step")
}

// Check for presence or create it if necessary
Expand Down
5 changes: 4 additions & 1 deletion usage/help.go
Expand Up @@ -7,6 +7,9 @@ import (
"github.com/urfave/cli"
)

// HelpCommandAction is the action function of the overwritten help command.
var HelpCommandAction = cli.ActionFunc(helpAction)

// HelpCommand overwrites default urfvafe/cli help command to support one or
// multiple subcommands like:
// step help
Expand All @@ -20,7 +23,7 @@ func HelpCommand() cli.Command {
Aliases: []string{"h"},
Usage: "displays help for the specified command or command group",
ArgsUsage: "[command]",
Action: cli.ActionFunc(helpAction),
Action: HelpCommandAction,
Flags: []cli.Flag{
cli.StringFlag{
Name: "http",
Expand Down

0 comments on commit 7642eca

Please sign in to comment.