diff --git a/cmd/step/main.go b/cmd/step/main.go index ed9b9cbde..5b689ceb4 100644 --- a/cmd/step/main.go +++ b/cmd/step/main.go @@ -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") diff --git a/config/config.go b/config/config.go index e9487d793..1d66547f3 100644 --- a/config/config.go +++ b/config/config.go @@ -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 diff --git a/usage/help.go b/usage/help.go index 85d5bf18b..9b49fc623 100644 --- a/usage/help.go +++ b/usage/help.go @@ -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 @@ -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",