From d742286a22b45dae29d7d6c9c90daaf5f17ac850 Mon Sep 17 00:00:00 2001 From: Emma Alyx Wunder Date: Fri, 19 May 2023 05:25:19 +0200 Subject: [PATCH] Added Makefile and version info --- .gitignore | 7 +++-- Makefile | 58 ++++++++++++++++++++++++++++++++++++++++++ constants/constants.go | 46 +++++++++++++++++++++++++++++++++ 3 files changed, 109 insertions(+), 2 deletions(-) create mode 100644 Makefile create mode 100644 constants/constants.go diff --git a/.gitignore b/.gitignore index 709e08d..9da1374 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,9 @@ # the binaries themselves -/mtxconv -/mtxconv.exe +/mtxconv* + +# garbage +Thumbs.db +.DS_Store # test directory /test diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..084e850 --- /dev/null +++ b/Makefile @@ -0,0 +1,58 @@ +GIT_BRANCH := $(shell git rev-parse --abbrev-ref HEAD) +GIT_COMMIT := $(shell git rev-parse HEAD) +GIT_COMMIT_SHORT := $(shell git rev-parse --short HEAD) +GIT_VERSION := $(shell git describe --tags --always --dirty) +GIT_TAG := $(shell git describe --tags) +BUILD_TIME := $(shell date -u +"%Y-%m-%dT%H:%M:%S %Z") + +LDFLAGS := -ldflags '\ +-X "mtxconv/constants.GitBranch=$(GIT_BRANCH)" \ +-X "mtxconv/constants.GitCommit=$(GIT_COMMIT)" \ +-X "mtxconv/constants.GitCommitShort=$(GIT_COMMIT_SHORT)" \ +-X "mtxconv/constants.GitVersion=$(GIT_VERSION)" \ +-X "mtxconv/constants.GitTag=$(GIT_TAG)" \ +-X "mtxconv/constants.BuildTime=$(BUILD_TIME)"' +BINARY_NAME = mtxconv +GOCMD = go +GOFORMAT = $(GOCMD) fmt +GOBUILD = $(GOCMD) build -trimpath +GOCLEAN = $(GOCMD) clean +GOTEST = $(GOCMD) test +GOGET = $(GOCMD) get +GOBUILDEXT = $(GOBUILD) -o $(BINARY_NAME) $(LDFLAGS) + +.EXPORT_ALL_VARIABLES: + GOAMD64 = v3 +.PHONY: build buildall image test format clean run debug deps +build: + $(GOBUILDEXT) + @echo Build completed… +buildall: + @echo Building windows-x64… + @GOOS=windows GOARCH=amd64 $(GOBUILD) -o $(BINARY_NAME)-windows-x64.exe $(LDFLAGS) + @echo Building macos-x64… + @GOOS=darwin GOARCH=amd64 $(GOBUILD) -o $(BINARY_NAME)-macos-x64 $(LDFLAGS) + @echo Building macos-arm64… + @GOOS=darwin GOARCH=arm64 $(GOBUILD) -o $(BINARY_NAME)-macos-arm64 $(LDFLAGS) + @echo Building linux-x64… + @GOOS=linux GOARCH=amd64 $(GOBUILD) -o $(BINARY_NAME)-linux-x64 $(LDFLAGS) + @echo Building linux-arm64… + @GOOS=linux GOARCH=arm64 $(GOBUILD) -o $(BINARY_NAME)-linux-arm64 $(LDFLAGS) +test: build + $(GOTEST) -v ./... +format: + $(GOFORMAT) ./... +clean: + $(GOCLEAN) + rm -f ./$(BINARY_NAME) + rm -f ./$(BINARY_NAME)-windows-x64.exe + rm -f ./$(BINARY_NAME)-macos-x64 + rm -f ./$(BINARY_NAME)-macos-arm64 + rm -f ./$(BINARY_NAME)-linux-x64 + rm -f ./$(BINARY_NAME)-linux-arm64 +run: build + ./$(BINARY_NAME) +debug: build + ./$(BINARY_NAME) --debug +deps: + $(GOGET) diff --git a/constants/constants.go b/constants/constants.go new file mode 100644 index 0000000..45a9896 --- /dev/null +++ b/constants/constants.go @@ -0,0 +1,46 @@ +package constants + +import ( + "log" + "time" +) + +var ( + GitBranch = "n/a" + GitCommit = "n/a" + GitCommitShort = "n/a" + GitVersion = "n/a" + GitTag = "v0.0.0" // provide default value that can be parsed + BuildTime = "n/a" +) + +const ( + BuildTimeFormat = "2006-01-02T15:04:05 MST" +) + +func ParseBuildTime() *time.Time { + parsed, err := time.Parse(BuildTimeFormat, BuildTime) + if err != nil { + log.Printf("ParseBuildTime: %v", err.Error()) + return nil + } + + return &parsed +} + +func ParseBuildTimeLocal() *time.Time { + parsed, err := time.Parse(BuildTimeFormat, BuildTime) + if err != nil { + log.Printf("ParseBuildTimeLocal: %v", err.Error()) + return nil + } + + localLocation, err := time.LoadLocation("Local") + if err != nil { + log.Printf("ParseBuildTimeLocal: %v", err.Error()) + return nil + } + + localTime := parsed.In(localLocation) + return &localTime +}