From adbae744fbec618cc4499801f06827108b16651f Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Tue, 3 Mar 2015 23:03:24 -0800 Subject: [PATCH] basic main boilerplate stuff --- command/version.go | 41 +++++++++++++++++++++++++++++++++++++++++ commands.go | 35 +++++++++++++++++++++++++++++++++++ main.go | 41 +++++++++++++++++++++++++++++++++++++++++ main_test.go | 4 ++++ version.go | 13 +++++++++++++ 5 files changed, 134 insertions(+) create mode 100644 command/version.go create mode 100644 commands.go create mode 100644 main.go create mode 100644 main_test.go create mode 100644 version.go diff --git a/command/version.go b/command/version.go new file mode 100644 index 000000000..097e3e356 --- /dev/null +++ b/command/version.go @@ -0,0 +1,41 @@ +package command + +import ( + "bytes" + "fmt" + + "github.com/mitchellh/cli" +) + +// VersionCommand is a Command implementation prints the version. +type VersionCommand struct { + Revision string + Version string + VersionPrerelease string + Ui cli.Ui +} + +func (c *VersionCommand) Help() string { + return "" +} + +func (c *VersionCommand) Run(_ []string) int { + + var versionString bytes.Buffer + + fmt.Fprintf(&versionString, "Vault v%s", c.Version) + if c.VersionPrerelease != "" { + fmt.Fprintf(&versionString, "-%s", c.VersionPrerelease) + + if c.Revision != "" { + fmt.Fprintf(&versionString, " (%s)", c.Revision) + } + } + + c.Ui.Output(versionString.String()) + return 0 +} + +func (c *VersionCommand) Synopsis() string { + return "Prints the Vault version" +} diff --git a/commands.go b/commands.go new file mode 100644 index 000000000..84c2e2e49 --- /dev/null +++ b/commands.go @@ -0,0 +1,35 @@ +package main + +import ( + "os" + + "github.com/hashicorp/vault/command" + "github.com/mitchellh/cli" +) + +// Commands is the mapping of all the available Consul commands. +var Commands map[string]cli.CommandFactory + +func init() { + ui := &cli.BasicUi{Writer: os.Stdout} + + Commands = map[string]cli.CommandFactory{ + "version": func() (cli.Command, error) { + ver := Version + rel := VersionPrerelease + if GitDescribe != "" { + ver = GitDescribe + } + if GitDescribe == "" && rel == "" { + rel = "dev" + } + + return &command.VersionCommand{ + Revision: GitCommit, + Version: ver, + VersionPrerelease: rel, + Ui: ui, + }, nil + }, + } +} diff --git a/main.go b/main.go new file mode 100644 index 000000000..2ab9f1433 --- /dev/null +++ b/main.go @@ -0,0 +1,41 @@ +package main + +import ( + "fmt" + "os" + + "github.com/mitchellh/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.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/main_test.go b/main_test.go new file mode 100644 index 000000000..6cadb734c --- /dev/null +++ b/main_test.go @@ -0,0 +1,4 @@ +package main + +// This file is intentionally empty to force early versions of Go +// to test compilation for tests. diff --git a/version.go b/version.go new file mode 100644 index 000000000..122e7ee9f --- /dev/null +++ b/version.go @@ -0,0 +1,13 @@ +package main + +// The git commit that was compiled. This will be filled in by the compiler. +var GitCommit string +var GitDescribe string + +// The main version number that is being run at the moment. +const Version = "0.1.0" + +// A pre-release marker for the version. If this is "" (empty string) +// then it means that it is a final release. Otherwise, this is a pre-release +// such as "dev" (in development), "beta", "rc1", etc. +const VersionPrerelease = "dev"