Skip to content

Commit

Permalink
feat: fill version with git and ldflags (#180)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
kachick authored May 14, 2024
1 parent e5bdc90 commit 7f77800
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 6 deletions.
2 changes: 0 additions & 2 deletions .github/workflows/release.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions .goreleaser.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ builds:
- linux
- windows
- darwin
ldflags:
- '-s -w -X main.version={{.Version}} -X main.commit={{.ShortCommit}}'
archives:
- name_template: >-
{{ .ProjectName }}_
Expand Down
9 changes: 7 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
@@ -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:
Expand Down Expand Up @@ -43,7 +48,7 @@ endif

.PHONY: install
install:
go install ./cmd/yamlfmt
go install -ldflags "$(LDFLAGS)" ./cmd/yamlfmt

.PHONY: install_tools
install_tools:
Expand Down
7 changes: 5 additions & 2 deletions cmd/yamlfmt/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
}

Expand Down

0 comments on commit 7f77800

Please sign in to comment.