Skip to content

Commit 9537b2f

Browse files
authored
Merge pull request #141 from gofiber/copilot/fix-137-2
2 parents 2d3abc3 + 44a0c45 commit 9537b2f

File tree

4 files changed

+35
-90
lines changed

4 files changed

+35
-90
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ go install github.com/gofiber/cli/fiber@latest
2222

2323
Learn more on [gofiber.io](https://gofiber.io)
2424

25-
CLI version v0.0.x
25+
CLI version detected using Go build info
2626

2727
### Options
2828

cmd/root.go

Lines changed: 26 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ package cmd
33
import (
44
"fmt"
55
"os"
6-
"os/exec"
6+
"runtime/debug"
77
"strings"
88
"time"
99

@@ -19,53 +19,43 @@ const (
1919

2020
var version string // dynamically determined version
2121

22-
// getVersion returns the current version, detected dynamically from git tags
23-
// Falls back to commit hash if git tag detection fails
22+
// getVersion returns the current version using build info
23+
// Falls back to VCS info if available, then to "unknown"
2424
func getVersion() string {
2525
if version != "" {
2626
return version
2727
}
2828

29-
// Try to get version from git describe
30-
if gitVersion := getVersionFromGit(); gitVersion != "" {
31-
version = gitVersion
29+
buildInfo, ok := debug.ReadBuildInfo()
30+
if !ok {
31+
version = unknownVersion
3232
return version
3333
}
3434

35-
// Fall back to commit hash
36-
version = getCommitHash()
37-
return version
38-
}
39-
40-
// getVersionFromGit attempts to get the version using git describe --tags
41-
func getVersionFromGit() string {
42-
cmd := exec.Command("git", "describe", "--tags", "--exact-match", "HEAD")
43-
output, err := cmd.Output()
44-
if err != nil {
45-
// If exact match fails, try getting the latest tag
46-
cmd = exec.Command("git", "describe", "--tags", "--abbrev=0")
47-
output, err = cmd.Output()
48-
if err != nil {
49-
return ""
50-
}
35+
// First try to get version from module version (when built with go install)
36+
if buildInfo.Main.Version != "" && buildInfo.Main.Version != "(devel)" {
37+
version = strings.TrimPrefix(buildInfo.Main.Version, "v")
38+
return version
5139
}
5240

53-
gitVersion := strings.TrimSpace(string(output))
54-
// Remove 'v' prefix if present
55-
gitVersion = strings.TrimPrefix(gitVersion, "v")
56-
57-
return gitVersion
58-
}
59-
60-
// getCommitHash returns the short commit hash of the current HEAD
61-
func getCommitHash() string {
62-
cmd := exec.Command("git", "rev-parse", "--short", "HEAD")
63-
output, err := cmd.Output()
64-
if err != nil {
65-
return unknownVersion
41+
// Fallback to VCS info if available
42+
for _, setting := range buildInfo.Settings {
43+
switch setting.Key {
44+
case "vcs.tag":
45+
if setting.Value != "" {
46+
version = strings.TrimPrefix(setting.Value, "v")
47+
return version
48+
}
49+
case "vcs.revision":
50+
if setting.Value != "" && len(setting.Value) >= 7 {
51+
version = setting.Value[:7] // short commit hash
52+
return version
53+
}
54+
}
6655
}
6756

68-
return strings.TrimSpace(string(output))
57+
version = unknownVersion
58+
return version
6959
}
7060

7161
var rc = rootConfig{

cmd/root_test.go

Lines changed: 8 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@ import (
55
"errors"
66
"fmt"
77
"net/http"
8-
"os"
9-
"strings"
108
"testing"
119
"time"
1210

@@ -163,64 +161,21 @@ func Test_GetVersion(t *testing.T) {
163161
assert.Equal(t, v, v2)
164162
}
165163

166-
func Test_GetVersionFromGit(t *testing.T) {
167-
// This test will depend on the git environment
168-
// It should either return a version or empty string
169-
gitVersion := getVersionFromGit()
170-
171-
// If we have git and are in a git repo, should return something
172-
// If not, should return empty string - both are valid
173-
if gitVersion != "" {
174-
// If we get a version, it should not start with 'v'
175-
assert.False(t, strings.HasPrefix(gitVersion, "v"))
176-
// Should be a valid semantic version format
177-
assert.NotEmpty(t, gitVersion)
178-
}
179-
}
180-
181-
func Test_GetCommitHash(t *testing.T) {
182-
// Test that getCommitHash returns a valid commit hash
183-
commitHash := getCommitHash()
184-
185-
// Should not be empty unless we're not in a git repo
186-
if commitHash != unknownVersion {
187-
// Should be a valid git commit hash (hex characters)
188-
assert.Regexp(t, `^[a-f0-9]+$`, commitHash)
189-
// Short hash is typically 7 characters, but can vary
190-
assert.Greater(t, len(commitHash), 4)
191-
assert.LessOrEqual(t, len(commitHash), 40) // Full hash is 40 chars max
192-
}
193-
}
194-
195164
func Test_GetVersionFallback(t *testing.T) {
196-
// Test that even if git detection fails, we get commit hash
165+
// Test that the new runtime/debug.ReadBuildInfo() approach works
197166
// Reset version cache
198167
version = ""
199-
200-
// Change to a directory without git to test fallback
201-
oldWd, err := os.Getwd()
202-
require.NoError(t, err)
203168
defer func() {
204-
err := os.Chdir(oldWd)
205-
require.NoError(t, err)
206169
version = "" // Reset for other tests
207170
}()
208171

209-
// Use cross-platform temporary directory
210-
tempDir := os.TempDir()
211-
err = os.Chdir(tempDir)
212-
require.NoError(t, err)
213-
214172
v := getVersion()
215-
// Should fall back to "unknown" only if even commit hash fails
216-
// In most cases it should return a commit hash
173+
// With runtime/debug.ReadBuildInfo(), the result depends on how the test is built
174+
// It could be module version, VCS tag, VCS revision, or "unknown"
217175
assert.NotEqual(t, "", v)
218-
if v == unknownVersion {
219-
// This means both git tag detection and commit hash detection failed
220-
// which is acceptable in non-git environments
221-
assert.Equal(t, unknownVersion, v)
222-
} else {
223-
// Should be a commit hash - typically 7 characters
224-
assert.Regexp(t, `^[a-f0-9]+$`, v)
225-
}
176+
177+
// Test that caching works
178+
version = "cached-version"
179+
v2 := getVersion()
180+
assert.Equal(t, "cached-version", v2)
226181
}
Binary file not shown.

0 commit comments

Comments
 (0)