From 5f940445a487b6491d27d67aeaa92acd939c0a55 Mon Sep 17 00:00:00 2001 From: schnie Date: Wed, 14 Feb 2018 14:44:41 -0500 Subject: [PATCH] Add version command --- Makefile | 9 ++++++++- cmd/version.go | 28 ++++++++++++++++++++++++++++ 2 files changed, 36 insertions(+), 1 deletion(-) create mode 100644 cmd/version.go diff --git a/Makefile b/Makefile index 13c6c7ead..c607f8bc8 100644 --- a/Makefile +++ b/Makefile @@ -1,12 +1,19 @@ OUTPUT ?= astro +GIT_COMMIT=$(shell git rev-parse HEAD) +GIT_COMMIT_SHORT=$(shell git rev-parse --short HEAD) +VERSION ?= SNAPSHOT-${GIT_COMMIT_SHORT} + +LDFLAGS_VERSION=-X github.com/astronomerio/astro-cli/cmd.version=${VERSION} +LDFLAGS_GIT_COMMIT=-X github.com/astronomerio/astro-cli/cmd.gitcommit=${GIT_COMMIT} + .DEFAULT_GOAL := build dep: dep ensure build: - go build -o ${OUTPUT} main.go + go build -o ${OUTPUT} -ldflags "${LDFLAGS_VERSION} ${LDFLAGS_GIT_COMMIT}" main.go install: build $(eval DESTDIR ?= $(GOBIN)) diff --git a/cmd/version.go b/cmd/version.go new file mode 100644 index 000000000..6d209b61c --- /dev/null +++ b/cmd/version.go @@ -0,0 +1,28 @@ +package cmd + +import ( + "fmt" + + "github.com/spf13/cobra" +) + +var ( + version = "" + gitcommit = "" + versionCmd = &cobra.Command{ + Use: "version", + Short: "Astronomer CLI version", + Long: "Astronomer CLI version", + RunE: printVersion, + } +) + +func init() { + RootCmd.AddCommand(versionCmd) +} + +func printVersion(cmd *cobra.Command, args []string) error { + fmt.Printf("Astro CLI Version: %s\n", version) + fmt.Printf("Git Commit: %s\n", gitcommit) + return nil +}