Skip to content

Commit

Permalink
dev: add Go version to version information
Browse files Browse the repository at this point in the history
  • Loading branch information
ldez committed Feb 22, 2023
1 parent 610a2bd commit 00eb94e
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 22 deletions.
11 changes: 10 additions & 1 deletion cmd/golangci-lint/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,23 @@ import (
)

var (
goVersion = "unknown"

// Populated by goreleaser during build
version = "master"
commit = "?"
date = ""
)

func main() {
e := commands.NewExecutor(version, commit, date)
info := commands.BuildInfo{
GoVersion: goVersion,
Version: version,
Commit: commit,
Date: date,
}

e := commands.NewExecutor(info)

if err := e.Execute(); err != nil {
fmt.Fprintf(os.Stderr, "failed executing command with error %v\n", err)
Expand Down
2 changes: 2 additions & 0 deletions cmd/golangci-lint/mod_version.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import (
//nolint:gochecknoinits
func init() {
if info, available := debug.ReadBuildInfo(); available {
goVersion = info.GoVersion

if date == "" {
version = info.Main.Version
commit = fmt.Sprintf("(unknown, mod sum: %q)", info.Main.Sum)
Expand Down
19 changes: 12 additions & 7 deletions pkg/commands/executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,20 @@ import (
"github.com/golangci/golangci-lint/pkg/timeutils"
)

type BuildInfo struct {
GoVersion string `json:"goVersion"`
Version string `json:"version"`
Commit string `json:"commit"`
Date string `json:"date"`
}

type Executor struct {
rootCmd *cobra.Command
runCmd *cobra.Command
lintersCmd *cobra.Command

exitCode int
version, commit, date string
exitCode int
buildInfo BuildInfo

cfg *config.Config // cfg is the unmarshaled data from the golangci config file.
log logutils.Log
Expand All @@ -56,13 +63,11 @@ type Executor struct {
}

// NewExecutor creates and initializes a new command executor.
func NewExecutor(version, commit, date string) *Executor {
func NewExecutor(buildInfo BuildInfo) *Executor {
startedAt := time.Now()
e := &Executor{
cfg: config.NewDefault(),
version: version,
commit: commit,
date: date,
buildInfo: buildInfo,
DBManager: lintersdb.NewManager(nil, nil),
debugf: logutils.Debug(logutils.DebugKeyExec),
}
Expand Down Expand Up @@ -135,7 +140,7 @@ func NewExecutor(version, commit, date string) *Executor {
e.loadGuard = load.NewGuard()
e.contextLoader = lint.NewContextLoader(e.cfg, e.log.Child(logutils.DebugKeyLoader), e.goenv,
e.lineCache, e.fileCache, e.pkgCache, e.loadGuard)
if err = e.initHashSalt(version); err != nil {
if err = e.initHashSalt(buildInfo.Version); err != nil {
e.log.Fatalf("Failed to init hash salt: %s", err)
}
e.debugf("Initialized executor in %s", time.Since(startedAt))
Expand Down
4 changes: 2 additions & 2 deletions pkg/commands/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ const (

func (e *Executor) persistentPreRun(_ *cobra.Command, _ []string) error {
if e.cfg.Run.PrintVersion {
_, _ = fmt.Fprintf(logutils.StdOut, "golangci-lint has version %s built from %s on %s\n", e.version, e.commit, e.date)
_ = printVersion(logutils.StdOut, e.buildInfo)
os.Exit(exitcodes.Success) // a return nil is not enough to stop the process because we are inside the `preRun`.
}

Expand Down Expand Up @@ -145,7 +145,7 @@ func (e *Executor) initRoot() {
}

func (e *Executor) needVersionOption() bool {
return e.date != ""
return e.buildInfo.Date != ""
}

func initRootFlagSet(fs *pflag.FlagSet, cfg *config.Config, needVersionOption bool) {
Expand Down
26 changes: 14 additions & 12 deletions pkg/commands/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package commands
import (
"encoding/json"
"fmt"
"io"
"os"
"strings"

Expand All @@ -13,9 +14,10 @@ import (
)

type jsonVersion struct {
Version string `json:"version"`
Commit string `json:"commit"`
Date string `json:"date"`
GoVersion string `json:"goVersion"`
Version string `json:"version"`
Commit string `json:"commit"`
Date string `json:"date"`
}

func (e *Executor) initVersionConfiguration(cmd *cobra.Command) {
Expand All @@ -39,24 +41,24 @@ func (e *Executor) initVersion() {
RunE: func(cmd *cobra.Command, _ []string) error {
switch strings.ToLower(e.cfg.Version.Format) {
case "short":
fmt.Println(e.version)
fmt.Println(e.buildInfo.Version)
return nil

case "json":
ver := jsonVersion{
Version: e.version,
Commit: e.commit,
Date: e.date,
}
return json.NewEncoder(os.Stdout).Encode(&ver)
return json.NewEncoder(os.Stdout).Encode(e.buildInfo)

default:
fmt.Printf("golangci-lint has version %s built from %s on %s\n", e.version, e.commit, e.date)
return nil
return printVersion(os.Stdout, e.buildInfo)
}
},
}

e.rootCmd.AddCommand(versionCmd)
e.initVersionConfiguration(versionCmd)
}

func printVersion(w io.Writer, buildInfo BuildInfo) error {
_, err := fmt.Fprintf(w, "golangci-lint has version %s built with %s from %s on %s\n",
buildInfo.Version, buildInfo.GoVersion, buildInfo.Commit, buildInfo.Date)
return err
}

0 comments on commit 00eb94e

Please sign in to comment.