Skip to content

Commit

Permalink
Refactor build time version string handling
Browse files Browse the repository at this point in the history
  • Loading branch information
knadh committed Mar 27, 2021
1 parent 3346e03 commit 9794dc6
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 17 deletions.
13 changes: 6 additions & 7 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
BIN := $(shell basename $$PWD)
HASH := $(shell git rev-parse HEAD | cut -c 1-8)
COMMIT_DATE := $(shell git show -s --format=%ci ${HASH})
BUILD_DATE := $(shell date '+%Y-%m-%d %H:%M:%S')
VERSION := ${HASH} (${COMMIT_DATE})
LAST_COMMIT := $(shell git rev-parse --short HEAD)
VERSION := $(shell git describe --tags --abbrev=0)
BUILDSTR := ${VERSION} (\#${LAST_COMMIT} $(shell date -u +"%Y-%m-%dT%H:%M:%S%z"))

STATIC := config.toml.sample schema.sql queries.sql
BIN := dictmaker

# Install dependencies needed for building
.PHONY: deps
Expand All @@ -13,8 +12,8 @@ deps:

.PHONY: build
build:
go build -o ${BIN} -ldflags="-X 'main.buildVersion=${VERSION}' -X 'main.buildDate=${BUILD_DATE}'" cmd/dictmaker/*.go
stuffbin -a stuff -in dictmaker -out dictmaker ${STATIC}
go build -o ${BIN} -ldflags="-s -w -X 'main.buildString=${BUILDSTR}'" cmd/${BIN}/*.go
stuffbin -a stuff -in ${BIN} -out ${BIN} ${STATIC}

.PHONY: build-tokenizers
build-tokenizers:
Expand Down
18 changes: 8 additions & 10 deletions cmd/dictmaker/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@ import (
)

var (
buildVersion = "unknown"
buildDate = "unknown"
buildString = "unknown"
)

// Lang represents a language's configuration.
Expand Down Expand Up @@ -67,7 +66,8 @@ func init() {
f := flag.NewFlagSet("config", flag.ContinueOnError)

f.Usage = func() {
log.Fatal(f.FlagUsages())
fmt.Println(f.FlagUsages())
os.Exit(0)
}

f.Bool("new", false, "generate a new sample config.toml file.")
Expand All @@ -82,15 +82,18 @@ func init() {
log.Fatalf("error parsing flags: %v", err)
}

if ok, _ := f.GetBool("version"); ok {
fmt.Println(buildString)
os.Exit(0)
}

// Generate new config file.
if ok, _ := f.GetBool("new"); ok {
if err := generateNewFiles(); err != nil {
fmt.Println(err)
os.Exit(1)
}

fmt.Println("config.toml and schema.sql generated. You can edit the config now.")

os.Exit(0)
}

Expand All @@ -108,11 +111,6 @@ func init() {
if err := ko.Load(posflag.Provider(f, ".", ko), nil); err != nil {
logger.Fatalf("error loading config: %v", err)
}

if ko.Bool("version") {
fmt.Printf("Commit: %v\nBuild: %v\n", buildVersion, buildDate)
os.Exit(0)
}
}

func main() {
Expand Down

0 comments on commit 9794dc6

Please sign in to comment.