Skip to content

Commit 983d57c

Browse files
author
KyleBanks
committed
Handle relative paths, resolves #1
1 parent c9d40eb commit 983d57c

File tree

5 files changed

+68
-15
lines changed

5 files changed

+68
-15
lines changed

Makefile

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ include github.com/KyleBanks/make/git/precommit
1111

1212
# Runs a number of depth commands as examples of what's possible.
1313
example: | install
14-
depth github.com/KyleBanks/depth/cmd/depth strings
14+
depth github.com/KyleBanks/depth/cmd/depth strings ./
1515

1616
depth -internal strings
1717

@@ -22,4 +22,8 @@ example: | install
2222
depth -test -internal strings
2323

2424
depth -test -internal -max 3 strings
25+
26+
depth .
27+
28+
depth ./cmd/depth
2529
.PHONY: example

README.md

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ go get github.com/KyleBanks/depth/cmd/depth
2020

2121
### Command-Line
2222

23-
Simply execute `depth` with one or more package names to visualize:
23+
Simply execute `depth` with one or more package names to visualize. You can use the fully qualified import path of the package, like so:
2424

2525
```sh
2626
$ depth github.com/KyleBanks/depth/cmd/depth
@@ -40,6 +40,14 @@ github.com/KyleBanks/depth/cmd/depth
4040
└ strings
4141
```
4242

43+
Or you can use a relative path, for example:
44+
45+
```sh
46+
$ depth .
47+
$ depth ./cmd/depth
48+
$ depth ../
49+
```
50+
4351
You can also use `depth` on the Go standard library:
4452

4553
```sh
@@ -54,7 +62,7 @@ strings
5462
Visualizing multiple packages at a time is supported by simply naming the packages you'd like to visualize:
5563

5664
```sh
57-
$ depth strings github.com/KyleBanks/depth
65+
$ depth strings github.com/KyleBanks/depth
5866
strings
5967
├ errors
6068
├ io
@@ -178,10 +186,25 @@ if err != nil {
178186
log.Printf("'%v' has %v dependencies.", t.Root.Name, len(t.Root.Deps))
179187
```
180188

189+
For additional customization, simply set the appropriate flags on the `Tree` before resolving:
190+
191+
```go
192+
import "github.com/KyleBanks/depth"
193+
194+
t := depth.Tree {
195+
ResolveInternal: true,
196+
ResolveTest: true,
197+
MaxDepth: 10,
198+
}
199+
200+
201+
err := t.Resolve("strings")
202+
```
203+
181204
## Author
182205

183206
`depth` was developed by [Kyle Banks](https://twitter.com/kylewbanks).
184207

185208
## License
186209

187-
`depth` is available under [MIT](./LICENSE)
210+
`depth` is available under the [MIT](./LICENSE) license.

depth.go

Lines changed: 31 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,34 @@
33
//
44
// For example, the dependencies of the stdlib `strings` package can be resolved like so:
55
//
6-
// ```go
7-
// import "github.com/KyleBanks/depth"
6+
// import "github.com/KyleBanks/depth"
87
//
9-
// var t depth.Tree
10-
// err := t.Resolve("strings")
11-
// if err != nil {
12-
// log.Fatal(err)
13-
// }
8+
// var t depth.Tree
9+
// err := t.Resolve("strings")
10+
// if err != nil {
11+
// log.Fatal(err)
12+
// }
1413
//
15-
// // Output: "strings has 4 dependencies."
16-
// log.Printf("%v has %v dependencies.", t.Root.Name, len(t.Root.Deps))
17-
// ```
14+
// // Output: "strings has 4 dependencies."
15+
// log.Printf("%v has %v dependencies.", t.Root.Name, len(t.Root.Deps))
16+
//
17+
// For additional customization, simply set the appropriate flags on the `Tree` before resolving:
18+
//
19+
// import "github.com/KyleBanks/depth"
20+
//
21+
// t := depth.Tree {
22+
// ResolveInternal: true,
23+
// ResolveTest: true,
24+
// MaxDepth: 10,
25+
// }
26+
//
27+
// err := t.Resolve("strings")
1828
package depth
1929

2030
import (
2131
"errors"
2232
"go/build"
33+
"os"
2334
)
2435

2536
// ErrRootPkgNotResolved is returned when the root Pkg of the Tree cannot be resolved,
@@ -48,7 +59,16 @@ type Tree struct {
4859
// Resolve recursively finds all dependencies for the root Pkg name provided,
4960
// and the packages it depends on.
5061
func (t *Tree) Resolve(name string) error {
51-
t.Root = &Pkg{Name: name, Tree: t}
62+
pwd, err := os.Getwd()
63+
if err != nil {
64+
return err
65+
}
66+
67+
t.Root = &Pkg{
68+
Name: name,
69+
Tree: t,
70+
SrcDir: pwd,
71+
}
5272

5373
// Reset the import cache each time to ensure a reused Tree doesn't
5474
// reuse the same cache.

depth_test.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,13 @@ func TestTree_Resolve(t *testing.T) {
2424
if err := tr.Resolve("strings"); err != nil {
2525
t.Fatal(err)
2626
}
27+
2728
if tr.Root == nil || tr.Root.Name != "strings" {
2829
t.Fatalf("Unexpected Root, expected=%v, got=%v", "strings", tr.Root)
2930
} else if len(tr.Root.Deps) == 0 {
3031
t.Fatal("Expected positive number of Deps")
32+
} else if len(tr.Root.SrcDir) == 0 {
33+
t.Fatal("Expected SrcDir to be populated")
3134
}
3235

3336
// Reuse the same tree and the same package to ensure that the internal pkg cache

pkg.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,9 @@ func (p *Pkg) Resolve(i Importer) {
4545
return
4646
}
4747

48+
// Update the name with the fully qualified import path.
49+
p.Name = pkg.ImportPath
50+
4851
// If this is an internal dependency, we may need to skip it.
4952
if pkg.Goroot {
5053
p.Internal = true

0 commit comments

Comments
 (0)