From bce9cd9bc334e7c31ce38b1faffe0b77fc5f20ec Mon Sep 17 00:00:00 2001
From: kayrus <kay.diam@gmail.com>
Date: Mon, 5 Aug 2019 15:35:07 +0200
Subject: [PATCH] Add --version flag

Resolves #4
---
 Makefile  | 8 +++++---
 README.md | 1 +
 main.go   | 3 +++
 3 files changed, 9 insertions(+), 3 deletions(-)

diff --git a/Makefile b/Makefile
index eaa5319..a8f0f10 100644
--- a/Makefile
+++ b/Makefile
@@ -2,15 +2,17 @@ PKG:=github.com/sapcc/nova-password
 APP_NAME:=nova-password
 PWD:=$(shell pwd)
 UID:=$(shell id -u)
+VERSION:=$(shell git describe --tags --always --dirty="-dev")
+LDFLAGS:=-X main.Version=$(VERSION)
 
 export GO111MODULE:=off
 export GOPATH:=$(PWD)/gopath
 export CGO_ENABLED:=0
 
 build: gopath/src/$(PKG) fmt
-	GOOS=linux go build -o bin/$(APP_NAME) $(PKG)
-	GOOS=darwin go build -o bin/$(APP_NAME)_darwin $(PKG)
-	GOOS=windows go build -o bin/$(APP_NAME).exe $(PKG)
+	GOOS=linux go build -ldflags="$(LDFLAGS)" -o bin/$(APP_NAME) $(PKG)
+	GOOS=darwin go build -ldflags="$(LDFLAGS)" -o bin/$(APP_NAME)_darwin $(PKG)
+	GOOS=windows go build -ldflags="$(LDFLAGS)" -o bin/$(APP_NAME).exe $(PKG)
 
 docker:
 	docker run -ti --rm -e GOCACHE=/tmp -v $(PWD):/$(APP_NAME) -u $(UID):$(UID) --workdir /$(APP_NAME) golang:latest make
diff --git a/README.md b/README.md
index 13adfb7..d823355 100644
--- a/README.md
+++ b/README.md
@@ -10,6 +10,7 @@ Flags:
   -d, --debug                     print out request and response objects
   -h, --help                      help for nova-password
   -i, --private-key-path string   a path to the RSA private key (PuTTY and OpenSSH formats) (default "~/.ssh/id_rsa")
+      --version                   version for nova-password
   -w, --wait uint                 wait for the password timeout in seconds
 ```
 
diff --git a/main.go b/main.go
index 3ed8caf..7c2bb6e 100644
--- a/main.go
+++ b/main.go
@@ -24,11 +24,14 @@ import (
 
 const MaxKeySize = 10240
 
+var Version string
+
 // RootCmd represents the base command when called without any subcommands
 var RootCmd = &cobra.Command{
 	Use:          "nova-password <server-name>|<server-id> [<server-name>|<server-id>...]",
 	Short:        "Get the admin password for an OpenStack server",
 	SilenceUsage: true,
+	Version:      Version,
 	PreRunE: func(cmd *cobra.Command, args []string) error {
 		if len(args) == 0 {
 			cmd.Usage()
-- 
GitLab