Skip to content

Commit

Permalink
Support -ignore flag (#27)
Browse files Browse the repository at this point in the history
  • Loading branch information
uudashr authored Sep 6, 2023
1 parent 4a90dac commit be33ac6
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 17 deletions.
16 changes: 9 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -166,13 +166,14 @@ Usage:
Flags:
-over N show functions with complexity > N only and
return exit code 1 if the set is non-empty
-top N show the top N most complex functions only
-avg show the average complexity over all functions,
not depending on whether -over or -top are set
-json encode the output as JSON
-f format string the format to use (default "{{.PkgName}}.{{.FuncName}}:{{.Complexity}}:{{.Pos}}")
-over N show functions with complexity > N only and
return exit code 1 if the set is non-empty
-top N show the top N most complex functions only
-avg show the average complexity over all functions,
not depending on whether -over or -top are set
-json encode the output as JSON
-f format string the format to use (default "{{.PkgName}}.{{.FuncName}}:{{.Complexity}}:{{.Pos}}")
-ignore expr ignore files matching the given regexp
The (default) output fields for each line are:
Expand All @@ -198,6 +199,7 @@ $ gocognit main.go
$ gocognit -top 10 src/
$ gocognit -over 25 docker
$ gocognit -avg .
$ gocognit -ignore "_test|testdata" .
```

The output fields for each line are:
Expand Down
43 changes: 33 additions & 10 deletions cmd/gocognit/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
"log"
"os"
"path/filepath"
"regexp"
"sort"
"strings"
"text/template"
Expand All @@ -41,13 +42,14 @@ Usage:
Flags:
-over N show functions with complexity > N only and
return exit code 1 if the set is non-empty
-top N show the top N most complex functions only
-avg show the average complexity over all functions,
not depending on whether -over or -top are set
-json encode the output as JSON
-f format string the format to use (default "{{.PkgName}}.{{.FuncName}}:{{.Complexity}}:{{.Pos}}")
-over N show functions with complexity > N only and
return exit code 1 if the set is non-empty
-top N show the top N most complex functions only
-avg show the average complexity over all functions,
not depending on whether -over or -top are set
-json encode the output as JSON
-f format string the format to use (default "{{.PkgName}}.{{.FuncName}}:{{.Complexity}}:{{.Pos}}")
-ignore expr ignore files matching the given regexp
The (default) output fields for each line are:
Expand Down Expand Up @@ -84,12 +86,14 @@ func main() {
avg bool
format string
jsonEncode bool
ignoreExpr string
)
flag.IntVar(&over, "over", defaultOverFlagVal, "show functions with complexity > N only")
flag.IntVar(&top, "top", defaultTopFlagVal, "show the top N most complex functions only")
flag.BoolVar(&avg, "avg", false, "show the average complexity")
flag.StringVar(&format, "f", defaultFormat, "the format to use")
flag.BoolVar(&jsonEncode, "json", false, "encode the output as JSON")
flag.StringVar(&ignoreExpr, "ignore", "", "ignore files matching the given regexp")

log.SetFlags(0)
log.SetPrefix("gocognit: ")
Expand All @@ -112,7 +116,12 @@ func main() {

sort.Sort(byComplexity(stats))

filteredStats := filterStats(stats, top, over)
ignoreRegexp, err := prepareRegexp(ignoreExpr)
if err != nil {
log.Fatal(err)
}

filteredStats := filterStats(stats, ignoreRegexp, top, over)
var written int
if jsonEncode {
written, err = writeJSONStats(os.Stdout, filteredStats)
Expand Down Expand Up @@ -219,9 +228,18 @@ func writeJSONStats(w io.Writer, stats []gocognit.Stat) (int, error) {
return len(stats), nil
}

func filterStats(sortedStats []gocognit.Stat, top, over int) []gocognit.Stat {
func prepareRegexp(expr string) (*regexp.Regexp, error) {
if expr == "" {
return nil, nil
}

return regexp.Compile(expr)
}

func filterStats(sortedStats []gocognit.Stat, ignoreRegexp *regexp.Regexp, top, over int) []gocognit.Stat {
var filtered []gocognit.Stat
for i, stat := range sortedStats {
i := 0
for _, stat := range sortedStats {
if i == top {
break
}
Expand All @@ -230,7 +248,12 @@ func filterStats(sortedStats []gocognit.Stat, top, over int) []gocognit.Stat {
break
}

if ignoreRegexp != nil && ignoreRegexp.MatchString(stat.Pos.Filename) {
continue
}

filtered = append(filtered, stat)
i++
}

return filtered
Expand Down

0 comments on commit be33ac6

Please sign in to comment.