basic main boilerplate stuff

This commit is contained in:
Mitchell Hashimoto 2015-03-03 23:03:24 -08:00
parent 4060860194
commit adbae744fb
5 changed files with 134 additions and 0 deletions

41
command/version.go Normal file
View File

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

35
commands.go Normal file
View File

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

41
main.go Normal file
View File

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

4
main_test.go Normal file
View File

@ -0,0 +1,4 @@
package main
// This file is intentionally empty to force early versions of Go
// to test compilation for tests.

13
version.go Normal file
View File

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