diff --git a/Makefile b/Makefile index 8ee0957..de261e2 100644 --- a/Makefile +++ b/Makefile @@ -1,12 +1,16 @@ +# Define variables +hash = $(shell git rev-parse --short HEAD) +DATE = $(shell date -u +'%Y-%m-%dT%H:%M:%SZ') + linux: clean @echo "Building for linux" - GOOS=linux GOARCH=amd64 go build -o bin/linux ./cmd/schema + GOOS=linux GOARCH=amd64 go build -o bin/linux -ldflags '-X main.Commit=$(hash) -X main.Date=$(DATE)' ./cmd/schema windows: clean @echo "Building for windows" - GOOS=windows GOARCH=amd64 go build -o bin/windows ./cmd/schema + GOOS=windows GOARCH=amd64 go build -o bin/windows -ldflags '-X main.Commit=$(hash) -X main.Date=$(DATE)' ./cmd/schema mac: clean @echo "Building for mac" - GOOS=darwin GOARCH=amd64 go build -o bin/mac ./cmd/schema + GOOS=darwin GOARCH=amd64 go build -o bin/mac -ldflags '-X main.Commit=$(hash) -X main.Date=$(DATE)' ./cmd/schema clean: @echo "Cleaning up" # Remove the bin directory diff --git a/cmd/schema/generate.go b/cmd/schema/cmd_generate.go similarity index 100% rename from cmd/schema/generate.go rename to cmd/schema/cmd_generate.go diff --git a/cmd/schema/cmd_version.go b/cmd/schema/cmd_version.go new file mode 100644 index 0000000..73969a9 --- /dev/null +++ b/cmd/schema/cmd_version.go @@ -0,0 +1,46 @@ +package main + +import ( + "context" + "flag" + "fmt" + "runtime" + + "github.com/google/subcommands" +) + +// Set at linking time +var ( + Commit string + Date string +) + +type versionCmd struct{} + +func (v versionCmd) Name() string { + return "version" +} + +func (v versionCmd) Synopsis() string { + return "Print application version information and exit" +} + +func (v versionCmd) Usage() string { + return `version: + Print application version information and exit. +` +} + +func (v versionCmd) SetFlags(f *flag.FlagSet) {} + +func (v versionCmd) Execute(_ context.Context, _ *flag.FlagSet, _ ...interface{}) subcommands.ExitStatus { + fmt.Printf( + "Commit: %s\nRuntime: %s %s/%s\nDate: %s\n", + Commit, + runtime.Version(), + runtime.GOOS, + runtime.GOARCH, + Date, + ) + return subcommands.ExitSuccess +} diff --git a/cmd/schema/main.go b/cmd/schema/main.go index 4fc31bc..8de30bb 100644 --- a/cmd/schema/main.go +++ b/cmd/schema/main.go @@ -16,6 +16,7 @@ func main() { subcommands.Register(subcommands.FlagsCommand(), "") subcommands.Register(subcommands.CommandsCommand(), "") + subcommands.Register(new(versionCmd), "") subcommands.Register(new(generateCmd), "") flag.Parse()