1
0
mirror of https://github.com/mgechev/revive.git synced 2025-07-05 00:28:53 +02:00

refactor: extract getVersion function (#1127)

Co-authored-by: chavacava <salvador.cavadini@gmail.com>
This commit is contained in:
Oleksandr Redko
2024-11-18 15:45:28 +02:00
committed by GitHub
parent d81fc8fab3
commit 93c6bc8636
2 changed files with 79 additions and 27 deletions

View File

@ -16,11 +16,18 @@ import (
"github.com/spf13/afero" "github.com/spf13/afero"
) )
const (
defaultVersion = "dev"
defaultCommit = "none"
defaultDate = "unknown"
defaultBuilder = "unknown"
)
var ( var (
version = "dev" version = defaultVersion
commit = "none" commit = defaultCommit
date = "unknown" date = defaultDate
builtBy = "unknown" builtBy = defaultBuilder
//AppFs is used to operations related with user config files //AppFs is used to operations related with user config files
AppFs = afero.NewOsFs() AppFs = afero.NewOsFs()
) )
@ -35,6 +42,12 @@ func RunRevive(extraRules ...revivelib.ExtraRule) {
// move parsing flags outside of init() otherwise tests dont works properly // move parsing flags outside of init() otherwise tests dont works properly
// more info: https://github.com/golang/go/issues/46869#issuecomment-865695953 // more info: https://github.com/golang/go/issues/46869#issuecomment-865695953
initConfig() initConfig()
if versionFlag {
fmt.Print(getVersion(builtBy, date, commit, version))
os.Exit(0)
}
conf, err := config.GetConfig(configPath) conf, err := config.GetConfig(configPath)
if err != nil { if err != nil {
fail(err.Error()) fail(err.Error())
@ -160,35 +173,33 @@ func initConfig() {
flag.BoolVar(&setExitStatus, "set_exit_status", false, exitStatusUsage) flag.BoolVar(&setExitStatus, "set_exit_status", false, exitStatusUsage)
flag.IntVar(&maxOpenFiles, "max_open_files", 0, maxOpenFilesUsage) flag.IntVar(&maxOpenFiles, "max_open_files", 0, maxOpenFilesUsage)
flag.Parse() flag.Parse()
}
// Output build info (version, commit, date and builtBy) // getVersion returns build info (version, commit, date and builtBy)
if versionFlag { func getVersion(builtBy, date, commit, version string) string {
var buildInfo string var buildInfo string
if date != "unknown" && builtBy != "unknown" { if date != defaultDate && builtBy != defaultBuilder {
buildInfo = fmt.Sprintf("Built\t\t%s by %s\n", date, builtBy) buildInfo = fmt.Sprintf("Built\t\t%s by %s\n", date, builtBy)
} }
if commit != "none" { if commit != defaultCommit {
buildInfo = fmt.Sprintf("Commit:\t\t%s\n%s", commit, buildInfo) buildInfo = fmt.Sprintf("Commit:\t\t%s\n%s", commit, buildInfo)
} }
if version == "dev" { if version == defaultVersion {
bi, ok := debug.ReadBuildInfo() bi, ok := debug.ReadBuildInfo()
if ok { if ok {
version = bi.Main.Version version = bi.Main.Version
if strings.HasPrefix(version, "v") { if strings.HasPrefix(version, "v") {
version = bi.Main.Version[1:] version = strings.TrimLeft(bi.Main.Version, "v")
} }
if len(buildInfo) == 0 { if len(buildInfo) == 0 {
fmt.Printf("version %s\n", version) return fmt.Sprintf("version %s\n", version)
os.Exit(0)
}
} }
} }
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 { func fileExist(path string) bool {

View File

@ -86,3 +86,44 @@ func TestXDGConfigDirNoFile(t *testing.T) {
t.Errorf("got %q, wanted %q", got, want) 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)
}
})
}
}