depth

package module
v1.0.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 4, 2017 License: MIT Imports: 4 Imported by: 0

README

depth

GoDoc  Build Status  Go Report Card

depth is tool to retrieve and visualize Go source code dependency trees.

Install

go get github.com/KyleBanks/depth/cmd/depth

Usage

depth can be used as a standalone command-line application, or as a package within your own project.

Command-Line

Simply execute depth with one or more package names to visualize:

$ depth github.com/KyleBanks/depth/cmd/depth
github.com/KyleBanks/depth/cmd/depth
  ├ encoding/json
  ├ flag
  ├ fmt
  ├ io
  ├ log
  ├ os
  ├ strings
  └ github.com/KyleBanks/depth
    ├ fmt
    ├ go/build
    ├ path
    ├ sort
    └ strings

You can also use depth on the Go standard library:

$ depth strings
strings
  ├ errors
  ├ io
  ├ unicode
  └ unicode/utf8

Visualizing multiple packages at a time is supported by simply naming the packages you'd like to visualize:

$ depth strings github.com/KyleBanks/depth
strings
  ├ errors
  ├ io
  ├ unicode
  └ unicode/utf8
github.com/KyleBanks/depth
  ├ fmt
  ├ go/build
  ├ path
  ├ sort
  └ strings
-internal

By default, depth only resolves the top level of dependencies for standard library packages, however you can use the -internal flag to visualize all internal dependencies:

$ depth -internal strings
strings
  ├ errors
  ├ io
    ├ errors
    └ sync
      ├ internal/race
        └ unsafe
      ├ runtime
        ├ runtime/internal/atomic
          └ unsafe
        ├ runtime/internal/sys
        └ unsafe
      ├ sync/atomic
        └ unsafe
      └ unsafe
  ├ unicode
  └ unicode/utf8
-max

The -max flag limits the dependency tree to the maximum depth provided. For example, if you supply -max 1 on the depth package, your output would look like so:

$ depth -max 1 github.com/KyleBanks/depth/cmd/depth
github.com/KyleBanks/depth/cmd/depth
  ├ encoding/json
  ├ flag
  ├ fmt
  ├ io
  ├ log
  ├ os
  ├ strings
  └ github.com/KyleBanks/depth

The -max flag is particularly useful in conjunction with the -internal flag which can lead to very deep dependency trees.

-test

By default, depth ignores dependencies that are only required for testing. However, you can view test dependencies using the -test flag:

$ depth -test strings
strings
  ├ bytes
  ├ errors
  ├ fmt
  ├ io
  ├ io/ioutil
  ├ math/rand
  ├ reflect
  ├ sync
  ├ testing
  ├ unicode
  ├ unicode/utf8
  └ unsafe
-json

The -json flag instructs depth to output dependencies in JSON format:

$ depth -json github.com/KyleBanks/depth/cmd/depth
{
  "name": "github.com/KyleBanks/depth/cmd/depth",
  "deps": [
    {
      "name": "encoding/json",
      "deps": null
    },
    ...
    {
      "name": "github.com/KyleBanks/depth",
      "deps": [
        {
          "name": "go/build",
          "deps": null
        },
        ...
      ]
    }
  ]
}
Integrating With Your Project

The depth package can easily be used to retrieve the dependency tree for a particular package in your own project. For example, here's how you would retrieve the dependency tree for the strings package:

import "github.com/KyleBanks/depth"

var t depth.Tree
err := t.Resolve("strings")
if err != nil {
    log.Fatal(err)
}

// Output: "'strings' has 4 dependencies."
log.Printf("'%v' has %v dependencies.", t.Root.Name, len(t.Root.Deps)) 

Author

depth was developed by Kyle Banks.

License

depth is available under MIT

Documentation

Overview

Package depth provides the ability to traverse and retrieve Go source code dependencies in the form of internal and external packages.

For example, the dependencies of the stdlib `strings` package can be resolved like so:

```go import "github.com/KyleBanks/depth"

var t depth.Tree err := t.Resolve("strings")

if err != nil {
    log.Fatal(err)
}

// Output: "strings has 4 dependencies." log.Printf("%v has %v dependencies.", t.Root.Name, len(t.Root.Deps)) ```

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Importer

type Importer interface {
	Import(name, srcDir string, im build.ImportMode) (*build.Package, error)
}

Importer defines a type that can import a package and return its details.

type Pkg

type Pkg struct {
	Name     string `json:"name"`
	SrcDir   string `json:"-"`
	Internal bool   `json:"-"`

	Tree   *Tree `json:"-"`
	Parent *Pkg  `json:"-"`
	Deps   []Pkg `json:"deps"`
}

Pkg represents a Go source package, and its dependencies.

func (*Pkg) Resolve

func (p *Pkg) Resolve(i Importer, resolveImports bool) error

Resolve recursively finds all dependencies for the Pkg and the packages it depends on.

type Tree

type Tree struct {
	Root *Pkg

	ResolveInternal bool
	ResolveTest     bool
	MaxDepth        int

	Importer Importer
	// contains filtered or unexported fields
}

Tree represents the top level of a Pkg and the configuration used to initialize and represent its contents.

func (*Tree) Resolve

func (t *Tree) Resolve(name string) error

Resolve recursively finds all dependencies for the root Pkg name provided, and the packages it depends on.

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL