forked from burik666/gobin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
package.go
104 lines (79 loc) · 1.81 KB
/
package.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
package mod
import (
"strings"
"github.com/burik666/gobin/internal/config"
"golang.org/x/mod/module"
"golang.org/x/mod/semver"
)
type Pkg struct {
Name string
BuildInfo *BuildInfo
ModuleInfo *ModuleInfo
SelectedVersion *ModuleInfo
}
func (pkg *Pkg) FetchModuleInfo() error {
mi, err := getModuleInfo(pkg.BuildInfo.Main.Path, pkg.BuildInfo.Main.Version)
if err != nil {
return err
}
pkg.ModuleInfo = mi
if module.IsPseudoVersion(pkg.BuildInfo.Main.Version) {
up, err := getModuleInfo(mi.Path, "latest")
if err != nil {
return err
}
if semver.Compare(up.Version, mi.Version) > 0 {
mi.Update = up
}
}
return nil
}
func (pkg *Pkg) HasUpdate() bool {
if pkg.ModuleInfo == nil {
return false
}
if (pkg.ModuleInfo.Update != nil) || pkg.hasGoUpdate() {
return true
}
return false
}
func (pkg *Pkg) CheckPath() bool {
return module.CheckPath(pkg.BuildInfo.Path) == nil
}
func (pkg *Pkg) hasGoUpdate() bool {
return semver.Compare("v"+pkg.BuildInfo.GoVersion, "v"+config.GoVersion) < 0
}
func ListInstalled(filter []string, exclude []string) ([]Pkg, error) {
builds, err := getBuildInfo(config.GOBIN)
if err != nil {
return nil, err
}
res := make([]Pkg, 0, len(builds))
for i := range builds {
bi := builds[i]
if len(filter) > 0 && !bi.Matched(filter) ||
len(exclude) > 0 && bi.Matched(exclude) {
continue
}
res = append(res, Pkg{Name: bi.BuildInfo.Path, BuildInfo: &bi})
}
return res, nil
}
func FindPackage(name, path, ver string) (*Pkg, error) {
p := strings.Split(path, "/")
var ferr error
for {
mi, err := getModuleInfo(strings.Join(p, "/"), ver)
if err != nil {
if ferr == nil {
ferr = err
}
if len(p) <= 2 {
return nil, ferr
}
p = p[:len(p)-1]
continue
}
return &Pkg{Name: name, ModuleInfo: mi}, err
}
}