Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement -ignore #11

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you should really use regexp.MustCompile here or manually panic on error. This currently behaves badly on malformed regexes

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indeed, this can be misleading: fixed.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did you mean to fix this?

EDIT: ah, you're manually panicking.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, you should probably implement the flag.Value interface for a regexp while you're here.

if err != nil {
log.Fatal(err)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks like exitError is used elsewhere

nevermind, that was an old rev

}
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