github.com/vanstinator/golangci-lint@v0.0.0-20240223191551-cc572f00d9d1/pkg/config/config.go (about)

     1  package config
     2  
     3  import (
     4  	"os"
     5  	"strings"
     6  
     7  	hcversion "github.com/hashicorp/go-version"
     8  	"github.com/ldez/gomoddirectives"
     9  )
    10  
    11  // Config encapsulates the config data specified in the golangci yaml config file.
    12  type Config struct {
    13  	cfgDir string // The directory containing the golangci config file.
    14  	Run    Run
    15  
    16  	Output Output
    17  
    18  	LintersSettings LintersSettings `mapstructure:"linters-settings"`
    19  	Linters         Linters
    20  	Issues          Issues
    21  	Severity        Severity
    22  	Version         Version
    23  
    24  	InternalCmdTest bool `mapstructure:"internal-cmd-test"` // Option is used only for testing golangci-lint command, don't use it
    25  	InternalTest    bool // Option is used only for testing golangci-lint code, don't use it
    26  }
    27  
    28  // GetConfigDir returns the directory that contains golangci config file.
    29  func (c *Config) GetConfigDir() string {
    30  	return c.cfgDir
    31  }
    32  
    33  func NewDefault() *Config {
    34  	return &Config{
    35  		LintersSettings: defaultLintersSettings,
    36  	}
    37  }
    38  
    39  type Version struct {
    40  	Format string `mapstructure:"format"`
    41  	Debug  bool   `mapstructure:"debug"`
    42  }
    43  
    44  func IsGreaterThanOrEqualGo121(v string) bool {
    45  	v1, err := hcversion.NewVersion(strings.TrimPrefix(v, "go"))
    46  	if err != nil {
    47  		return false
    48  	}
    49  
    50  	limit, err := hcversion.NewVersion("1.21")
    51  	if err != nil {
    52  		return false
    53  	}
    54  
    55  	return v1.GreaterThanOrEqual(limit)
    56  }
    57  
    58  func DetectGoVersion() string {
    59  	file, _ := gomoddirectives.GetModuleFile()
    60  
    61  	if file != nil && file.Go != nil && file.Go.Version != "" {
    62  		return file.Go.Version
    63  	}
    64  
    65  	v := os.Getenv("GOVERSION")
    66  	if v != "" {
    67  		return v
    68  	}
    69  
    70  	return "1.17"
    71  }