forked from Masterminds/glide
/
novendor.go
134 lines (111 loc) · 2.66 KB
/
novendor.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
129
130
131
132
133
134
package cmd
import (
"fmt"
"os"
"path/filepath"
"strings"
"github.com/Masterminds/cookoo"
)
// NoVendor takes a path and returns all subpaths that are not vendor directories.
//
// It is not recursive.
//
// If the given path is a file, it returns that path unaltered.
//
// If the given path is a directory, it scans all of the immediate children,
// and returns all of the go files and directories that are not vendor.
func NoVendor(c cookoo.Context, p *cookoo.Params) (interface{}, cookoo.Interrupt) {
path := p.Get("path", ".").(string)
gonly := p.Get("onlyGo", true).(bool)
return noVend(path, gonly)
}
// Take a list of paths and print a single string with space-separated paths.
func PathString(c cookoo.Context, p *cookoo.Params) (interface{}, cookoo.Interrupt) {
paths := p.Get("paths", []string{}).([]string)
s := strings.Join(paths, " ")
fmt.Println(s)
return nil, nil
}
// noVend takes a directory and returns a list of Go-like files or directories,
// provided the directory is not a vendor directory.
//
// If onlyGo is true, this will filter out all directories that do not contain
// ".go" files.
func noVend(path string, onlyGo bool) ([]string, error) {
info, err := os.Stat(path)
if err != nil {
return []string{}, err
}
if !info.IsDir() {
return []string{path}, nil
}
res := []string{}
f, err := os.Open(path)
if err != nil {
return res, err
}
fis, err := f.Readdir(0)
if err != nil {
return res, err
}
cur := false
for _, fi := range fis {
if exclude(fi) {
continue
}
full := filepath.Join(path, fi.Name())
if fi.IsDir() && !isVend(fi) {
p := "./" + full + "/..."
res = append(res, p)
} else if !fi.IsDir() && isGoish(fi) {
//res = append(res, full)
cur = true
}
}
// Filter out directories that do not contain Go code
if onlyGo {
res = hasGoSource(res)
}
if cur {
res = append(res, ".")
}
return res, nil
}
func hasGoSource(dirs []string) []string {
buf := []string{}
for _, d := range dirs {
d := filepath.Dir(d)
found := false
walker := func(p string, fi os.FileInfo, err error) error {
// Dumb optimization
if found {
return nil
}
// If the file ends with .go, report a match.
if strings.ToLower(filepath.Ext(p)) == ".go" {
found = true
}
return nil
}
filepath.Walk(d, walker)
if found {
buf = append(buf, "./"+d+"/...")
}
}
return buf
}
func isVend(fi os.FileInfo) bool {
return fi.Name() == "vendor"
}
func exclude(fi os.FileInfo) bool {
if strings.HasPrefix(fi.Name(), "_") {
return true
}
if strings.HasPrefix(fi.Name(), ".") {
return true
}
return false
}
func isGoish(fi os.FileInfo) bool {
return filepath.Ext(fi.Name()) == ".go"
}