-
Notifications
You must be signed in to change notification settings - Fork 10
/
mod.go
61 lines (54 loc) · 1.38 KB
/
mod.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
package base
import (
"bufio"
"bytes"
"io/ioutil"
"os/exec"
"path/filepath"
"strings"
"golang.org/x/mod/modfile"
)
// ModulePath returns go module path.
func ModulePath(filename string) (string, error) {
modBytes, err := ioutil.ReadFile(filename)
if err != nil {
return "", err
}
return modfile.ModulePath(modBytes), nil
}
// ModuleVersion returns module version.
func ModuleVersion(path string) (string, error) {
stdout := &bytes.Buffer{}
fd := exec.Command("go", "mod", "graph")
fd.Stdout = stdout
fd.Stderr = stdout
if err := fd.Run(); err != nil {
return "", err
}
rd := bufio.NewReader(stdout)
for {
line, _, err := rd.ReadLine()
if err != nil {
return "", err
}
str := string(line)
i := strings.Index(str, "@")
if strings.Contains(str, path+"@") && i != -1 {
return path + str[i:], nil
}
}
}
// TemplateMod returns template mod.
func TemplateMod() string {
// go 1.15+ read from env GOMODCACHE
cacheOut, _ := exec.Command("go", "env", "GOMODCACHE").Output()
cachePath := strings.Trim(string(cacheOut), "\n")
pathOut, _ := exec.Command("go", "env", "GOPATH").Output()
gopath := strings.Trim(string(pathOut), "\n")
if cachePath == "" {
cachePath = filepath.Join(gopath, "pkg", "mod")
}
//TODO support GOMODCACHE
// $GOPATH/src/github.com/tkeel-io/tkeel-template-go
return filepath.Join(gopath, "src", "github.com", "tkeel-io", "tkeel-template-go")
}