Skip to content

Commit

Permalink
Merge pull request #69 from cfergeau/version
Browse files Browse the repository at this point in the history
Generate vfkit version from git tag
  • Loading branch information
praveenkumar authored Jan 29, 2024
2 parents 1bad6ae + a6e2d5d commit 452c2e0
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 4 deletions.
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/pkg/cmdline/version.go export-subst
9 changes: 9 additions & 0 deletions .github/workflows/compile.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,15 @@ jobs:
steps:
- name: Check out repository code
uses: actions/checkout@v4
with:
fetch-depth: 0

- name: >-
WORKAROUND: Fetch tags that points to the revisions
checked-out(actions/checkout#1467)
run: |-
git fetch --tags --force
- name: Set up Go
uses: actions/setup-go@v5
with:
Expand Down
4 changes: 3 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
.PHONY: all build clean test

GIT_VERSION ?= $(shell git describe --always --dirty)
CGO_CFLAGS=-mmacosx-version-min=11.0
VERSION_LDFLAGS=-X github.com/crc-org/vfkit/pkg/cmdline.gitVersion=$(GIT_VERSION)

all: build

Expand All @@ -17,7 +19,7 @@ clean:

out/vfkit-amd64 out/vfkit-arm64: out/vfkit-%: force-build
@mkdir -p $(@D)
CGO_ENABLED=1 CGO_CFLAGS=$(CGO_CFLAGS) GOOS=darwin GOARCH=$* go build -o $@ ./cmd/vfkit
CGO_ENABLED=1 CGO_CFLAGS=$(CGO_CFLAGS) GOOS=darwin GOARCH=$* go build -ldflags "$(VERSION_LDFLAGS)" -o $@ ./cmd/vfkit
codesign --entitlements vf.entitlements -s - $@

out/vfkit: out/vfkit-amd64 out/vfkit-arm64
Expand Down
4 changes: 1 addition & 3 deletions cmd/vfkit/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ import (
"github.com/spf13/cobra"
)

const vfkitVersion = "0.5.1"

var opts = &cmdline.Options{}

var rootCmd = &cobra.Command{
Expand All @@ -32,7 +30,7 @@ var rootCmd = &cobra.Command{
}
return runVFKit(vmConfig, opts)
},
Version: vfkitVersion,
Version: cmdline.Version(),
}

func init() {
Expand Down
40 changes: 40 additions & 0 deletions pkg/cmdline/version.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package cmdline

import (
"runtime/debug"
"strings"
)

var (
// set using the '-X github.com/crc-org/vfkit/pkg/cmdline.gitVersion' linker flag
gitVersion = "unknown"

// set through .gitattributes when `git archive` is used
// see https://icinga.com/blog/2022/05/25/embedding-git-commit-information-in-go-binaries/
gitArchiveVersion = "$Format:%(describe)$"
)

func Version() string {
switch {
// This will be substituted when building from a GitHub tarball
case !strings.HasPrefix(gitArchiveVersion, "$Format:"):
return gitArchiveVersion
// This will be set when building from git using make
case gitVersion != "":
return gitVersion
// moduleVersionFromBuildInfo() will be set when using `go install`
default:
return moduleVersionFromBuildInfo()
}
}

func moduleVersionFromBuildInfo() string {
info, ok := debug.ReadBuildInfo()
if !ok {
return ""
}
if info.Main.Version == "(devel)" {
return ""
}
return info.Main.Version
}

0 comments on commit 452c2e0

Please sign in to comment.