Skip to content

Commit

Permalink
chore: code cleanup (#24)
Browse files Browse the repository at this point in the history
Signed-off-by: Timon Wong <[email protected]>

Signed-off-by: Timon Wong <[email protected]>
  • Loading branch information
timonwong authored Aug 29, 2022
1 parent 95aac08 commit f040dc8
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 20 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ Flags:
-debug string
debug flags, any subset of "fpstv"
-disable value
comma-separated list of disabled logger checker (klog,logr,zap).
comma-separated list of disabled logger checker (klog,logr,zap)
-fix
apply all suggested fixes
-flags
Expand All @@ -49,7 +49,7 @@ Flags:
-memprofile string
write memory profile to this file
-rulefile value
path to a file contains a list of rules.
path to a file contains a list of rules
-source
no effect (deprecated)
-tags string
Expand Down
16 changes: 5 additions & 11 deletions internal/rules/rules.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,28 +6,22 @@ import (
"fmt"
"go/types"
"io"
"sort"
"strings"

"github.com/timonwong/loggercheck/internal/bytebufferpool"
"github.com/timonwong/loggercheck/internal/sets"
)

var ErrInvalidRule = errors.New("invalid rule format")

type RulesetList []Ruleset

func (rl RulesetList) Names() []string {
keys := make([]string, len(rl))
visited := make(map[string]struct{})
for i, pg := range rl {
if _, ok := visited[pg.Name]; ok {
continue
}
visited[pg.Name] = struct{}{}
keys[i] = pg.Name
names := make([]string, 0, len(rl))
for _, rs := range rl {
names = append(names, rs.Name)
}
sort.Strings(keys)
return keys
return sets.NewString(names...).List()
}

type Ruleset struct {
Expand Down
14 changes: 7 additions & 7 deletions loggercheck.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ func NewAnalyzer(opts ...Option) *analysis.Analyzer {

checkerKeys := strings.Join(staticRuleList.Names(), ",")
a.Flags.Init("loggercheck", flag.ExitOnError)
a.Flags.StringVar(&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))
a.Flags.StringVar(&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
}

Expand All @@ -60,21 +60,21 @@ func (l *loggercheck) isValidLoggerFunc(fn *types.Func) bool {
}

for i := range staticRuleList {
pg := &staticRuleList[i]
if l.isCheckerDisabled(pg.Name) {
rs := &staticRuleList[i]
if l.isCheckerDisabled(rs.Name) {
// Skip ignored logger checker.
continue
}

if pg.Match(fn, pkg) {
if rs.Match(fn, pkg) {
return true
}
}

customRulesetList := l.customRulesetList
for i := range customRulesetList {
pg := &customRulesetList[i]
if pg.Match(fn, pkg) {
rs := &customRulesetList[i]
if rs.Match(fn, pkg) {
return true
}
}
Expand Down

0 comments on commit f040dc8

Please sign in to comment.