-
Notifications
You must be signed in to change notification settings - Fork 148
/
gomod.go
124 lines (116 loc) · 2.83 KB
/
gomod.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
package gore
import (
"bytes"
"encoding/json"
"go/build"
"io"
"net"
"net/url"
"os"
"os/exec"
"path/filepath"
"strconv"
"strings"
"time"
)
func (s *Session) initGoMod() error {
tempModule := filepath.Base(s.tempDir)
goModPath := filepath.Join(s.tempDir, "go.mod")
directives := s.listModuleDirectives()
mod := "module " + tempModule + "\n" + strings.Join(directives, "\n")
return os.WriteFile(goModPath, []byte(mod), 0o644)
}
func (s *Session) listModuleDirectives() []string {
var directives []string
for i, pp := range printerPkgs {
if pp.path == "fmt" {
continue
}
// Check local module caches.
found := lookupGoModule(pp.path, pp.version)
if found {
for _, r := range pp.requires {
if !lookupGoModule(r.path, r.version) {
found = false
break
}
}
}
if found || canAccessGoproxy() {
// Specifying the version of the printer package improves startup
// performance by skipping module version fetching. Also allows to
// use gore in offline environment.
directives = append(directives, "require "+pp.path+" "+pp.version)
for _, r := range pp.requires {
directives = append(directives, "require "+r.path+" "+r.version)
}
} else {
// If there is no module cache and no network connection, use fmt package.
printerPkgs = printerPkgs[i+1:]
}
// only the first printer is checked (assuming printerPkgs[1] is fmt)
break
}
modules, err := goListAll()
if err != nil {
return directives
}
for _, m := range modules {
if m.Main || m.Replace != nil {
directives = append(directives, "replace "+m.Path+" => "+strconv.Quote(m.Dir))
s.requiredModules = append(s.requiredModules, m.Path)
}
}
return directives
}
type goModule struct {
Path, Dir, Version string
Main bool
Replace *goModule
}
func goListAll() ([]*goModule, error) {
cmd := exec.Command("go", "list", "-json", "-m", "all")
out, err := cmd.Output()
if err != nil {
return nil, err
}
d := json.NewDecoder(bytes.NewReader(out))
var ms []*goModule
for {
m := new(goModule)
if err := d.Decode(m); err != nil {
if err == io.EOF {
return ms, nil
}
return nil, err
}
ms = append(ms, m)
}
}
func lookupGoModule(pkg, version string) bool {
modDir := filepath.Join(build.Default.GOPATH, "pkg/mod", pkg+"@"+version)
fi, err := os.Stat(modDir)
return err == nil && fi.IsDir()
}
func canAccessGoproxy() bool {
var host string
if u, err := url.Parse(getGoproxy()); err != nil {
host = "proxy.golang.org"
} else {
host = u.Hostname()
}
addr := net.JoinHostPort(host, "80")
dialer := net.Dialer{Timeout: 5 * time.Second}
conn, err := dialer.Dial("tcp", addr)
if err != nil {
return false
}
defer conn.Close()
return true
}
func getGoproxy() string {
if goproxy := os.Getenv("GOPROXY"); goproxy != "" {
return goproxy
}
return "https://proxy.golang.org/"
}