-
Notifications
You must be signed in to change notification settings - Fork 149
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #165 from motemen/support-gomod
Support Go modules
- Loading branch information
Showing
12 changed files
with
498 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,12 @@ | ||
module github.com/motemen/gore | ||
|
||
go 1.12 | ||
go 1.13 | ||
|
||
require ( | ||
github.com/mattn/go-runewidth v0.0.7 // indirect | ||
github.com/motemen/go-quickfix v0.0.0-20160413151302-5c522febc679 | ||
github.com/motemen/go-quickfix v0.0.0-20200103095207-27e35cdee537 | ||
github.com/peterh/liner v1.1.0 | ||
github.com/stretchr/testify v1.3.0 | ||
golang.org/x/text v0.3.2 | ||
golang.org/x/tools v0.0.0-20191212051200-825cb0626375 | ||
golang.org/x/tools v0.0.0-20191230220329-2aa90c603ae3 | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
package gore | ||
|
||
import ( | ||
"bytes" | ||
"encoding/json" | ||
"go/build" | ||
"io" | ||
"io/ioutil" | ||
"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 := listModuleDirectives() | ||
mod := "module " + tempModule + "\n" + strings.Join(directives, "\n") | ||
return ioutil.WriteFile(goModPath, []byte(mod), 0644) | ||
} | ||
|
||
func 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)) | ||
} | ||
} | ||
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 url, err := url.Parse(getGoproxy()); err != nil { | ||
host = "proxy.golang.org" | ||
} else { | ||
host = url.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/" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.