1
0
mirror of https://github.com/mgechev/revive.git synced 2024-11-21 17:16:40 +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
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 79 additions and 27 deletions

View File

@ -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()
)
@ -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())
@ -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 {

View File

@ -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)
}
})
}
}