From a5526c8561b24bfc502b155e9c56639239a0b0dd Mon Sep 17 00:00:00 2001 From: Jonathan Gautheron Date: Thu, 24 Dec 2015 12:31:48 +0100 Subject: [PATCH] Implement -ignore --- README.md | 1 + gocyclo.go | 29 ++++++++++++++++++++--------- 2 files changed, 21 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index b2ec36a..dd2cb84 100644 --- a/README.md +++ b/README.md @@ -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: diff --git a/gocyclo.go b/gocyclo.go index 00ab50d..609388f 100644 --- a/gocyclo.go +++ b/gocyclo.go @@ -28,6 +28,7 @@ import ( "log" "os" "path/filepath" + "regexp" "sort" "strings" ) @@ -37,11 +38,12 @@ Usage: gocyclo [flags] ... 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: @@ -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() { @@ -98,6 +101,14 @@ 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) + return err == nil && match +} + func analyzeFile(fname string, stats []stat) []stat { fset := token.NewFileSet() f, err := parser.ParseFile(fset, fname, nil, 0) @@ -109,7 +120,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