Skip to content

Commit

Permalink
Implement -ignore
Browse files Browse the repository at this point in the history
  • Loading branch information
Jonathan Gautheron committed Dec 25, 2015
1 parent 6acd434 commit b825e1a
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 9 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ Examples:
$ gocyclo -top 10 src/
$ gocyclo -over 25 docker
$ gocyclo -avg .
$ gocyclo -ignore "_test|Godeps" .

The output fields for each line are:

Expand Down
32 changes: 23 additions & 9 deletions gocyclo.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (
"log"
"os"
"path/filepath"
"regexp"
"sort"
"strings"
)
Expand All @@ -37,11 +38,12 @@ Usage:
gocyclo [flags] <Go file or directory> ...
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
-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
-ignore REGEX exclude files matching the given regular expression
The output fields for each line are:
<complexity> <package> <function> <file:row:column>
Expand All @@ -53,9 +55,10 @@ func usage() {
}

var (
over = flag.Int("over", 0, "show functions with complexity > N only")
top = flag.Int("top", -1, "show the top N most complex functions only")
avg = flag.Bool("avg", false, "show the average complexity")
over = flag.Int("over", 0, "show functions with complexity > N only")
top = flag.Int("top", -1, "show the top N most complex functions only")
avg = flag.Bool("avg", false, "show the average complexity")
ignore = flag.String("ignore", "", "exclude files matching the given regular expression")
)

func main() {
Expand Down Expand Up @@ -98,6 +101,17 @@ func isDir(filename string) bool {
return err == nil && fi.IsDir()
}

func isIgnored(path string) bool {
if len(*ignore) == 0 {
return false
}
match, err := regexp.MatchString(*ignore, path)
if err != nil {
log.Fatal(err)
}
return match
}

func analyzeFile(fname string, stats []stat) []stat {
fset := token.NewFileSet()
f, err := parser.ParseFile(fset, fname, nil, 0)
Expand All @@ -109,7 +123,7 @@ func analyzeFile(fname string, stats []stat) []stat {

func analyzeDir(dirname string, stats []stat) []stat {
filepath.Walk(dirname, func(path string, info os.FileInfo, err error) error {
if err == nil && !info.IsDir() && strings.HasSuffix(path, ".go") {
if err == nil && !info.IsDir() && strings.HasSuffix(path, ".go") && !isIgnored(path+info.Name()) {
stats = analyzeFile(path, stats)
}
return err
Expand Down

0 comments on commit b825e1a

Please sign in to comment.