/
icfg.go
78 lines (67 loc) · 1.88 KB
/
icfg.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
// Copyright 2017 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package load
import (
"bytes"
"encoding/json"
"errors"
"io/ioutil"
)
// DebugDeprecatedImportcfg is installed as the undocumented -debug-deprecated-importcfg build flag.
// It is useful for debugging subtle problems in the go command logic but not something
// we want users to depend on. The hope is that the "deprecated" will make that clear.
// We intend to remove this flag in Go 1.11.
var DebugDeprecatedImportcfg debugDeprecatedImportcfgFlag
type debugDeprecatedImportcfgFlag struct {
enabled bool
Import map[string]string
Pkg map[string]*debugDeprecatedImportcfgPkg
}
type debugDeprecatedImportcfgPkg struct {
Dir string
Import map[string]string
}
var (
debugDeprecatedImportcfgMagic = []byte("# debug-deprecated-importcfg\n")
errImportcfgSyntax = errors.New("malformed syntax")
)
func (f *debugDeprecatedImportcfgFlag) String() string { return "" }
func (f *debugDeprecatedImportcfgFlag) Set(x string) error {
if x == "" {
*f = debugDeprecatedImportcfgFlag{}
return nil
}
data, err := ioutil.ReadFile(x)
if err != nil {
return err
}
if !bytes.HasPrefix(data, debugDeprecatedImportcfgMagic) {
return errImportcfgSyntax
}
data = data[len(debugDeprecatedImportcfgMagic):]
f.Import = nil
f.Pkg = nil
if err := json.Unmarshal(data, &f); err != nil {
return errImportcfgSyntax
}
f.enabled = true
return nil
}
func (f *debugDeprecatedImportcfgFlag) lookup(parent *Package, path string) (dir, newPath string) {
newPath = path
if p := f.Import[path]; p != "" {
newPath = p
}
if parent != nil {
if p1 := f.Pkg[parent.ImportPath]; p1 != nil {
if p := p1.Import[path]; p != "" {
newPath = p
}
}
}
if p2 := f.Pkg[newPath]; p2 != nil {
return p2.Dir, newPath
}
return "", ""
}