-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added new linter gomodguard, disabled by default
- Loading branch information
1 parent
4958e50
commit 020b9e7
Showing
39 changed files
with
4,479 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
package golinters | ||
|
||
import ( | ||
"log" | ||
"os" | ||
"sync" | ||
|
||
"github.com/ryancurrah/gomodguard" | ||
"golang.org/x/tools/go/analysis" | ||
|
||
"github.com/golangci/golangci-lint/pkg/golinters/goanalysis" | ||
"github.com/golangci/golangci-lint/pkg/lint/linter" | ||
"github.com/golangci/golangci-lint/pkg/result" | ||
) | ||
|
||
const ( | ||
gomodguardName = "gomodguard" | ||
) | ||
|
||
// NewGomodguard returns a new Gomodguard linter. | ||
func NewGomodguard() *goanalysis.Linter { | ||
var ( | ||
issues []goanalysis.Issue | ||
mu = sync.Mutex{} | ||
analyzer = &analysis.Analyzer{ | ||
Name: goanalysis.TheOnlyAnalyzerName, | ||
Doc: goanalysis.TheOnlyanalyzerDoc, | ||
} | ||
) | ||
|
||
return goanalysis.NewLinter( | ||
gomodguardName, | ||
"Allow and block list linter for direct Go module dependencies.", | ||
[]*analysis.Analyzer{analyzer}, | ||
nil, | ||
).WithContextSetter(func(lintCtx *linter.Context) { | ||
analyzer.Run = func(pass *analysis.Pass) (interface{}, error) { | ||
var ( | ||
files = []string{} | ||
linterCfg = lintCtx.Cfg.LintersSettings.Gomodguard | ||
processorCfg = gomodguard.Configuration{} | ||
) | ||
processorCfg.Allowed.Modules = linterCfg.Allowed.Modules | ||
processorCfg.Allowed.Domains = linterCfg.Allowed.Domains | ||
for n := range linterCfg.Blocked.Modules { | ||
for k, v := range linterCfg.Blocked.Modules[n] { | ||
m := gomodguard.BlockedModule{k: gomodguard.Recommendations{ | ||
Recommendations: v.Recommendations, | ||
Reason: v.Reason, | ||
}} | ||
processorCfg.Blocked.Modules = append(processorCfg.Blocked.Modules, m) | ||
break | ||
} | ||
} | ||
|
||
for _, file := range pass.Files { | ||
files = append(files, pass.Fset.Position(file.Pos()).Filename) | ||
} | ||
|
||
processor, err := gomodguard.NewProcessor(processorCfg, log.New(os.Stderr, "", 0)) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
gomodguardErrors := processor.ProcessFiles(files) | ||
if len(gomodguardErrors) == 0 { | ||
return nil, nil | ||
} | ||
|
||
mu.Lock() | ||
defer mu.Unlock() | ||
|
||
for _, err := range gomodguardErrors { | ||
issues = append(issues, goanalysis.NewIssue(&result.Issue{ //nolint:scopelint | ||
FromLinter: gomodguardName, | ||
Pos: err.Position, | ||
Text: err.Reason, | ||
}, pass)) | ||
} | ||
|
||
return nil, nil | ||
} | ||
}).WithIssuesReporter(func(*linter.Context) []goanalysis.Issue { | ||
return issues | ||
}).WithLoadMode(goanalysis.LoadModeSyntax) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
linters-settings: | ||
gomodguard: | ||
allowed: | ||
modules: # List of allowed modules | ||
- golang.org/x/mod/modfile | ||
blocked: | ||
modules: # List of blocked modules | ||
- gopkg.in/yaml.v2: # Blocked module | ||
recommendations: # Recommended modules that should be used instead (Optional) | ||
- github.com/kylelemons/go-gypsy | ||
reason: "This is an example of recommendations." # Reason why the recommended module should be used (Optional) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
//args: -Egomodguard | ||
//config_path: testdata/configs/gomodguard.yml | ||
package testdata | ||
|
||
import ( | ||
"log" | ||
|
||
"golang.org/x/mod/modfile" | ||
"gopkg.in/yaml.v2" // ERROR : "import of package `gopkg.in/yaml.v2` is blocked because the module is in the blocked modules list. `github.com/kylelemons/go-gypsy` is a recommended module. This is an example of recommendations." | ||
) | ||
|
||
// Something just some struct | ||
type Something struct{} | ||
|
||
func aAllowedImport() { // nolint: deadcode,unused | ||
mfile, _ := modfile.Parse("go.mod", []byte{}, nil) | ||
|
||
log.Println(mfile) | ||
} | ||
|
||
func aBlockedImport() { // nolint: deadcode,unused | ||
data := []byte{} | ||
something := Something{} | ||
_ = yaml.Unmarshal(data, &something) | ||
|
||
log.Println(data) | ||
} |
19 changes: 19 additions & 0 deletions
19
vendor/github.com/ryancurrah/gomodguard/.gitignore
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
15 changes: 15 additions & 0 deletions
15
vendor/github.com/ryancurrah/gomodguard/.gomodguard.yaml
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.