From 57be8bcc0983c4f15f06cf3a2c99cf3d82e9a004 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Sun, 12 Apr 2015 16:58:45 -0700 Subject: [PATCH] move the cli to the cli/ package so enterprising individuals can call it --- commands.go => cli/commands.go | 2 +- cli/main.go | 37 ++++++++++++++++++++++++++++++++++ version.go => cli/version.go | 2 +- main.go | 35 ++------------------------------ scripts/build.sh | 2 +- 5 files changed, 42 insertions(+), 36 deletions(-) rename commands.go => cli/commands.go (99%) create mode 100644 cli/main.go rename version.go => cli/version.go (97%) 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" \ .