Skip to content

Commit

Permalink
refactor: extract getVersion function (#1127)
Browse files Browse the repository at this point in the history
Co-authored-by: chavacava <salvador.cavadini@gmail.com>
  • Loading branch information
alexandear and chavacava authored Nov 18, 2024
1 parent d81fc8f commit 93c6bc8
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 27 deletions.
65 changes: 38 additions & 27 deletions cli/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,18 @@ import (
"github.com/spf13/afero"
)

const (
defaultVersion = "dev"
defaultCommit = "none"
defaultDate = "unknown"
defaultBuilder = "unknown"
)

var (
version = "dev"
commit = "none"
date = "unknown"
builtBy = "unknown"
version = defaultVersion
commit = defaultCommit
date = defaultDate
builtBy = defaultBuilder
//AppFs is used to operations related with user config files
AppFs = afero.NewOsFs()
)
Expand All @@ -35,6 +42,12 @@ func RunRevive(extraRules ...revivelib.ExtraRule) {
// move parsing flags outside of init() otherwise tests dont works properly
// more info: https://github.com/golang/go/issues/46869#issuecomment-865695953
initConfig()

if versionFlag {
fmt.Print(getVersion(builtBy, date, commit, version))
os.Exit(0)
}

conf, err := config.GetConfig(configPath)
if err != nil {
fail(err.Error())
Expand Down Expand Up @@ -160,35 +173,33 @@ func initConfig() {
flag.BoolVar(&setExitStatus, "set_exit_status", false, exitStatusUsage)
flag.IntVar(&maxOpenFiles, "max_open_files", 0, maxOpenFilesUsage)
flag.Parse()
}

// Output build info (version, commit, date and builtBy)
if versionFlag {
var buildInfo string
if date != "unknown" && builtBy != "unknown" {
buildInfo = fmt.Sprintf("Built\t\t%s by %s\n", date, builtBy)
}
// getVersion returns build info (version, commit, date and builtBy)
func getVersion(builtBy, date, commit, version string) string {
var buildInfo string
if date != defaultDate && builtBy != defaultBuilder {
buildInfo = fmt.Sprintf("Built\t\t%s by %s\n", date, builtBy)
}

if commit != "none" {
buildInfo = fmt.Sprintf("Commit:\t\t%s\n%s", commit, buildInfo)
}
if commit != defaultCommit {
buildInfo = fmt.Sprintf("Commit:\t\t%s\n%s", commit, buildInfo)
}

if version == "dev" {
bi, ok := debug.ReadBuildInfo()
if ok {
version = bi.Main.Version
if strings.HasPrefix(version, "v") {
version = bi.Main.Version[1:]
}
if len(buildInfo) == 0 {
fmt.Printf("version %s\n", version)
os.Exit(0)
}
if version == defaultVersion {
bi, ok := debug.ReadBuildInfo()
if ok {
version = bi.Main.Version
if strings.HasPrefix(version, "v") {
version = strings.TrimLeft(bi.Main.Version, "v")
}
if len(buildInfo) == 0 {
return fmt.Sprintf("version %s\n", version)
}
}

fmt.Printf("Version:\t%s\n%s", version, buildInfo)
os.Exit(0)
}

return fmt.Sprintf("Version:\t%s\n%s", version, buildInfo)
}

func fileExist(path string) bool {
Expand Down
41 changes: 41 additions & 0 deletions cli/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,3 +86,44 @@ func TestXDGConfigDirNoFile(t *testing.T) {
t.Errorf("got %q, wanted %q", got, want)
}
}

func TestGetVersion(t *testing.T) {
tests := []struct {
name string
version string
commit string
date string
builtBy string
want string
}{
{
name: "Development version",
version: defaultVersion,
commit: defaultCommit,
date: defaultDate,
builtBy: defaultBuilder,
want: "version \n",
},
{
name: "Release version",
version: "v1.5.0-12-g7ee4500-dev",
commit: "7ee4500e125e2d1b12653b2c8e140fec380919b4",
date: "2024-11-15 10:52 UTC",
builtBy: "builder",
want: `Version: v1.5.0-12-g7ee4500-dev
Commit: 7ee4500e125e2d1b12653b2c8e140fec380919b4
Built 2024-11-15 10:52 UTC by builder
`,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := getVersion(tt.builtBy, tt.date, tt.commit, tt.version)

if got != tt.want {
t.Errorf("getVersion() = %q, want %q", got, tt.want)
}
})
}
}

0 comments on commit 93c6bc8

Please # to comment.