forked from govend/govend
/
scan.go
128 lines (105 loc) · 2.54 KB
/
scan.go
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
118
119
120
121
122
123
124
125
126
127
128
// Copyright 2016 govend. All rights reserved.
// Use of this source code is governed by an Apache 2.0
// license that can be found in the LICENSE file.
package imports
import (
"os"
"sort"
"strings"
"github.com/govend/govend/imports/filters"
"github.com/kr/fs"
)
// ScanOptions represents available scan options.
type ScanOptions int
const (
// SinglePackage only scans a single package
SinglePackage ScanOptions = iota
// SkipTestFiles does not scan files that end in "_test.go".
SkipTestFiles
// SkipFilters returns the raw unfiltered list of scanned packages.
SkipFilters
)
// Scan takes a directory and scans it for import dependencies.
func Scan(path string, options ...ScanOptions) ([]string, error) {
// if the path is a Godeps path, filter it out
path = filters.Godeps([]string{path})[0]
// parse scan options
var singlePackage, skipTestFiles, skipFilters bool
for _, option := range options {
switch option {
case SinglePackage:
singlePackage = true
case SkipTestFiles:
skipTestFiles = true
case SkipFilters:
skipFilters = true
}
}
// get directory info
dinfo, err := os.Stat(path)
if err != nil {
return nil, err
}
// if we are in a directory step to it!
w := fs.Walk(path)
if dinfo.IsDir() {
w.Step()
}
// we will parse a list of packages
pkgs := []string{}
for w.Step() {
// skip all files and directories that start with '.' or '_'
finfo := w.Stat()
// check for any walker errors
if w.Err() != nil {
return nil, w.Err()
}
firstchar := []rune(finfo.Name())[0]
if firstchar == '_' || firstchar == '.' {
if finfo.IsDir() {
w.SkipDir()
continue
} else {
continue
}
}
// skip directories named "vendor"
if finfo.IsDir() {
if finfo.Name() == "vendor" || finfo.Name() == "Godeps" {
w.SkipDir()
continue
}
if singlePackage {
w.SkipDir()
continue
}
}
// if testfiles is false then skip all go tests deps
if skipTestFiles && strings.HasSuffix(finfo.Name(), "_test.go") {
continue
}
// only parse .go files
fpath := w.Path()
if strings.HasSuffix(fpath, ".go") {
p, err := Parse(w.Path())
if err != nil {
// if the error is because of a bad file, skip the file
if strings.Contains(err.Error(), eofError) {
continue
}
}
pkgs = append(pkgs, p...)
}
}
// filter packages
if !skipFilters {
pkgs = filters.Exceptions(pkgs)
pkgs = filters.Standard(pkgs)
pkgs = filters.Local(pkgs)
pkgs = filters.Godeps(pkgs)
}
pkgs = filters.Duplicates(pkgs)
// sort
sort.Strings(pkgs)
return pkgs, nil
}