File: main.go

package info (click to toggle)
golang-github-kisom-goutils 0.0~git20161101.0.858c9cb-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 384 kB
  • ctags: 331
  • sloc: makefile: 6
file content (117 lines) | stat: -rw-r--r-- 2,380 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
// 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
	debug   bool
)

var (
	stdLibRegexp = regexp.MustCompile(`^\w+(/\w+)*$`)
	sourceRegexp = regexp.MustCompile(`^[^.].*\.go$`)
	log          = logging.NewConsole()
	imports      = map[string]bool{}
	fset         = &token.FileSet{}
)

func debugf(format string, args ...interface{}) {
	if debug {
		fmt.Printf(format, args...)
	}
}

func debugln(args ...interface{}) {
	if debug {
		fmt.Println(args...)
	}
}

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 {
	if !sourceRegexp.MatchString(path) {
		return nil
	}

	debugln(path)

	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) {
			debugln("standard lib:", importPath)
			continue
		} else if strings.HasPrefix(importPath, project) {
			debugln("internal import:", importPath)
			continue
		} else if strings.HasPrefix(importPath, "golang.org/") {
			debugln("extended lib:", importPath)
			continue
		}
		debugln("import:", importPath)
		imports[importPath] = true
	}

	return nil
}

func main() {
	flag.BoolVar(&debug, "v", false, "log debugging information")
	flag.Parse()

	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)
	}
}