Skip to content

Commit

Permalink
Build and report the BuildDate
Browse files Browse the repository at this point in the history
  • Loading branch information
osteele committed Jul 18, 2017
1 parent 0642963 commit 84ed3e6
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 10 deletions.
12 changes: 7 additions & 5 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
SOURCEDIR=.
SOURCES := $(shell find $(SOURCEDIR) -name '*.go')

BINARY = gojekyll
PACKAGE = github.com/osteele/gojekyll

SOURCEDIR = .
SOURCES := $(shell find $(SOURCEDIR) -name '*.go')

COMMIT_HASH = `git rev-parse --short HEAD 2>/dev/null`
BUILD_DATE = `date +%FT%T%z`

LDFLAGS=-ldflags "-X ${PACKAGE}.commands.Version=${COMMIT_HASH}"
LDFLAGS=-ldflags "-X ${PACKAGE}/cmd.Version=${COMMIT_HASH} -X ${PACKAGE}/cmd.BuildDate=${BUILD_DATE} "

.DEFAULT_GOAL: build
.PHONY: build clean deps setup install lint test help
Expand All @@ -16,7 +18,7 @@ $(BINARY): $(SOURCES)
build: $(BINARY) ## compile the package

clean: ## remove binary files
rm -fI ${BINARY}
rm -f ${BINARY}

imports: ## list imports
go list -f '{{join .Imports "\n"}}' ./... | grep -v `go list -f '{{.ImportPath}}'` | grep '\.' | sort | uniq
Expand Down
6 changes: 5 additions & 1 deletion cmd/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,10 @@ func variablesCommand(site *site.Site) (err error) {
}

func versionCommand() error {
fmt.Printf("gojekyll %s\n", Version)
var d string
if !BuildTime.IsZero() {
d = BuildTime.Format(" (Build time: 2006-01-02T15:04)")
}
fmt.Printf("gojekyll version %s%s\n", Version, d)
return nil
}
24 changes: 20 additions & 4 deletions cmd/version.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,28 @@
package cmd

// Version is the build version.
//
// Make initializes this to the git commit hash.
var Version string
import (
"fmt"
"os"
"time"
)

// Make initializes Version to the git commit hash, and BuildDate.
var (
Version string
BuildDate string
BuildTime time.Time
)

func init() {
if Version == "" {
Version = "develop"
}
if BuildDate != "" {
bd, err := time.Parse("2006-01-02T15:04:05-0700", BuildDate)
if err != nil {
fmt.Fprintln(os.Stderr, "invalid BuildDate", BuildDate) // nolint: gas
} else {
BuildTime = bd.In(time.UTC)
}
}
}

0 comments on commit 84ed3e6

Please sign in to comment.