forked from sourcegraph/srclib-go
/
config.go
206 lines (173 loc) · 5.66 KB
/
config.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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
package main
import (
"encoding/json"
"fmt"
"go/build"
"os"
"path/filepath"
"strings"
"golang.org/x/tools/go/loader"
"golang.org/x/tools/go/types"
)
var (
buildContext = build.Default
loaderConfig = loader.Config{
TypeChecker: types.Config{FakeImportC: true},
Build: &buildContext,
AllowErrors: true,
}
config *srcfileConfig
goBinaryName string
validVersions = []string{"", "1.3", "1.2", "1.1", "1"}
// effectiveConfigGOPATHs is a list of GOPATH dirs that were
// created as a result of the GOPATH config property. These are
// the dirs that are appended to the actual build context GOPATH.
effectiveConfigGOPATHs []string
)
// isInEffectiveConfigGOPATH is true if dir is underneath any of the
// dirs in effectiveConfigGOPATHs.
func isInEffectiveConfigGOPATH(dir string) bool {
for _, gopath := range effectiveConfigGOPATHs {
if pathHasPrefix(dir, gopath) {
return true
}
}
return false
}
type srcfileConfig struct {
// GOROOT, if specified, is made absolute (prefixed with the
// directory that the repository being built is checked out to)
// and is set as the GOROOT environment variable.
GOROOT string
// GOROOTForCmd, if set, is used as the GOROOT env var when
// invoking the "go" tool.
GOROOTForCmd string
// GOPATH's colon-separated dirs, if specified, are made absolute
// (prefixed with the directory that the repository being built is
// checked out to) and the resulting value is appended to the
// GOPATH environment variable during the build.
GOPATH string
// GOVERSION is the version of the go tool that srclib-go
// should shell out to. If GOVERSION is empty, the system's
// default go binary is used. The only valid values for
// GOVERSION are the empty string, "1.3", "1.2", "1.1", and
// "1", which are transformed into "go", "go1.3", ..., "go1",
// respectively, when the binary is called.
GOVERSION string
PkgPatterns []string // pattern passed to `go list` (defaults to {"./..."})
// SkipGodeps makes srclib-go skip the Godeps/_workspace directory when
// scanning for packages. This causes references to those packages to point
// to the files in their respective repositories instead of the local copies
// inside of Godeps/_workspace.
SkipGodeps bool
// ImportPathRoot is a prefix which is used when converting from repository
// URI to Go import path.
ImportPathRoot string
// VendorDirs are a list of all src directories used by a package,
// relative to the repository root. These are used for
// GO15VENDOREXPERIMENT related vendor support, and this is usually
// not set by the user. At build time a GOPATH is created which will
// include each VendorDir on it linked to src. VendorDirs should be
// sorted by longest path to shortest (ie most specific to least
// specific)
VendorDirs []string
}
// unmarshalTypedConfig parses config from the Config field of the source unit.
// It stores it in the config global variable.
//
// Callers should typically call config.apply() after calling
// unmarshalTypedConfig to actually apply the config.
func unmarshalTypedConfig(cfg map[string]interface{}) error {
data, err := json.Marshal(cfg)
if err != nil {
return err
}
if err := json.Unmarshal(data, &config); err != nil {
return err
}
if config == nil {
config = &srcfileConfig{}
}
return config.apply()
}
// apply applies the configuration.
func (c *srcfileConfig) apply() error {
var versionValid bool
for _, v := range validVersions {
if config.GOVERSION == v {
versionValid = true
goBinaryName = fmt.Sprintf("go%s", config.GOVERSION)
if config.GOVERSION != "" && config.GOROOT == "" {
// If GOROOT is empty, assign $GOROOT<version_num> to it.
newGOROOT := os.Getenv(fmt.Sprintf("GOROOT%s", strings.Replace(config.GOVERSION, ".", "", -1)))
if newGOROOT != "" {
config.GOROOTForCmd = newGOROOT
}
}
break
}
}
if !versionValid {
return fmt.Errorf("The version %s is not valid. Use one of the following: %v", config.GOVERSION, validVersions)
}
if config.GOROOT != "" {
// clean/absolutize all paths
config.GOROOT = filepath.Clean(config.GOROOT)
if !filepath.IsAbs(config.GOROOT) {
config.GOROOT = filepath.Join(cwd, config.GOROOT)
}
buildContext.GOROOT = c.GOROOT
loaderConfig.Build = &buildContext
}
if config.GOPATH != "" {
// clean/absolutize all paths
dirs := cleanDirs(filepath.SplitList(config.GOPATH))
config.GOPATH = strings.Join(dirs, string(filepath.ListSeparator))
dirs = append(dirs, filepath.SplitList(buildContext.GOPATH)...)
buildContext.GOPATH = strings.Join(uniq(dirs), string(filepath.ListSeparator))
loaderConfig.Build = &buildContext
}
config.VendorDirs = cleanDirs(config.VendorDirs)
if config.GOROOTForCmd == "" {
config.GOROOTForCmd = buildContext.GOROOT
}
return nil
}
func (c *srcfileConfig) env() []string {
return []string{
"PATH=" + os.Getenv("PATH"),
"GOARCH=" + buildContext.GOARCH,
"GOOS=" + buildContext.GOOS,
"GOROOT=" + config.GOROOTForCmd,
"GOPATH=" + buildContext.GOPATH,
"GO15VENDOREXPERIMENT=1",
}
}
func pathHasPrefix(path, prefix string) bool {
return prefix == "." || path == prefix || strings.HasPrefix(path, prefix+string(filepath.Separator))
}
// cleanDirs takes a list of paths cleans/abs them + removes duplicates
func cleanDirs(dirs []string) []string {
dirs = uniq(dirs)
for i, dir := range dirs {
dir = filepath.Clean(dir)
if !filepath.IsAbs(dir) {
dir = filepath.Join(cwd, dir)
}
dirs[i] = dir
}
return dirs
}
// uniq maintains the order of s.
func uniq(s []string) []string {
seen := make(map[string]struct{}, len(s))
var uniq []string
for _, s := range s {
if _, seen := seen[s]; seen {
continue
}
seen[s] = struct{}{}
uniq = append(uniq, s)
}
return uniq
}