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:
65
cli/main.go
65
cli/main.go
@ -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 {
|
||||
|
Reference in New Issue
Block a user