diff --git a/commands.go b/cli/commands.go similarity index 99% rename from commands.go rename to cli/commands.go index 20a77223c..5d7f1ad7d 100644 --- a/commands.go +++ b/cli/commands.go @@ -1,4 +1,4 @@ -package main +package cli import ( "os" diff --git a/cli/main.go b/cli/main.go new file mode 100644 index 000000000..fd2046d7a --- /dev/null +++ b/cli/main.go @@ -0,0 +1,37 @@ +package cli + +import ( + "fmt" + "os" + + "github.com/mitchellh/cli" +) + +func Run(args []string) int { + // Get the command line args. We shortcut "--version" and "-v" to + // just show the version. + for _, arg := range args { + if arg == "-v" || arg == "--version" { + newArgs := make([]string, len(args)+1) + newArgs[0] = "version" + copy(newArgs[1:], args) + args = newArgs + break + } + } + + cli := &cli.CLI{ + Args: args, + Commands: Commands, + HelpFunc: cli.FilteredHelpFunc( + CommandsInclude, cli.BasicHelpFunc("vault")), + } + + exitCode, err := cli.Run() + if err != nil { + fmt.Fprintf(os.Stderr, "Error executing CLI: %s\n", err.Error()) + return 1 + } + + return exitCode +} diff --git a/version.go b/cli/version.go similarity index 97% rename from version.go rename to cli/version.go index 122e7ee9f..3c5fd164c 100644 --- a/version.go +++ b/cli/version.go @@ -1,4 +1,4 @@ -package main +package cli // The git commit that was compiled. This will be filled in by the compiler. var GitCommit string diff --git a/main.go b/main.go index 05f95f3dc..77fcd06a0 100644 --- a/main.go +++ b/main.go @@ -1,42 +1,11 @@ package main import ( - "fmt" "os" - "github.com/mitchellh/cli" + "github.com/hashicorp/vault/cli" ) func main() { - os.Exit(realMain()) -} - -func realMain() int { - // Get the command line args. We shortcut "--version" and "-v" to - // just show the version. - args := os.Args[1:] - for _, arg := range args { - if arg == "-v" || arg == "--version" { - newArgs := make([]string, len(args)+1) - newArgs[0] = "version" - copy(newArgs[1:], args) - args = newArgs - break - } - } - - cli := &cli.CLI{ - Args: args, - Commands: Commands, - HelpFunc: cli.FilteredHelpFunc( - CommandsInclude, cli.BasicHelpFunc("vault")), - } - - exitCode, err := cli.Run() - if err != nil { - fmt.Fprintf(os.Stderr, "Error executing CLI: %s\n", err.Error()) - return 1 - } - - return exitCode + os.Exit(cli.Run(os.Args[1:])) } diff --git a/scripts/build.sh b/scripts/build.sh index d390b3e58..f6cdafa01 100755 --- a/scripts/build.sh +++ b/scripts/build.sh @@ -40,7 +40,7 @@ echo "==> Building..." gox \ -os="${XC_OS}" \ -arch="${XC_ARCH}" \ - -ldflags "-X main.GitCommit ${GIT_COMMIT}${GIT_DIRTY}" \ + -ldflags "-X github.com/hashicorp/vault/cli.GitCommit ${GIT_COMMIT}${GIT_DIRTY}" \ -output "pkg/{{.OS}}_{{.Arch}}/vault" \ .