This repository has been archived by the owner on Oct 3, 2022. It is now read-only.
forked from nao1215/gup
-
Notifications
You must be signed in to change notification settings - Fork 0
/
goutil.go
290 lines (247 loc) · 6.67 KB
/
goutil.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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
package goutil
import (
"errors"
"fmt"
"go/build"
"os"
"os/exec"
"path/filepath"
"regexp"
"strings"
"github.com/adrg/xdg"
"github.com/fatih/color"
"github.com/Akimon658/gup/config"
"github.com/Akimon658/gup/internal/print"
)
type GoPaths struct {
GOBIN string
GOPATH string
// TmpPath is tmporary path for dry run
TmpPath string
}
type Package struct {
Name string
ImportPath string
// ModulePath is path where go.mod is stored
ModulePath string
Version *Version
BuildFlags *config.BuildFlags
}
type Version struct {
Current string
Latest string
}
func NewVersion() *Version {
return &Version{
Current: "",
Latest: "",
}
}
func (p *Package) SetCurrentVer() {
p.Version.Current = GetPackageVersion(p.Name)
}
func (p *Package) SetLatestVer() {
p.Version.Latest = GetPackageVersion(p.Name)
}
// CurrentToLatestStr returns string about the current version and the latest version
func (p *Package) CurrentToLatestStr() string {
if IsAlreadyUpToDate(*p.Version) {
return "Already up-to-date: " + color.GreenString(p.Version.Latest)
}
return color.GreenString(p.Version.Current) + " to " + color.YellowString(p.Version.Latest)
}
// VersionCheckResultStr returns string about command version check.
func (p *Package) VersionCheckResultStr() string {
if IsAlreadyUpToDate(*p.Version) {
return "Already up-to-date: " + color.GreenString(p.Version.Latest)
}
return "current: " + color.GreenString(p.Version.Current) + ", latest: " + color.YellowString(p.Version.Latest)
}
func IsAlreadyUpToDate(ver Version) bool {
return ver.Current == ver.Latest
}
func NewGoPaths() *GoPaths {
return &GoPaths{
GOBIN: goBin(),
GOPATH: goPath(),
}
}
// StartDryRunMode changes the GOBIN or GOPATH settings to install the binaries in the temporary directory.
func (gp *GoPaths) StartDryRunMode() error {
tmpDir, err := os.MkdirTemp("", "")
if err != nil {
return err
}
if gp.GOBIN != "" {
if err := os.Setenv("GOBIN", tmpDir); err != nil {
return err
}
} else if gp.GOPATH != "" {
if err := os.Setenv("GOPATH", tmpDir); err != nil {
return err
}
} else {
return errors.New("$GOPATH and $GOBIN is not set")
}
return nil
}
func (gp *GoPaths) EndDryRunMode() error {
if gp.GOBIN != "" {
if err := os.Setenv("GOBIN", gp.GOBIN); err != nil {
return err
}
} else if gp.GOPATH != "" {
if err := os.Setenv("GOPATH", gp.GOPATH); err != nil {
return err
}
} else {
return errors.New("$GOPATH and $GOBIN is not set")
}
if err := gp.removeTmpDir(); err != nil {
return fmt.Errorf("%s: %w", "temporary directory for dry run remains", err)
}
return nil
}
func (gp *GoPaths) removeTmpDir() error {
if gp.TmpPath != "" {
if err := os.RemoveAll(gp.TmpPath); err != nil {
return err
}
}
return nil
}
func Install(importPath string, buildFlags *config.BuildFlags) error {
if importPath == "command-line-arguments" {
return errors.New("is devel-binary copied from local environment")
}
if err := exec.Command("go", "install", "-ldflags", buildFlags.Ldflags, "-tags", buildFlags.Tags, importPath+"@latest").Run(); err != nil {
return fmt.Errorf("can't install %s: %w", importPath, err)
}
return nil
}
// GetLatestVer executes "go list -m -f {{.Version}} <importPath>@latest"
func GetLatestVer(modulePath string) (string, error) {
out, err := exec.Command("go", "list", "-m", "-f", "{{.Version}}", modulePath+"@latest").Output()
if err != nil {
return "", errors.New("can't check " + modulePath)
}
return strings.TrimRight(string(out), "\n"), nil
}
func goPath() string {
gopath := os.Getenv("GOPATH")
if gopath != "" {
return gopath
}
return build.Default.GOPATH
}
func goBin() string {
return os.Getenv("GOBIN")
}
func GoBin() (string, error) {
goBin := goBin()
if goBin != "" {
return goBin, nil
}
goPath := goPath()
if goPath == "" {
return "", errors.New("$GOPATH is not set")
}
return filepath.Join(goPath, "bin"), nil
}
// GoVersionWithOptionM returns result of "go version -m"
func GoVersionWithOptionM(bin string) ([]string, error) {
out, err := exec.Command("go", "version", "-m", bin).Output()
if err != nil {
return nil, err
}
return strings.Split(string(out), "\n"), nil
}
func BinaryPathList(path string) ([]string, error) {
entries, err := os.ReadDir(path)
if err != nil {
return nil, err
}
list := []string{}
for _, e := range entries {
if !e.IsDir() {
list = append(list, filepath.Join(path, e.Name()))
}
}
return list, nil
}
func GetPackageInformation(binList []string) ([]Package, error) {
var pkgs []Package
conf, err := config.Read(filepath.Join(xdg.ConfigHome, "gup", "package.yml"))
if err != nil {
return nil, err
}
for _, v := range binList {
out, err := GoVersionWithOptionM(v)
if err != nil {
print.Warn(fmt.Errorf("%s: %w", "can not get package path", err))
continue
}
name := filepath.Base(v)
pkg := Package{
Name: name,
ImportPath: extractImportPath(out),
ModulePath: extractModulePath(out),
Version: NewVersion(),
BuildFlags: conf.GetFlags(name),
}
pkg.SetCurrentVer()
pkgs = append(pkgs, pkg)
}
return pkgs, nil
}
func GetPackageVersion(cmdName string) string {
goBin, err := GoBin()
if err != nil {
return "unknown"
}
out, err := GoVersionWithOptionM(filepath.Join(goBin, cmdName))
if err != nil {
return "unknown"
}
for _, v := range out {
vv := strings.TrimSpace(v)
if len(v) != len(vv) && strings.HasPrefix(vv, "mod") {
// mod github.com/nao1215/subaru v1.0.2 h1:LU9/1bFyqef3re6FVSFgTFMSXCZvrmDpmX3KQtlHzXA=
v = strings.TrimLeft(vv, "mod")
v = strings.TrimSpace(v)
//github.com/nao1215/subaru v1.0.2 h1:LU9/1bFyqef3re6FVSFgTFMSXCZvrmDpmX3KQtlHzXA=
r := regexp.MustCompile(`^[^\s]+(\s)`)
v = r.ReplaceAllString(v, "")
// v1.0.2 h1:LU9/1bFyqef3re6FVSFgTFMSXCZvrmDpmX3KQtlHzXA=
r = regexp.MustCompile(`(\s)[^\s]+$`)
// v1.0.2
return r.ReplaceAllString(v, "")
}
}
return "unknown"
}
func extractImportPath(lines []string) string {
for _, v := range lines {
vv := strings.TrimSpace(v)
if len(v) != len(vv) && strings.HasPrefix(vv, "path") {
vv = strings.TrimLeft(vv, "path")
vv = strings.TrimSpace(vv)
return strings.TrimRight(vv, "\n")
}
}
return ""
}
func extractModulePath(lines []string) string {
for _, v := range lines {
vv := strings.TrimSpace(v)
if len(v) != len(vv) && strings.HasPrefix(vv, "mod") {
// mod github.com/nao1215/subaru v1.0.2 h1:LU9/1bFyqef3re6FVSFgTFMSXCZvrmDpmX3KQtlHzXA=
v = strings.TrimLeft(vv, "mod")
v = strings.TrimSpace(v)
//github.com/nao1215/subaru v1.0.2 h1:LU9/1bFyqef3re6FVSFgTFMSXCZvrmDpmX3KQtlHzXA=
r := regexp.MustCompile(`(\s).*$`)
return r.ReplaceAllString(v, "")
}
}
return ""
}