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

add option for only in current directory #42

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
15 changes: 14 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"go/build"
"log"
"os"
"path/filepath"
"sort"
"strings"
)
Expand All @@ -28,20 +29,25 @@ 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")
maxLevel = flag.Int("maxlevel", 256, "max level of go dependency graph")

buildTags []string
buildContext = build.Default

cwd string
cwbd string
)

func init() {
flag.BoolVar(ignoreStdlib, "s", false, "(alias for -nostdlib) ignore packages in the Go standard library")
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")
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
}

Expand Down