2015-09-22 18:25:54 +00:00
|
|
|
// showimp is a utility for displaying the imports in a package.
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"flag"
|
|
|
|
"fmt"
|
|
|
|
"go/parser"
|
|
|
|
"go/token"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"regexp"
|
|
|
|
"sort"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/kisom/goutils/die"
|
|
|
|
"github.com/kisom/goutils/logging"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
gopath string
|
|
|
|
project string
|
2016-04-28 18:29:58 +00:00
|
|
|
debug bool
|
2015-09-22 18:25:54 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
stdLibRegexp = regexp.MustCompile(`^\w+(/\w+)*$`)
|
|
|
|
sourceRegexp = regexp.MustCompile(`^[^.].*\.go$`)
|
2016-04-28 18:29:58 +00:00
|
|
|
log = logging.NewConsole()
|
|
|
|
imports = map[string]bool{}
|
|
|
|
fset = &token.FileSet{}
|
2015-09-22 18:25:54 +00:00
|
|
|
)
|
|
|
|
|
2016-04-28 18:29:58 +00:00
|
|
|
func debugf(format string, args ...interface{}) {
|
|
|
|
if debug {
|
|
|
|
fmt.Printf(format, args...)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func debugln(args ...interface{}) {
|
|
|
|
if debug {
|
|
|
|
fmt.Println(args...)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-22 18:25:54 +00:00
|
|
|
func init() {
|
|
|
|
gopath = os.Getenv("GOPATH")
|
|
|
|
if gopath == "" {
|
|
|
|
fmt.Fprintf(os.Stderr, "GOPATH isn't set, can't proceed.")
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
gopath += "/src/"
|
|
|
|
|
|
|
|
wd, err := os.Getwd()
|
|
|
|
if err != nil {
|
|
|
|
fmt.Fprintf(os.Stderr, "Unable to establish working directory: %v", err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !strings.HasPrefix(wd, gopath) {
|
|
|
|
fmt.Fprintf(os.Stderr, "Can't determine my location in the GOPATH.\n")
|
|
|
|
fmt.Fprintf(os.Stderr, "Working directory is %s\n", wd)
|
|
|
|
fmt.Fprintf(os.Stderr, "Go source path is %s\n", gopath)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
|
|
|
project = wd[len(gopath):]
|
|
|
|
}
|
|
|
|
|
|
|
|
func walkFile(path string, info os.FileInfo, err error) error {
|
2018-04-24 16:42:54 +00:00
|
|
|
if ignores[path] {
|
|
|
|
return filepath.SkipDir
|
|
|
|
}
|
|
|
|
|
2015-09-22 18:25:54 +00:00
|
|
|
if !sourceRegexp.MatchString(path) {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-04-28 18:29:58 +00:00
|
|
|
debugln(path)
|
2015-09-22 18:25:54 +00:00
|
|
|
|
|
|
|
f, err := parser.ParseFile(fset, path, nil, parser.ImportsOnly)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, importSpec := range f.Imports {
|
|
|
|
importPath := strings.Trim(importSpec.Path.Value, `"`)
|
|
|
|
if stdLibRegexp.MatchString(importPath) {
|
2016-04-28 18:29:58 +00:00
|
|
|
debugln("standard lib:", importPath)
|
2015-09-22 18:25:54 +00:00
|
|
|
continue
|
|
|
|
} else if strings.HasPrefix(importPath, project) {
|
2016-04-28 18:29:58 +00:00
|
|
|
debugln("internal import:", importPath)
|
2015-09-22 18:25:54 +00:00
|
|
|
continue
|
|
|
|
} else if strings.HasPrefix(importPath, "golang.org/") {
|
2016-04-28 18:29:58 +00:00
|
|
|
debugln("extended lib:", importPath)
|
2015-09-22 18:25:54 +00:00
|
|
|
continue
|
|
|
|
}
|
2016-04-28 18:29:58 +00:00
|
|
|
debugln("import:", importPath)
|
2015-09-22 18:25:54 +00:00
|
|
|
imports[importPath] = true
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-04-24 16:42:54 +00:00
|
|
|
var ignores = map[string]bool{}
|
|
|
|
|
2015-09-22 18:25:54 +00:00
|
|
|
func main() {
|
2018-04-24 16:42:54 +00:00
|
|
|
var ignoreLine string
|
|
|
|
var noVendor bool
|
|
|
|
flag.StringVar(&ignoreLine, "i", "", "comma-separated list of directories to ignore")
|
|
|
|
flag.BoolVar(&noVendor, "nv", false, "ignore the vendor directory")
|
2016-04-28 18:29:58 +00:00
|
|
|
flag.BoolVar(&debug, "v", false, "log debugging information")
|
2015-09-22 18:25:54 +00:00
|
|
|
flag.Parse()
|
|
|
|
|
2018-04-24 16:42:54 +00:00
|
|
|
if noVendor {
|
|
|
|
ignores["vendor"] = true
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, word := range strings.Split(ignoreLine, ",") {
|
|
|
|
ignores[strings.TrimSpace(word)] = true
|
|
|
|
}
|
|
|
|
|
2015-09-22 18:25:54 +00:00
|
|
|
err := filepath.Walk(".", walkFile)
|
|
|
|
die.If(err)
|
|
|
|
|
|
|
|
fmt.Println("External imports:")
|
|
|
|
importList := make([]string, 0, len(imports))
|
|
|
|
for imp := range imports {
|
|
|
|
importList = append(importList, imp)
|
|
|
|
}
|
|
|
|
sort.Strings(importList)
|
|
|
|
|
|
|
|
for _, imp := range importList {
|
|
|
|
fmt.Println("\t", imp)
|
|
|
|
}
|
|
|
|
}
|