Browse Source

chore(cmd): added git hash and build date to version

bigeagle 9 năm trước cách đây
mục cha
commit
03fdaeeb7f
6 tập tin đã thay đổi với 77 bổ sung4 xóa
  1. 1 4
      .gitignore
  2. 2 0
      .testandcover.bash
  3. 1 0
      .travis.yml
  4. 16 0
      Makefile
  5. 29 0
      cmd/tunasync/tunasync.go
  6. 28 0
      cmd/tunasynctl/tunasynctl.go

+ 1 - 4
.gitignore

@@ -1,4 +1 @@
-*.swp
-*~
-/*.cov
-node_modules
+/build

+ 2 - 0
.testandcover.bash

@@ -8,6 +8,8 @@ function die() {
 
 export GOPATH=`pwd`:$GOPATH
 
+make
+
 # Initialize profile.cov
 echo "mode: count" > profile.cov
 

+ 1 - 0
.travis.yml

@@ -4,6 +4,7 @@ go:
 
 before_install:
     - sudo apt-get install cgroup-bin
+    - go get github.com/smartystreets/goconvey
     - go get golang.org/x/tools/cmd/cover
     - go get -v github.com/mattn/goveralls
 

+ 16 - 0
Makefile

@@ -0,0 +1,16 @@
+LDFLAGS="-X main.buildstamp=`date -u '+%s'` -X main.githash=`git rev-parse HEAD`"
+
+all: get tunasync tunasynctl
+
+get: 
+	go get ./cmd/tunasync
+	go get ./cmd/tunasynctl
+
+build:
+	mkdir -p build
+
+tunasync: build
+	go build -o build/tunasync -ldflags ${LDFLAGS} github.com/tuna/tunasync/cmd/tunasync
+
+tunasynctl: build
+	go build -o build/tunasynctl -ldflags ${LDFLAGS} github.com/tuna/tunasync/cmd/tunasynctl

+ 29 - 0
cmd/tunasync/tunasync.go

@@ -1,8 +1,10 @@
 package main
 
 import (
+	"fmt"
 	"os"
 	"os/signal"
+	"strconv"
 	"syscall"
 	"time"
 
@@ -16,6 +18,11 @@ import (
 	"github.com/tuna/tunasync/worker"
 )
 
+var (
+	buildstamp = ""
+	githash    = "No githash provided"
+)
+
 var logger = logging.MustGetLogger("tunasync")
 
 func startManager(c *cli.Context) {
@@ -99,6 +106,28 @@ func startWorker(c *cli.Context) {
 }
 
 func main() {
+
+	cli.VersionPrinter = func(c *cli.Context) {
+		var builddate string
+		if buildstamp == "" {
+			builddate = "No build date provided"
+		} else {
+			ts, err := strconv.Atoi(buildstamp)
+			if err != nil {
+				builddate = "No build date provided"
+			} else {
+				t := time.Unix(int64(ts), 0)
+				builddate = t.String()
+			}
+		}
+		fmt.Printf(
+			"Version: %s\n"+
+				"Git Hash: %s\n"+
+				"Build Date: %s\n",
+			c.App.Version, githash, builddate,
+		)
+	}
+
 	app := cli.NewApp()
 	app.EnableBashCompletion = true
 	app.Version = "0.1"

+ 28 - 0
cmd/tunasynctl/tunasynctl.go

@@ -6,7 +6,9 @@ import (
 	"io/ioutil"
 	"net/http"
 	"os"
+	"strconv"
 	"strings"
+	"time"
 
 	"github.com/BurntSushi/toml"
 	"github.com/codegangsta/cli"
@@ -15,6 +17,11 @@ import (
 	tunasync "github.com/tuna/tunasync/internal"
 )
 
+var (
+	buildstamp = ""
+	githash    = "No githash provided"
+)
+
 const (
 	listJobsPath    = "/jobs"
 	listWorkersPath = "/workers"
@@ -259,6 +266,27 @@ func cmdWorker(cmd tunasync.CmdVerb) cli.ActionFunc {
 }
 
 func main() {
+	cli.VersionPrinter = func(c *cli.Context) {
+		var builddate string
+		if buildstamp == "" {
+			builddate = "No build date provided"
+		} else {
+			ts, err := strconv.Atoi(buildstamp)
+			if err != nil {
+				builddate = "No build date provided"
+			} else {
+				t := time.Unix(int64(ts), 0)
+				builddate = t.String()
+			}
+		}
+		fmt.Printf(
+			"Version: %s\n"+
+				"Git Hash: %s\n"+
+				"Build Date: %s\n",
+			c.App.Version, githash, builddate,
+		)
+	}
+
 	app := cli.NewApp()
 	app.EnableBashCompletion = true
 	app.Version = "0.1"