Skip to content

Commit 93c6bc8

Browse files
alexandearchavacava
andauthored
refactor: extract getVersion function (#1127)
Co-authored-by: chavacava <[email protected]>
1 parent d81fc8f commit 93c6bc8

File tree

2 files changed

+79
-27
lines changed

2 files changed

+79
-27
lines changed

cli/main.go

Lines changed: 38 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,18 @@ import (
1616
"github.com/spf13/afero"
1717
)
1818

19+
const (
20+
defaultVersion = "dev"
21+
defaultCommit = "none"
22+
defaultDate = "unknown"
23+
defaultBuilder = "unknown"
24+
)
25+
1926
var (
20-
version = "dev"
21-
commit = "none"
22-
date = "unknown"
23-
builtBy = "unknown"
27+
version = defaultVersion
28+
commit = defaultCommit
29+
date = defaultDate
30+
builtBy = defaultBuilder
2431
//AppFs is used to operations related with user config files
2532
AppFs = afero.NewOsFs()
2633
)
@@ -35,6 +42,12 @@ func RunRevive(extraRules ...revivelib.ExtraRule) {
3542
// move parsing flags outside of init() otherwise tests dont works properly
3643
// more info: https://github.com/golang/go/issues/46869#issuecomment-865695953
3744
initConfig()
45+
46+
if versionFlag {
47+
fmt.Print(getVersion(builtBy, date, commit, version))
48+
os.Exit(0)
49+
}
50+
3851
conf, err := config.GetConfig(configPath)
3952
if err != nil {
4053
fail(err.Error())
@@ -160,35 +173,33 @@ func initConfig() {
160173
flag.BoolVar(&setExitStatus, "set_exit_status", false, exitStatusUsage)
161174
flag.IntVar(&maxOpenFiles, "max_open_files", 0, maxOpenFilesUsage)
162175
flag.Parse()
176+
}
163177

164-
// Output build info (version, commit, date and builtBy)
165-
if versionFlag {
166-
var buildInfo string
167-
if date != "unknown" && builtBy != "unknown" {
168-
buildInfo = fmt.Sprintf("Built\t\t%s by %s\n", date, builtBy)
169-
}
178+
// getVersion returns build info (version, commit, date and builtBy)
179+
func getVersion(builtBy, date, commit, version string) string {
180+
var buildInfo string
181+
if date != defaultDate && builtBy != defaultBuilder {
182+
buildInfo = fmt.Sprintf("Built\t\t%s by %s\n", date, builtBy)
183+
}
170184

171-
if commit != "none" {
172-
buildInfo = fmt.Sprintf("Commit:\t\t%s\n%s", commit, buildInfo)
173-
}
185+
if commit != defaultCommit {
186+
buildInfo = fmt.Sprintf("Commit:\t\t%s\n%s", commit, buildInfo)
187+
}
174188

175-
if version == "dev" {
176-
bi, ok := debug.ReadBuildInfo()
177-
if ok {
178-
version = bi.Main.Version
179-
if strings.HasPrefix(version, "v") {
180-
version = bi.Main.Version[1:]
181-
}
182-
if len(buildInfo) == 0 {
183-
fmt.Printf("version %s\n", version)
184-
os.Exit(0)
185-
}
189+
if version == defaultVersion {
190+
bi, ok := debug.ReadBuildInfo()
191+
if ok {
192+
version = bi.Main.Version
193+
if strings.HasPrefix(version, "v") {
194+
version = strings.TrimLeft(bi.Main.Version, "v")
195+
}
196+
if len(buildInfo) == 0 {
197+
return fmt.Sprintf("version %s\n", version)
186198
}
187199
}
188-
189-
fmt.Printf("Version:\t%s\n%s", version, buildInfo)
190-
os.Exit(0)
191200
}
201+
202+
return fmt.Sprintf("Version:\t%s\n%s", version, buildInfo)
192203
}
193204

194205
func fileExist(path string) bool {

cli/main_test.go

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,3 +86,44 @@ func TestXDGConfigDirNoFile(t *testing.T) {
8686
t.Errorf("got %q, wanted %q", got, want)
8787
}
8888
}
89+
90+
func TestGetVersion(t *testing.T) {
91+
tests := []struct {
92+
name string
93+
version string
94+
commit string
95+
date string
96+
builtBy string
97+
want string
98+
}{
99+
{
100+
name: "Development version",
101+
version: defaultVersion,
102+
commit: defaultCommit,
103+
date: defaultDate,
104+
builtBy: defaultBuilder,
105+
want: "version \n",
106+
},
107+
{
108+
name: "Release version",
109+
version: "v1.5.0-12-g7ee4500-dev",
110+
commit: "7ee4500e125e2d1b12653b2c8e140fec380919b4",
111+
date: "2024-11-15 10:52 UTC",
112+
builtBy: "builder",
113+
want: `Version: v1.5.0-12-g7ee4500-dev
114+
Commit: 7ee4500e125e2d1b12653b2c8e140fec380919b4
115+
Built 2024-11-15 10:52 UTC by builder
116+
`,
117+
},
118+
}
119+
120+
for _, tt := range tests {
121+
t.Run(tt.name, func(t *testing.T) {
122+
got := getVersion(tt.builtBy, tt.date, tt.commit, tt.version)
123+
124+
if got != tt.want {
125+
t.Errorf("getVersion() = %q, want %q", got, tt.want)
126+
}
127+
})
128+
}
129+
}

0 commit comments

Comments
 (0)