Created
December 22, 2020 09:31
-
-
Save rubensayshi/2bb1a074be2c63f4699eb963520ad393 to your computer and use it in GitHub Desktop.
go-ruleguard with excludes
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
package main | |
import ( | |
"flag" | |
"strings" | |
"github.com/quasilyte/go-ruleguard/analyzer" | |
"golang.org/x/tools/go/analysis" | |
"golang.org/x/tools/go/analysis/singlechecker" | |
) | |
// | |
// the original ruleguard binary is just `singlechecker.main(analyzer.Analyzer)` | |
// we've extended that to add basic exclude capabilities | |
// | |
func main() { | |
excludeFlags := argsValue{} | |
flag.Var(&excludeFlags, "ex", "excludes") | |
ruleguard := analyzer.Analyzer | |
run := ruleguard.Run | |
var excludes map[string]bool | |
runAnalyzer := func(pass *analysis.Pass) (interface{}, error) { | |
// late init of excludes because we want to let `singlechecker.Main` do the `flags.Parse` call | |
if excludes == nil { | |
excludes = make(map[string]bool, len(excludeFlags.args)) | |
for _, exclude := range excludeFlags.args { | |
excludes[exclude] = true | |
} | |
} | |
if excludes[pass.Pkg.Path()] { | |
return nil, nil | |
} | |
return run(pass) | |
} | |
ruleguard.Run = runAnalyzer | |
singlechecker.Main(ruleguard) | |
} | |
type argsValue struct { | |
args []string | |
} | |
var _ flag.Value = (*argsValue)(nil) | |
func (v *argsValue) String() string { | |
if v == nil { | |
return "" | |
} | |
return strings.Join(v.args, " ") | |
} | |
func (v *argsValue) Set(s string) error { | |
v.args = append(v.args, s) | |
return nil | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment