forked from burik666/gobin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
buildinfo.go
97 lines (75 loc) · 1.67 KB
/
buildinfo.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
package mod
import (
"bytes"
"fmt"
"os"
"os/exec"
"path"
"path/filepath"
"strings"
"github.com/burik666/gobin/internal/config"
"github.com/burik666/gobin/internal/pkg/mod/buildinfo"
)
type BuildInfo struct {
buildinfo.BuildInfo
}
func (bi *BuildInfo) Matched(filters []string) bool {
if len(filters) > 0 {
for i := range filters {
name, ver := SplitVer(filters[i])
if ((bi.Path == "" && name == path.Base(bi.Filename)) ||
name == filepath.Base(bi.Path) ||
name == bi.Path ||
name == bi.Main.Path) &&
(ver == "" || ver == "latest" || ver == bi.Main.Version) {
return true
}
if strings.HasSuffix(name, "...") && strings.HasPrefix(bi.Path, strings.TrimSuffix(name, "...")) {
return true
}
}
}
return false
}
var buildInfoCache []BuildInfo
func getBuildInfo(path string) ([]BuildInfo, error) {
if buildInfoCache != nil {
return buildInfoCache, nil
}
cmd := exec.Command(config.Go, "version", "-m", path)
if config.Trace {
fmt.Fprintf(os.Stderr, "> %s\n", cmd.String())
}
var errb bytes.Buffer
cmd.Stderr = &errb
data, err := cmd.Output()
if err != nil {
return nil, fmt.Errorf("%s", errb.String())
}
res := make([]BuildInfo, 0)
for len(data) > 0 {
var bi BuildInfo
if err := bi.UnmarshalText(data); err != nil {
return nil, err
}
res = append(res, bi)
for len(data) > 0 {
n := bytes.IndexByte(data, '\n')
if n < 0 {
break
}
data = data[n+1:]
if len(data) > 0 && data[0] != '\t' {
break
}
}
}
buildInfoCache = res
return res, nil
}
func Uninstall(bi *BuildInfo) error {
if config.Trace {
fmt.Fprintf(os.Stderr, "rm %s\n", bi.Filename)
}
return os.Remove(bi.Filename)
}