fuzzyworld

template / commit

Adding package version.

2543ce03bbe010b2ef45071c970455af41cbe6ca · Pavel Safronov · 2023-01-29 14:09:23 -0800 · parents 77405b973f11

Adding package version.
 Makefile |  9 +++++++--
 main.go  | 13 +++++++++++++
 2 files changed, 20 insertions(+), 2 deletions(-)

diff --git a/Makefile b/Makefile
index ede671e..9c34776 100644
--- a/Makefile
+++ b/Makefile
@@ -1,5 +1,9 @@
 .PHONY: test
 
+BINARY=template
+VERSION := $(shell git describe --tags)
+LDFLAGS=-ldflags "-w -s -X main.Version=${VERSION}"
+
 all: build test lint install
 
 prep:
@@ -9,7 +13,7 @@ clean:
 	go clean
 
 build: prep
-	go build .
+	go build ${LDFLAGS} -o ${BINARY} .
 
 lint: build
 	go install github.com/golangci/golangci-lint/cmd/golangci-lint@latest
@@ -26,11 +30,12 @@ test_coverage_html: test_coverage
 	open "./test-coverage.html"
 
 install: build test lint
-	go install
+	go install ${LDFLAGS}
 	template reset
 
 uninstall:
 	go clean -i gitlab.com/fuzzy-rp/template...
+	if [ -f ${BINARY} ] ; then rm ${BINARY} ; fi
 
 release-dry-run: build test lint
 	go install github.com/goreleaser/goreleaser@latest
diff --git a/main.go b/main.go
index b9b5a03..d261eb5 100644
--- a/main.go
+++ b/main.go
@@ -13,6 +13,11 @@ import (
 	"gopkg.in/yaml.v3"
 )
 
+var (
+	// Version is the version of the application
+	Version string
+)
+
 const defaultUserConfigPath = "~/.template.user.yaml"
 const defaultConfigPath = "~/.template.yaml"
 const defaultLogLevel = "info"
@@ -161,6 +166,14 @@ func main() {
 					return nil
 				},
 			},
+			{
+				Name:  "version",
+				Usage: "print version",
+				Action: func(c *cli.Context) error {
+					fmt.Printf("Version: %v", Version)
+					return nil
+				},
+			},
 		},
 		Action: func(context *cli.Context) error {
 			settings, err := createSettings(argumentValues, context)