-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
Copy pathconfig.go
173 lines (137 loc) · 3.75 KB
/
config.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
package config
import (
"cmp"
"fmt"
"os"
"path/filepath"
"slices"
"strings"
hcversion "github.com/hashicorp/go-version"
"github.com/ldez/grignotin/goenv"
"github.com/ldez/grignotin/gomod"
"golang.org/x/mod/modfile"
)
// Config encapsulates the config data specified in the golangci-lint YAML config file.
type Config struct {
cfgDir string // The directory containing the golangci-lint config file.
Run Run `mapstructure:"run"`
Output Output `mapstructure:"output"`
LintersSettings LintersSettings `mapstructure:"linters-settings"`
Linters Linters `mapstructure:"linters"`
Issues Issues `mapstructure:"issues"`
Severity Severity `mapstructure:"severity"`
InternalCmdTest bool // Option is used only for testing golangci-lint command, don't use it
InternalTest bool // Option is used only for testing golangci-lint code, don't use it
}
// GetConfigDir returns the directory that contains golangci config file.
func (c *Config) GetConfigDir() string {
return c.cfgDir
}
func (c *Config) Validate() error {
validators := []func() error{
c.Run.Validate,
c.Output.Validate,
c.LintersSettings.Validate,
c.Linters.Validate,
c.Issues.Validate,
c.Severity.Validate,
}
for _, v := range validators {
if err := v(); err != nil {
return err
}
}
return nil
}
func NewDefault() *Config {
return &Config{
LintersSettings: defaultLintersSettings,
}
}
type Version struct {
Format string `mapstructure:"format"`
Debug bool `mapstructure:"debug"`
}
func IsGoGreaterThanOrEqual(current, limit string) bool {
v1, err := hcversion.NewVersion(strings.TrimPrefix(current, "go"))
if err != nil {
return false
}
l, err := hcversion.NewVersion(limit)
if err != nil {
return false
}
return v1.GreaterThanOrEqual(l)
}
func detectGoVersion() string {
return cmp.Or(detectGoVersionFromGoMod(), "1.17")
}
// detectGoVersionFromGoMod tries to get Go version from go.mod.
// It returns `toolchain` version if present,
// else it returns `go` version if present,
// else it returns `GOVERSION` version if present,
// else it returns empty.
func detectGoVersionFromGoMod() string {
values, err := goenv.Get(goenv.GOMOD, goenv.GOVERSION)
if err != nil {
values = map[string]string{
goenv.GOMOD: detectGoModFallback(),
}
}
if values[goenv.GOMOD] == "" {
return parseGoVersion(values[goenv.GOVERSION])
}
file, err := parseGoMod(values[goenv.GOMOD])
if err != nil {
return parseGoVersion(values[goenv.GOVERSION])
}
// The toolchain exists only if 'toolchain' version > 'go' version.
// If 'toolchain' version <= 'go' version, `go mod tidy` will remove 'toolchain' version from go.mod.
if file.Toolchain != nil && file.Toolchain.Name != "" {
return parseGoVersion(file.Toolchain.Name)
}
if file.Go != nil && file.Go.Version != "" {
return file.Go.Version
}
return parseGoVersion(values[goenv.GOVERSION])
}
func parseGoVersion(v string) string {
raw := strings.TrimPrefix(v, "go")
// prerelease version (ex: go1.24rc1)
idx := strings.IndexFunc(raw, func(r rune) bool {
return (r < '0' || r > '9') && r != '.'
})
if idx != -1 {
raw = raw[:idx]
}
return raw
}
func parseGoMod(goMod string) (*modfile.File, error) {
raw, err := os.ReadFile(filepath.Clean(goMod))
if err != nil {
return nil, fmt.Errorf("reading go.mod file: %w", err)
}
return modfile.Parse("go.mod", raw, nil)
}
func detectGoModFallback() string {
info, err := gomod.GetModuleInfo()
if err != nil {
return ""
}
wd, err := os.Getwd()
if err != nil {
return ""
}
slices.SortFunc(info, func(a, b gomod.ModInfo) int {
return cmp.Compare(len(b.Path), len(a.Path))
})
goMod := info[0]
for _, m := range info {
if !strings.HasPrefix(wd, m.Dir) {
continue
}
goMod = m
break
}
return goMod.GoMod
}