Skip to content

dev: add Go version to version information #3625

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Mar 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 21 additions & 1 deletion cmd/golangci-lint/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,40 @@ package main
import (
"fmt"
"os"
"runtime/debug"

"github.com/golangci/golangci-lint/pkg/commands"
"github.com/golangci/golangci-lint/pkg/exitcodes"
)

var (
goVersion = "unknown"

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

func main() {
e := commands.NewExecutor(version, commit, date)
if buildInfo, available := debug.ReadBuildInfo(); available {
goVersion = buildInfo.GoVersion

if date == "" {
version = buildInfo.Main.Version
commit = fmt.Sprintf("(unknown, mod sum: %q)", buildInfo.Main.Sum)
date = "(unknown)"
}
}

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
17 changes: 0 additions & 17 deletions cmd/golangci-lint/mod_version.go

This file was deleted.

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
47 changes: 34 additions & 13 deletions pkg/commands/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ package commands
import (
"encoding/json"
"fmt"
"io"
"os"
"runtime/debug"
"strings"

"github.com/spf13/cobra"
Expand All @@ -12,10 +14,9 @@ import (
"github.com/golangci/golangci-lint/pkg/config"
)

type jsonVersion struct {
Version string `json:"version"`
Commit string `json:"commit"`
Date string `json:"date"`
type versionInfo struct {
Info BuildInfo
BuildInfo *debug.BuildInfo
}

func (e *Executor) initVersionConfiguration(cmd *cobra.Command) {
Expand All @@ -28,6 +29,7 @@ func initVersionFlagSet(fs *pflag.FlagSet, cfg *config.Config) {
// Version config
vc := &cfg.Version
fs.StringVar(&vc.Format, "format", "", wh("The version's format can be: 'short', 'json'"))
fs.BoolVar(&vc.Debug, "debug", false, wh("Add build information"))
}

func (e *Executor) initVersion() {
Expand All @@ -37,26 +39,45 @@ func (e *Executor) initVersion() {
Args: cobra.NoArgs,
ValidArgsFunction: cobra.NoFileCompletions,
RunE: func(cmd *cobra.Command, _ []string) error {
if e.cfg.Version.Debug {
info, ok := debug.ReadBuildInfo()
if !ok {
return nil
}

switch strings.ToLower(e.cfg.Version.Format) {
case "json":
return json.NewEncoder(os.Stdout).Encode(versionInfo{
Info: e.buildInfo,
BuildInfo: info,
})

default:
fmt.Println(info.String())
return printVersion(os.Stdout, e.buildInfo)
}
}

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
}
1 change: 1 addition & 0 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ func NewDefault() *Config {

type Version struct {
Format string `mapstructure:"format"`
Debug bool `mapstructure:"debug"`
}

func IsGreaterThanOrEqualGo118(v string) bool {
Expand Down