From 7f77800e268130016a7c3c0e3d1c4cd512714eb8 Mon Sep 17 00:00:00 2001 From: Kenichi Kamiya Date: Tue, 14 May 2024 21:14:11 +0900 Subject: [PATCH] feat: fill version with git and ldflags (#180) * feat: fill version with git and ldflags * Respect goreleaser default except using short hash * Manually set values with git in Makefile as same as goreleaser * chore: remove needless tag overriding * chore: remove unused ldflag --- .github/workflows/release.yaml | 2 -- .goreleaser.yaml | 2 ++ Makefile | 9 +++++++-- cmd/yamlfmt/main.go | 7 +++++-- 4 files changed, 14 insertions(+), 6 deletions(-) diff --git a/.github/workflows/release.yaml b/.github/workflows/release.yaml index 15400aa..7642300 100644 --- a/.github/workflows/release.yaml +++ b/.github/workflows/release.yaml @@ -26,8 +26,6 @@ jobs: strategy: matrix: version: ['1.18'] - env: - GORELEASER_CURRENT_TAG: ${{ github.ref_name }} steps: - name: Checkout repo uses: actions/checkout@v3 diff --git a/.goreleaser.yaml b/.goreleaser.yaml index a3e59b3..0ee8767 100644 --- a/.goreleaser.yaml +++ b/.goreleaser.yaml @@ -25,6 +25,8 @@ builds: - linux - windows - darwin + ldflags: + - '-s -w -X main.version={{.Version}} -X main.commit={{.ShortCommit}}' archives: - name_template: >- {{ .ProjectName }}_ diff --git a/Makefile b/Makefile index 69d08ff..314a11e 100644 --- a/Makefile +++ b/Makefile @@ -1,8 +1,13 @@ .EXPORT_ALL_VARIABLES: +VERSION := $(shell git describe --abbrev=0 --tags | tr -d v) +COMMIT := $(shell git rev-parse --short HEAD) +LDFLAGS := -X 'main.version=$(VERSION)' \ + -X 'main.commit=$(COMMIT)' + .PHONY: build build: - go build -o dist/yamlfmt ./cmd/yamlfmt + go build -ldflags "$(LDFLAGS)" -o dist/yamlfmt ./cmd/yamlfmt .PHONY: test test: @@ -43,7 +48,7 @@ endif .PHONY: install install: - go install ./cmd/yamlfmt + go install -ldflags "$(LDFLAGS)" ./cmd/yamlfmt .PHONY: install_tools install_tools: diff --git a/cmd/yamlfmt/main.go b/cmd/yamlfmt/main.go index e5d8f53..097c7bf 100644 --- a/cmd/yamlfmt/main.go +++ b/cmd/yamlfmt/main.go @@ -26,7 +26,10 @@ import ( "github.com/google/yamlfmt/internal/logger" ) -var version string = "0.12.0" +var ( + version = "dev" + commit = "none" +) func main() { if err := run(); err != nil { @@ -41,7 +44,7 @@ func run() error { flag.Parse() if *flagVersion { - fmt.Printf("%s\n", version) + fmt.Printf("yamlfmt %s (%s)\n", version, commit) return nil }