Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions cmd/gomoddirectives/gomoddirectives.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ func (f *flagSlice) Set(s string) error {
}

type config struct {
ReplaceAllowAny bool
ReplaceAllowList flagSlice
ReplaceAllowLocal bool
ExcludeForbidden bool
Expand All @@ -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")
Expand All @@ -63,6 +65,7 @@ func main() {
}

opts := gomoddirectives.Options{
ReplaceAllowAny: cfg.ReplaceAllowAny,
ReplaceAllowList: cfg.ReplaceAllowList,
ReplaceAllowLocal: cfg.ReplaceAllowLocal,
ExcludeForbidden: cfg.ExcludeForbidden,
Expand Down
5 changes: 5 additions & 0 deletions gomoddirectives.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ func (r Result) String() string {

// Options the analyzer options.
type Options struct {
ReplaceAllowAny bool
ReplaceAllowList []string
ReplaceAllowLocal bool
ExcludeForbidden bool
Expand Down Expand Up @@ -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 ""
Expand Down
7 changes: 7 additions & 0 deletions gomoddirectives_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
7 changes: 7 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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.
Expand Down