Skip to content

Commit 8123e7b

Browse files
[FIX] enhance version info extraction
1 parent 5d3fd09 commit 8123e7b

File tree

3 files changed

+47
-21
lines changed

3 files changed

+47
-21
lines changed

flag/flag.go

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ package flag
3939

4040
import (
4141
"fmt"
42-
"os"
4342
"reflect"
4443

4544
"github.com/spf13/pflag"
@@ -57,7 +56,7 @@ var (
5756
)
5857

5958
func init() {
60-
pflag.StringVar(&Path, "path", "./data", "Sets the application default path")
59+
pflag.StringVar(&Path, "path", "./", "Sets the application working directory")
6160
pflag.BoolVar(&Help, "help", false, "Prints the help page")
6261
pflag.BoolVar(&Version, "version", false, "Prints the software version")
6362
pflag.BoolVar(&Debug, "debug", false, "Enables debug mode")
@@ -67,15 +66,9 @@ func init() {
6766
// It should be called in the main package of the application
6867
func Init() {
6968
pflag.Parse()
70-
71-
if Help {
72-
Print()
73-
os.Exit(0)
74-
}
7569
}
7670

77-
// Print prints the help page and the default values of the flags
78-
func Print() {
71+
func PrintHelp() {
7972
fmt.Println("Usage:")
8073
pflag.PrintDefaults()
8174
}

flag/flag_test.go

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -133,12 +133,6 @@ func TestInit(_ *testing.T) {
133133
flag.Init()
134134
}
135135

136-
func TestPrint(_ *testing.T) {
137-
// Test that Print doesn't panic
138-
// We can't easily test the output, but we can ensure it doesn't crash
139-
flag.Print()
140-
}
141-
142136
// Test flag registration with default values
143137
func TestRegisterFlagWithDefaults(_ *testing.T) {
144138
var stringFlag = "default"

version/version.go

Lines changed: 45 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,21 @@
44
//
55
// This package captures key metadata about the application build, including the
66
// Git tag, full and short commit hashes, build date, Go runtime version, target
7-
// platform, and the list of Go modules used in the build. These values are
8-
// intended to be set at build time via linker flags (-ldflags), allowing
9-
// embedding of dynamic version information within the compiled binary.
7+
// platform, and the list of Go modules used in the build. These values can be
8+
// set at build time via linker flags (-ldflags), or automatically extracted from
9+
// debug.BuildInfo at runtime when available.
10+
//
11+
// Version Information Sources:
12+
//
13+
// 1. Build-time ldflags (recommended for releases):
14+
// - Provides precise control over version information
15+
// - Set via -ldflags during go build
16+
//
17+
// 2. Runtime debug.BuildInfo (automatic fallback):
18+
// - Automatically used when ldflags are not set
19+
// - Extracts vcs.revision (git commit), vcs.time (build date), vcs.modified
20+
// - Available when building with Go 1.18+ and VCS information embedded
21+
// - Can be manually triggered via LoadFromBuildInfo()
1022
//
1123
// Supported Version Formats:
1224
//
@@ -107,9 +119,36 @@ var (
107119
)
108120

109121
func init() {
110-
if info, available := debug.ReadBuildInfo(); available {
111-
for _, mod := range info.Deps {
112-
Modules = append(Modules, (&Module{}).fromBuildInfo(mod))
122+
info, available := debug.ReadBuildInfo()
123+
if !available {
124+
return
125+
}
126+
127+
for _, mod := range info.Deps {
128+
Modules = append(Modules, (&Module{}).fromBuildInfo(mod))
129+
}
130+
131+
if GitCommit != "unknown" || BuildDate != "unknown" {
132+
return
133+
}
134+
135+
if info.Main.Version != "" && info.Main.Version != "(devel)" {
136+
GitTag = info.Main.Version
137+
}
138+
139+
for _, setting := range info.Settings {
140+
switch setting.Key {
141+
case "vcs.revision":
142+
if setting.Value != "" {
143+
GitCommit = setting.Value
144+
GitShort = GitCommit
145+
146+
if len(GitShort) >= 7 {
147+
GitShort = GitShort[:7]
148+
}
149+
}
150+
case "vcs.time":
151+
BuildDate = setting.Value
113152
}
114153
}
115154
}

0 commit comments

Comments
 (0)