From 1a81425b053a276f2fdaa27800e3eb32dd1ef1da Mon Sep 17 00:00:00 2001 From: Taras Burko Date: Thu, 1 Apr 2021 15:57:14 +0100 Subject: [PATCH] Implement version command (#3) --- .github/workflows/workflow.yml | 8 +++++--- Makefile | 5 +++-- cli/main.go | 1 + cli/version.go | 27 +++++++++++++++++++++++++++ 4 files changed, 36 insertions(+), 5 deletions(-) create mode 100644 cli/version.go diff --git a/.github/workflows/workflow.yml b/.github/workflows/workflow.yml index 8380351..0b6a7fe 100644 --- a/.github/workflows/workflow.yml +++ b/.github/workflows/workflow.yml @@ -10,6 +10,8 @@ jobs: build_and_publish: runs-on: ubuntu-latest + env: + version: v0.1.${{ github.run_number }} steps: - uses: actions/checkout@v2 @@ -22,16 +24,16 @@ jobs: run: make test - name: Build Klaabu for Linux - run: make GOOS=linux GOARCH=amd64 build + run: make GOOS=linux GOARCH=amd64 VERSION=${{ env.version }} build - name: Build Klaabu for MacOS - run: make GOOS=darwin GOARCH=amd64 build + run: make GOOS=darwin GOARCH=amd64 VERSION=${{ env.version }} build - name: Publish the Klaabu binary uses: softprops/action-gh-release@v1 if: github.ref == 'refs/heads/master' with: - tag_name: v0.1.${{ github.run_number }} + tag_name: ${{ env.version }} files: | ./build/bin/klaabu-linux-amd64 ./build/bin/klaabu-darwin-amd64 diff --git a/Makefile b/Makefile index 24505a0..24ca40d 100644 --- a/Makefile +++ b/Makefile @@ -2,6 +2,7 @@ NAME := klaabu BUILD := ${CURDIR}/build BIN := ${BUILD}/bin +VERSION := dev .PHONY: clean tidy compile shasum test run .DEFAULT_GOAL := build @@ -21,10 +22,10 @@ vendor: compile: ifdef GOOS ifdef GOARCH - go build -mod=readonly -o ${BIN}/${NAME}-$(GOOS)-$(GOARCH) cli/*.go + go build -mod=readonly -ldflags="-X 'main.Version=$${VERSION}'" -o ${BIN}/${NAME}-$(GOOS)-$(GOARCH) cli/*.go endif else - go build -mod=readonly -o ${BIN}/${NAME} cli/*.go + go build -mod=readonly -ldflags="-X 'main.Version=$${VERSION}'" -o ${BIN}/${NAME} cli/*.go endif shasum: diff --git a/cli/main.go b/cli/main.go index 8ea3f2d..cb17697 100644 --- a/cli/main.go +++ b/cli/main.go @@ -13,6 +13,7 @@ var commands = map[string]func(){ "init": initCommand, "space": spaceCommand, "validate": validateCommand, + "version": versionCommand, } func helpCommand(c string) { diff --git a/cli/version.go b/cli/version.go new file mode 100644 index 0000000..343e44f --- /dev/null +++ b/cli/version.go @@ -0,0 +1,27 @@ +package main + +import ( + "flag" + "log" + "os" +) + +var Version string + +func versionCommand() { + commandName := "version" + flagSet := flag.NewFlagSet(commandName, flag.ExitOnError) + flagSet.Parse(os.Args[2:]) + + if len(os.Args) != 2 { + log.Printf("Usage: klaabu %s\n", flagSet.Name()) + flagSet.PrintDefaults() + os.Exit(1) + } + + if Version != "" { + log.Println(Version) + } else { + log.Println("development") + } +}