diff --git a/.github/workflows/publish.yml b/.github/workflows/publish.yml new file mode 100644 index 0000000..b3888d8 --- /dev/null +++ b/.github/workflows/publish.yml @@ -0,0 +1,30 @@ +name: publish + +on: + push: + tags: + - '*' + +jobs: + publish: + strategy: + matrix: + go-version: [ 1.15.x ] + os: [ ubuntu-latest ] + runs-on: ${{ matrix.os }} + steps: + - uses: actions/checkout@master + with: + fetch-depth: 1 + - name: Install Go + uses: actions/setup-go@v2 + with: + go-version: ${{ matrix.go-version }} + - name: Make all + run: make all + - name: Upload release binaries + uses: alexellis/upload-assets@0.3.0 + env: + GITHUB_TOKEN: ${{ github.token }} + with: + asset_paths: '["./dist/redis-benchmark-go_*"]' diff --git a/Makefile b/Makefile index 4771889..554913c 100644 --- a/Makefile +++ b/Makefile @@ -8,12 +8,23 @@ GOGET=$(GOCMD) get GOMOD=$(GOCMD) mod GOFMT=$(GOCMD) fmt DISTDIR = ./dist +OS_ARCHs = "linux/amd64 linux/arm64 windows/amd64 darwin/amd64 darwin/arm64" + +# Build-time GIT variables +ifeq ($(GIT_SHA),) +GIT_SHA:=$(shell git rev-parse HEAD) +endif + +ifeq ($(GIT_DIRTY),) +GIT_DIRTY:=$(shell git diff --no-ext-diff 2> /dev/null | wc -l) +endif .PHONY: all test coverage -all: test coverage build +all: test build release build: - $(GOBUILD) . + $(GOBUILD) \ + -ldflags="-X 'main.GitSHA1=$(GIT_SHA)' -X 'main.GitDirty=$(GIT_DIRTY)'" . checkfmt: @echo 'Checking gofmt';\ @@ -42,7 +53,9 @@ coverage: get test release: $(GOGET) github.com/mitchellh/gox $(GOGET) github.com/tcnksm/ghr - GO111MODULE=on gox -osarch "linux/amd64 linux/arm64 windows/amd64 darwin/amd64 darwin/arm64" -output "${DISTDIR}/redis-benchmark-go_{{.OS}}_{{.Arch}}" . + GO111MODULE=on gox -osarch ${OS_ARCHs} \ + -ldflags="-X 'main.GitSHA1=$(GIT_SHA)' -X 'main.GitDirty=$(GIT_DIRTY)'" \ + -output "${DISTDIR}/redis-benchmark-go_{{.OS}}_{{.Arch}}" . publish: release @for f in $(shell ls ${DISTDIR}); \ diff --git a/bin_info.go b/bin_info.go new file mode 100644 index 0000000..c1afb79 --- /dev/null +++ b/bin_info.go @@ -0,0 +1,23 @@ +package main + +import ( + "strconv" + "strings" +) + +// Vars only for git sha and diff handling +var GitSHA1 string = "" +var GitDirty string = "0" + +func toolGitSHA1() string { + return GitSHA1 +} + +func toolGitDirty() (dirty bool) { + dirty = false + dirtyLines, err := strconv.Atoi(strings.TrimSpace(GitDirty)) + if err == nil { + dirty = (dirtyLines != 0) + } + return +} diff --git a/redis-bechmark-go.go b/redis-bechmark-go.go index e04a6ec..2553c65 100644 --- a/redis-bechmark-go.go +++ b/redis-bechmark-go.go @@ -128,7 +128,17 @@ func main() { multi := flag.Bool("multi", false, "Run each command in multi-exec.") clusterMode := flag.Bool("oss-cluster", false, "Enable OSS cluster mode.") loop := flag.Bool("l", false, "Loop. Run the tests forever.") + version := flag.Bool("v", false, "Output version and exit") flag.Parse() + git_sha := toolGitSHA1() + git_dirty_str := "" + if toolGitDirty() { + git_dirty_str = "-dirty" + } + if *version { + fmt.Fprintf(os.Stdout, "redis-benchmark-go (git_sha1:%s%s)\n", git_sha, git_dirty_str) + os.Exit(0) + } args := flag.Args() if len(args) < 2 { log.Fatalf("You need to specify a command after the flag command arguments. The commands requires a minimum size of 2 ( command name and key )")