From 093744fbed270c803f612a76d72721284e0478fc Mon Sep 17 00:00:00 2001 From: Alex Jones Date: Wed, 17 Aug 2022 17:06:42 +0100 Subject: [PATCH] addressing concerns Signed-off-by: Alex Jones --- Makefile | 8 ++++---- cmd/root.go | 12 ++++++++++-- cmd/version.go | 26 ++++++++++++++++++++++++++ main.go | 13 +------------ 4 files changed, 41 insertions(+), 18 deletions(-) create mode 100644 cmd/version.go diff --git a/Makefile b/Makefile index 901702b6b..27f022f24 100644 --- a/Makefile +++ b/Makefile @@ -1,18 +1,18 @@ IMG ?= flagd:latest PHONY: .docker-build .build .run .mockgen PREFIX=/usr/local -VERSION=0.0.8 + guard-%: @ if [ "${${*}}" = "" ]; then \ echo "Environment variable $* not set"; \ exit 1; \ fi docker-build: - docker buildx build --build-arg=VERSION=$(VERSION) --build-arg=COMMIT=$(git rev-parse --short HEAD) --build-arg DATE="$(date +%Y%m%d)" --platform="linux/ppc64le,linux/s390x,linux/amd64,linux/arm64" -t ${IMG} . + docker buildx build --build-arg=VERSION="$$(git describe --tags --abbrev=0)" --build-arg=COMMIT="$$(git rev-parse --short HEAD)" --build-arg DATE="$$(date +%Y%m%d)" --platform="linux/ppc64le,linux/s390x,linux/amd64,linux/arm64" -t ${IMG} . --load docker-push: - docker buildx build --push --build-arg=VERSION=$(VERSION) --build-arg=COMMIT=$(git rev-parse --short HEAD) --build-arg DATE="$(date +%Y%m%d)" --platform="linux/ppc64le,linux/s390x,linux/amd64,linux/arm64" -t ${IMG} . + docker buildx build --push --build-arg=VERSION="$$(git describe --tags --abbrev=0)" --build-arg=COMMIT="$$(git rev-parse --short HEAD)" --build-arg DATE="$$(date +%Y%m%d)" --platform="linux/ppc64le,linux/s390x,linux/amd64,linux/arm64" -t ${IMG} . build: - go build -o flagd + go build -ldflags "-X main.version=dev -X main.commit=$$(git rev-parse --short HEAD) -X main.date=$$(date +%Y%m%d)" -o flagd test: go test -cover ./... run: diff --git a/cmd/root.go b/cmd/root.go index f2b35e00e..fa89cae7e 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -8,7 +8,12 @@ import ( "github.com/spf13/viper" ) -var cfgFile string +var ( + cfgFile string + Version string + Commit string + Date string +) var rootCmd = &cobra.Command{ Use: "flagd", @@ -22,7 +27,10 @@ var rootCmd = &cobra.Command{ // Execute adds all child commands to the root command and sets flags appropriately. // This is called by main.main(). It only needs to happen once to the rootCmd. -func Execute() { +func Execute(version string, commit string, date string) { + Version = version + Commit = commit + Date = date err := rootCmd.Execute() if err != nil { os.Exit(1) diff --git a/cmd/version.go b/cmd/version.go new file mode 100644 index 000000000..990177b35 --- /dev/null +++ b/cmd/version.go @@ -0,0 +1,26 @@ +/* +Copyright © 2022 NAME HERE + +*/ +package cmd + +import ( + "fmt" + + "github.com/spf13/cobra" +) + +// versionCmd represents the version command +var versionCmd = &cobra.Command{ + Use: "version", + Short: "Print the version number of FlagD", + Long: ``, + Run: func(cmd *cobra.Command, args []string) { + fmt.Printf("flagd %s (%s) built at %s\n", Version, Commit, Date) + }, +} + +func init() { + rootCmd.AddCommand(versionCmd) + +} diff --git a/main.go b/main.go index 031033223..54f97676e 100644 --- a/main.go +++ b/main.go @@ -1,10 +1,6 @@ package main import ( - "flag" - "fmt" - "os" - "github.com/open-feature/flagd/cmd" ) @@ -15,12 +11,5 @@ var ( ) func main() { - showVersion := flag.Bool("version", false, "show version") - flag.Parse() - if *showVersion { - fmt.Printf("flagd %s (%s) built at %s\n", version, commit, date) - os.Exit(0) - } else { - cmd.Execute() - } + cmd.Execute(version, commit, date) }