move the cli to the cli/ package so enterprising individuals can call it

This commit is contained in:
Mitchell Hashimoto
2015-04-12 16:58:45 -07:00
parent 40027e22d3
commit 0b058d6335
5 changed files with 42 additions and 36 deletions

View File

@@ -1,4 +1,4 @@
package main package cli
import ( import (
"os" "os"

37
cli/main.go Normal file
View File

@@ -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
}

View File

@@ -1,4 +1,4 @@
package main package cli
// The git commit that was compiled. This will be filled in by the compiler. // The git commit that was compiled. This will be filled in by the compiler.
var GitCommit string var GitCommit string

35
main.go
View File

@@ -1,42 +1,11 @@
package main package main
import ( import (
"fmt"
"os" "os"
"github.com/mitchellh/cli" "github.com/hashicorp/vault/cli"
) )
func main() { func main() {
os.Exit(realMain()) os.Exit(cli.Run(os.Args[1:]))
}
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
} }

View File

@@ -40,7 +40,7 @@ echo "==> Building..."
gox \ gox \
-os="${XC_OS}" \ -os="${XC_OS}" \
-arch="${XC_ARCH}" \ -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" \ -output "pkg/{{.OS}}_{{.Arch}}/vault" \
. .