github.com/nozzle/golangci-lint@v1.49.0-nz3/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  }
    42  
    43  func IsGreaterThanOrEqualGo118(v string) bool {
    44  	v1, err := hcversion.NewVersion(strings.TrimPrefix(v, "go"))
    45  	if err != nil {
    46  		return false
    47  	}
    48  
    49  	limit, err := hcversion.NewVersion("1.18")
    50  	if err != nil {
    51  		return false
    52  	}
    53  
    54  	return v1.GreaterThanOrEqual(limit)
    55  }
    56  
    57  func DetectGoVersion() string {
    58  	file, _ := gomoddirectives.GetModuleFile()
    59  
    60  	if file != nil && file.Go != nil && file.Go.Version != "" {
    61  		return file.Go.Version
    62  	}
    63  
    64  	v := os.Getenv("GOVERSION")
    65  	if v != "" {
    66  		return v
    67  	}
    68  
    69  	return "1.17"
    70  }