Skip to content

Commit

Permalink
refactor: remove Config struct
Browse files Browse the repository at this point in the history
  • Loading branch information
ttys3 committed Aug 29, 2022
1 parent 3a42c8d commit 274a646
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 47 deletions.
6 changes: 5 additions & 1 deletion cmd/loggercheck/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,9 @@ import (
)

func main() {
singlechecker.Main(loggercheck.NewAnalyzer())
a, err := loggercheck.NewAnalyzer()
if err != nil {
panic(err)
}
singlechecker.Main(a)
}
25 changes: 0 additions & 25 deletions config.go

This file was deleted.

11 changes: 5 additions & 6 deletions loggercheck.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,14 @@ import (

const Doc = `Checks key valur pairs for common logger libraries (logr,klog,zap).`

func NewAnalyzer(opts ...Option) *analysis.Analyzer {
func NewAnalyzer(opts ...Option) (*analysis.Analyzer, error) {
l := &loggercheck{}
for _, o := range opts {
o(l)
if err := o(l); err != nil {
return nil, err
}
}

l.cfg.init(l)
a := &analysis.Analyzer{
Name: "loggercheck",
Doc: Doc,
Expand All @@ -33,14 +34,12 @@ func NewAnalyzer(opts ...Option) *analysis.Analyzer {
a.Flags.Init("loggercheck", flag.ExitOnError)
a.Flags.Var(&l.ruleFile, "rulefile", "path to a file contains a list of rules.")
a.Flags.Var(&l.disable, "disable", fmt.Sprintf("comma-separated list of disabled logger checker (%s).", checkerKeys))
return a
return a, nil
}

type loggercheck struct {
disable loggerCheckersFlag // flag -disable
ruleFile ruleFileFlag // flag -rulefile

cfg *Config // used for external integration, for example golangci-lint
}

func (l *loggercheck) isCheckerDisabled(name string) bool {
Expand Down
17 changes: 7 additions & 10 deletions loggercheck_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func TestLinter(t *testing.T) {
for _, tc := range testCases {
tc := tc
t.Run(tc.name, func(t *testing.T) {
a := loggercheck.NewAnalyzer()
a, _ := loggercheck.NewAnalyzer()
err := a.Flags.Parse(tc.flags)
require.NoError(t, err)
analysistest.Run(t, testdata, a, tc.patterns)
Expand Down Expand Up @@ -71,27 +71,24 @@ func TestOptions(t *testing.T) {
{
name: "disable-all-then-enable-mylogger",
options: []loggercheck.Option{
loggercheck.WithConfig(&loggercheck.Config{
Disable: []string{"klog", "logr", "zap"},
Rules: rules,
}),
loggercheck.WithDisable([]string{"klog", "logr", "zap"}),
loggercheck.WithRules(rules),
},
},
{
name: "ignore-logr",
options: []loggercheck.Option{
loggercheck.WithConfig(&loggercheck.Config{
Disable: []string{"logr"},
Rules: rules,
}),
loggercheck.WithDisable([]string{"logr"}),
loggercheck.WithRules(rules),
},
},
}

for _, tc := range testCases {
tc := tc
t.Run(tc.name, func(t *testing.T) {
a := loggercheck.NewAnalyzer(tc.options...)
a, err := loggercheck.NewAnalyzer(tc.options...)
require.NoError(t, err)
analysistest.Run(t, testdata, a, "a/customonly")
})
}
Expand Down
26 changes: 22 additions & 4 deletions options.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,27 @@
package loggercheck

type Option func(*loggercheck)
import (
"github.com/timonwong/loggercheck/rules"
"github.com/timonwong/loggercheck/sets"
)

func WithConfig(cfg *Config) Option {
return func(l *loggercheck) {
l.cfg = cfg
type Option func(*loggercheck) error

func WithDisable(disable []string) Option {
return func(l *loggercheck) error {
l.disable.StringSet = sets.NewStringSet(disable...)
return nil
}
}

func WithRules(customRules []string) Option {
return func(l *loggercheck) error {
l.ruleFile.filename = "<internal>"
ruleset, err := rules.ParseRules(customRules)
if err != nil {
return err
}
l.ruleFile.rulsetList = ruleset
return nil
}
}
6 changes: 5 additions & 1 deletion plugin/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,11 @@ var AnalyzerPlugin analyzerPlugin
type analyzerPlugin struct{}

func (analyzerPlugin) GetAnalyzers() []*analysis.Analyzer {
a, err := loggercheck.NewAnalyzer()
if err != nil {
panic(err)
}
return []*analysis.Analyzer{
loggercheck.NewAnalyzer(),
a,
}
}

0 comments on commit 274a646

Please sign in to comment.