forked from dave/courtney
-
Notifications
You must be signed in to change notification settings - Fork 0
/
shared.go
96 lines (90 loc) · 2 KB
/
shared.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
package shared
import (
"os"
"strings"
"path/filepath"
"github.com/dave/patsy"
"github.com/dave/patsy/vos"
)
// Setup holds globals, environment and command line flags for the courtney
// command
type Setup struct {
Env vos.Env
Paths *patsy.Cache
Enforce bool
Verbose bool
Short bool
Timeout string
Load string
Output string
TestArgs []string
Packages []PackageSpec
}
// PackageSpec identifies a package by dir and path
type PackageSpec struct {
Dir string
Path string
}
// Parse parses a slice of strings into the Packages slice
func (s *Setup) Parse(args []string) error {
if len(args) == 0 {
args = []string{"./..."}
}
packages := map[string]string{}
for _, ppath := range args {
var dir string
recursive := false
if strings.HasSuffix(ppath, "/...") {
ppath = strings.TrimSuffix(ppath, "/...")
recursive = true
}
if strings.HasSuffix(ppath, "/") {
ppath = strings.TrimSuffix(ppath, "/")
}
if ppath == "." {
var err error
dir, err = s.Env.Getwd()
if err != nil {
return err
}
ppath, err = s.Paths.Path(dir)
if err != nil {
return err
}
} else {
var err error
dir, err = s.Paths.Dir(ppath)
if err != nil {
return err
}
}
if !recursive {
packages[ppath] = dir
} else {
dirs := map[string]bool{}
filepath.Walk(dir, func(fpath string, info os.FileInfo, err error) error {
if !info.IsDir() && strings.HasSuffix(info.Name(), ".go") {
// Scan until we find a Go source file. Record the dir and
// skip the rest of the dir
fdir, _ := filepath.Split(fpath)
// don't want the dir to end with "/"
fdir = strings.TrimSuffix(fdir, string(filepath.Separator))
dirs[fdir] = true
return nil
}
return nil
})
for dir := range dirs {
ppath, err := s.Paths.Path(dir)
if err != nil {
return err
}
packages[ppath] = dir
}
}
}
for ppath, dir := range packages {
s.Packages = append(s.Packages, PackageSpec{Path: ppath, Dir: dir})
}
return nil
}