rework modules download mode option
This commit is contained in:
parent
b693037af0
commit
09c65fcf42
@ -36,6 +36,16 @@ run:
|
||||
- ".*\\.my\\.go$"
|
||||
- lib/bad.go
|
||||
|
||||
# by default isn't set. If set we pass it to "go list -mod={option}". From "go help modules":
|
||||
# If invoked with -mod=readonly, the go command is disallowed from the implicit
|
||||
# automatic updating of go.mod described above. Instead, it fails when any changes
|
||||
# to go.mod are needed. This setting is most useful to check that go.mod does
|
||||
# not need updates, such as in a continuous integration and testing system.
|
||||
# If invoked with -mod=vendor, the go command assumes that the vendor
|
||||
# directory holds the correct copies of dependencies and ignores
|
||||
# the dependency descriptions in go.mod.
|
||||
modules-download-mode: readonly|release|vendor
|
||||
|
||||
|
||||
# output configuration options
|
||||
output:
|
||||
|
11
README.md
11
README.md
@ -432,7 +432,6 @@ Flags:
|
||||
--print-linter-name Print linter name in issue line (default true)
|
||||
--issues-exit-code int Exit code when issues were found (default 1)
|
||||
--build-tags strings Build tags
|
||||
--mod string module download mode to use: readonly or vendor (passed to go list)
|
||||
--deadline duration Deadline for total work (default 1m0s)
|
||||
--tests Analyze tests (*_test.go) (default true)
|
||||
--print-resources-usage Print avg and max memory usage of golangci-lint and total time
|
||||
@ -552,6 +551,16 @@ run:
|
||||
- ".*\\.my\\.go$"
|
||||
- lib/bad.go
|
||||
|
||||
# by default isn't set. If set we pass it to "go list -mod={option}". From "go help modules":
|
||||
# If invoked with -mod=readonly, the go command is disallowed from the implicit
|
||||
# automatic updating of go.mod described above. Instead, it fails when any changes
|
||||
# to go.mod are needed. This setting is most useful to check that go.mod does
|
||||
# not need updates, such as in a continuous integration and testing system.
|
||||
# If invoked with -mod=vendor, the go command assumes that the vendor
|
||||
# directory holds the correct copies of dependencies and ignores
|
||||
# the dependency descriptions in go.mod.
|
||||
modules-download-mode: readonly|release|vendor
|
||||
|
||||
|
||||
# output configuration options
|
||||
output:
|
||||
|
@ -63,7 +63,6 @@ func initFlagSet(fs *pflag.FlagSet, cfg *config.Config, m *lintersdb.Manager) {
|
||||
fs.IntVar(&rc.ExitCodeIfIssuesFound, "issues-exit-code",
|
||||
exitcodes.IssuesFound, wh("Exit code when issues were found"))
|
||||
fs.StringSliceVar(&rc.BuildTags, "build-tags", nil, wh("Build tags"))
|
||||
fs.StringVar(&rc.Mod, "mod", "", wh("module download mode to use: readonly or vendor (passed to go list)"))
|
||||
fs.DurationVar(&rc.Deadline, "deadline", time.Minute, wh("Deadline for total work"))
|
||||
fs.BoolVar(&rc.AnalyzeTests, "tests", true, wh("Analyze tests (*_test.go)"))
|
||||
fs.BoolVar(&rc.PrintResourcesUsage, "print-resources-usage", false,
|
||||
|
@ -109,8 +109,8 @@ type Run struct {
|
||||
|
||||
Args []string
|
||||
|
||||
BuildTags []string `mapstructure:"build-tags"`
|
||||
Mod string `mapstructure:"mod"`
|
||||
BuildTags []string `mapstructure:"build-tags"`
|
||||
ModulesDownloadMode string `mapstructure:"modules-download-mode"`
|
||||
|
||||
ExitCodeIfIssuesFound int `mapstructure:"issues-exit-code"`
|
||||
AnalyzeTests bool `mapstructure:"tests"`
|
||||
|
@ -193,6 +193,35 @@ func (cl ContextLoader) buildArgs() []string {
|
||||
return retArgs
|
||||
}
|
||||
|
||||
func (cl ContextLoader) makeBuildFlags() ([]string, error) {
|
||||
var buildFlags []string
|
||||
|
||||
if len(cl.cfg.Run.BuildTags) != 0 {
|
||||
// go help build
|
||||
buildFlags = append(buildFlags, "-tags", strings.Join(cl.cfg.Run.BuildTags, " "))
|
||||
}
|
||||
|
||||
mod := cl.cfg.Run.ModulesDownloadMode
|
||||
if mod != "" {
|
||||
// go help modules
|
||||
allowedMods := []string{"release", "readonly", "vendor"}
|
||||
var ok bool
|
||||
for _, am := range allowedMods {
|
||||
if am == mod {
|
||||
ok = true
|
||||
break
|
||||
}
|
||||
}
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("invalid modules download path %s, only (%s) allowed", mod, strings.Join(allowedMods, "|"))
|
||||
}
|
||||
|
||||
buildFlags = append(buildFlags, fmt.Sprintf("-mod=%s", cl.cfg.Run.ModulesDownloadMode))
|
||||
}
|
||||
|
||||
return buildFlags, nil
|
||||
}
|
||||
|
||||
func (cl ContextLoader) loadPackages(ctx context.Context, loadMode packages.LoadMode) ([]*packages.Package, error) {
|
||||
defer func(startedAt time.Time) {
|
||||
cl.log.Infof("Go packages loading at mode %s took %s", stringifyLoadMode(loadMode), time.Since(startedAt))
|
||||
@ -200,16 +229,9 @@ func (cl ContextLoader) loadPackages(ctx context.Context, loadMode packages.Load
|
||||
|
||||
cl.prepareBuildContext()
|
||||
|
||||
var buildFlags []string
|
||||
|
||||
if len(cl.cfg.Run.BuildTags) != 0 {
|
||||
// go help build
|
||||
buildFlags = []string{"-tags", strings.Join(cl.cfg.Run.BuildTags, " ")}
|
||||
}
|
||||
|
||||
if cl.cfg.Run.Mod != "" {
|
||||
// go help module
|
||||
buildFlags = append(buildFlags, fmt.Sprintf("-mod=%s", cl.cfg.Run.Mod))
|
||||
buildFlags, err := cl.makeBuildFlags()
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "failed to make build flags for go list")
|
||||
}
|
||||
|
||||
conf := &packages.Config{
|
||||
|
Loading…
x
Reference in New Issue
Block a user