diff --git a/cmd/gomoddirectives/gomoddirectives.go b/cmd/gomoddirectives/gomoddirectives.go index 487d535..91a9655 100644 --- a/cmd/gomoddirectives/gomoddirectives.go +++ b/cmd/gomoddirectives/gomoddirectives.go @@ -24,6 +24,7 @@ func (f *flagSlice) Set(s string) error { } type config struct { + ReplaceAllowAny bool ReplaceAllowList flagSlice ReplaceAllowLocal bool ExcludeForbidden bool @@ -42,6 +43,7 @@ func main() { flag.BoolVar(&cfg.ExcludeForbidden, "exclude", false, "Forbid the use of exclude directives") flag.BoolVar(&cfg.IgnoreForbidden, "ignore", false, "Forbid the use of ignore directives") + flag.BoolVar(&cfg.ReplaceAllowAny, "replace-any", false, "Allow any replace directives") flag.Var(&cfg.ReplaceAllowList, "list", "List of allowed replace directives") flag.BoolVar(&cfg.ReplaceAllowLocal, "local", false, "Allow local replace directives") flag.BoolVar(&cfg.RetractAllowNoExplanation, "retract-no-explanation", false, "Allow to use retract directives without explanation") @@ -63,6 +65,7 @@ func main() { } opts := gomoddirectives.Options{ + ReplaceAllowAny: cfg.ReplaceAllowAny, ReplaceAllowList: cfg.ReplaceAllowList, ReplaceAllowLocal: cfg.ReplaceAllowLocal, ExcludeForbidden: cfg.ExcludeForbidden, diff --git a/gomoddirectives.go b/gomoddirectives.go index ae416c1..3a4c26a 100644 --- a/gomoddirectives.go +++ b/gomoddirectives.go @@ -52,6 +52,7 @@ func (r Result) String() string { // Options the analyzer options. type Options struct { + ReplaceAllowAny bool ReplaceAllowList []string ReplaceAllowLocal bool ExcludeForbidden bool @@ -252,6 +253,10 @@ func checkReplaceDirectives(file *modfile.File, opts Options) []Result { } func checkReplaceDirective(opts Options, r *modfile.Replace) string { + if opts.ReplaceAllowAny { + return "" + } + if isLocal(r) { if opts.ReplaceAllowLocal { return "" diff --git a/gomoddirectives_test.go b/gomoddirectives_test.go index 3078818..da13ead 100644 --- a/gomoddirectives_test.go +++ b/gomoddirectives_test.go @@ -47,6 +47,13 @@ func TestAnalyzeFile(t *testing.T) { }, }, }, + { + desc: "replace: allow all", + modulePath: "replace/go.mod", + opts: Options{ + ReplaceAllowAny: true, + }, + }, { desc: "replace: allow an element", modulePath: "replace/go.mod", diff --git a/readme.md b/readme.md index 52c6a4c..a9f6875 100644 --- a/readme.md +++ b/readme.md @@ -18,6 +18,10 @@ linters: settings: gomoddirectives: + # Allow any `replace` directives. + # Default: false + replace-any: true + # Allow local `replace` directives. # Default: false replace-local: true @@ -85,6 +89,8 @@ Flags: List of allowed replace directives -local Allow local replace directives + -replace-any + Allow any replace directives -retract-no-explanation Allow to use retract directives without explanation -tool @@ -118,6 +124,7 @@ retract ( ### [`replace`](https://golang.org/ref/mod#go-mod-file-replace) directives - Ban all `replace` directives. +- Allow any `replace` directives. - Allow only local `replace` directives. - Allow only some `replace` directives. - Detect duplicated `replace` directives.