diff --git a/README.md b/README.md index 81e3677..bab4a59 100644 --- a/README.md +++ b/README.md @@ -62,6 +62,13 @@ list of prefixes: godepgraph -p github.com,launchpad.net bitbucket.org/foo/bar +## Filter +### Only in Current Directory + +Only include package in current directory: + + godepgraph -c bitbucket.org/foo/bar + ## Example Here's some example output for a component of Gary Burd's [gopkgdoc][gopkgdoc] project: diff --git a/main.go b/main.go index 1592cf7..fc5a2d6 100644 --- a/main.go +++ b/main.go @@ -6,6 +6,7 @@ import ( "go/build" "log" "os" + "path/filepath" "sort" "strings" ) @@ -28,6 +29,7 @@ var ( ignorePrefixes = flag.String("ignoreprefixes", "", "a comma-separated list of prefixes to ignore") ignorePackages = flag.String("ignorepackages", "", "a comma-separated list of packages to ignore") onlyPrefix = flag.String("onlyprefixes", "", "a comma-separated list of prefixes to include") + onlyInSrc = flag.Bool("onlyinsrc", false, "include packages only in current directory") tagList = flag.String("tags", "", "a comma-separated list of build tags to consider satisfied during the build") horizontal = flag.Bool("horizontal", false, "lay out the dependency graph horizontally instead of vertically") withTests = flag.Bool("withtests", false, "include test packages") @@ -35,6 +37,9 @@ var ( buildTags []string buildContext = build.Default + + cwd string + cwbd string ) func init() { @@ -42,6 +47,7 @@ func init() { flag.StringVar(ignorePrefixes, "p", "", "(alias for -ignoreprefixes) a comma-separated list of prefixes to ignore") flag.StringVar(ignorePackages, "i", "", "(alias for -ignorepackages) a comma-separated list of packages to ignore") flag.StringVar(onlyPrefix, "o", "", "(alias for -onlyprefixes) a comma-separated list of prefixes to include") + flag.BoolVar(onlyInSrc, "c", false, "(alias for -onlyinsrc) include packages only in current directory") flag.BoolVar(withTests, "t", false, "(alias for -withtests) include test packages") flag.IntVar(maxLevel, "l", 256, "(alias for -maxlevel) maximum level of the go dependency graph") flag.BoolVar(withGoroot, "d", false, "(alias for -withgoroot) show dependencies of packages in the Go standard library") @@ -75,10 +81,12 @@ func main() { } buildContext.BuildTags = buildTags - cwd, err := os.Getwd() + var err error + cwd, err = os.Getwd() if err != nil { log.Fatalf("failed to get cwd: %s", err) } + cwbd = filepath.Base(cwd) for _, a := range args { if err := processPackage(cwd, a, 0, "", *stopOnError); err != nil { log.Fatal(err) @@ -244,6 +252,11 @@ func isIgnored(pkg *build.Package) bool { if *ignoreVendor && isVendored(pkg.ImportPath) { return true } + + if *onlyInSrc && pkg.SrcRoot+"/"+cwbd != cwd { + return true + } + return ignored[normalizeVendor(pkg.ImportPath)] || (pkg.Goroot && *ignoreStdlib) || hasPrefixes(normalizeVendor(pkg.ImportPath), ignoredPrefixes) }