-
-
Notifications
You must be signed in to change notification settings - Fork 19
/
update.go
205 lines (177 loc) · 4.88 KB
/
update.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
package cmd
import (
"fmt"
"os"
"os/signal"
"strconv"
"syscall"
"time"
"github.com/nao1215/gup/internal/goutil"
"github.com/nao1215/gup/internal/notify"
"github.com/nao1215/gup/internal/print"
"github.com/nao1215/gup/internal/slice"
"github.com/spf13/cobra"
)
var updateCmd = &cobra.Command{
Use: "update",
Short: "Update binaries installed by 'go install'",
Long: `Update binaries installed by 'go install'
If you execute '$ gup update', gup gets the package path of all commands
under $GOPATH/bin and automatically updates commands to the latest version.`,
Run: func(cmd *cobra.Command, args []string) {
OsExit(gup(cmd, args))
},
}
func init() {
updateCmd.Flags().BoolP("dry-run", "n", false, "perform the trial update with no changes")
updateCmd.Flags().BoolP("notify", "N", false, "enable desktop notifications")
rootCmd.AddCommand(updateCmd)
}
// gup is main sequence.
// All errors are handled in this function.
func gup(cmd *cobra.Command, args []string) int {
dryRun, err := cmd.Flags().GetBool("dry-run")
if err != nil {
print.Err(fmt.Errorf("%s: %w", "can not parse command line argument (--dry-run)", err))
return 1
}
notify, err := cmd.Flags().GetBool("notify")
if err != nil {
print.Err(fmt.Errorf("%s: %w", "can not parse command line argument (--notify)", err))
return 1
}
if err := goutil.CanUseGoCmd(); err != nil {
print.Err(fmt.Errorf("%s: %w", "you didn't install golang", err))
return 1
}
pkgs, err := getPackageInfo()
if err != nil {
print.Err(err)
return 1
}
pkgs = extractUserSpecifyPkg(pkgs, args)
if len(pkgs) == 0 {
print.Err("unable to update package: no package information")
return 1
}
return update(pkgs, dryRun, notify)
}
type updateResult struct {
pkg goutil.Package
err error
}
func update(pkgs []goutil.Package, dryRun, notification bool) int {
result := 0
countFmt := "[%" + pkgDigit(pkgs) + "d/%" + pkgDigit(pkgs) + "d]"
dryRunManager := goutil.NewGoPaths()
print.Info("update binary under $GOPATH/bin or $GOBIN")
signals := make(chan os.Signal, 1)
if dryRun {
if err := dryRunManager.StartDryRunMode(); err != nil {
print.Err(fmt.Errorf("can not change to dry run mode: %w", err))
notify.Warn("gup", "Can not change to dry run mode")
return 1
}
signal.Notify(signals, syscall.SIGINT, syscall.SIGTERM, syscall.SIGHUP,
syscall.SIGQUIT, syscall.SIGABRT)
go catchSignal(signals, dryRunManager)
}
ch := make(chan updateResult)
updater := func(p goutil.Package, result chan updateResult) {
var err error
if p.ImportPath == "" {
err = fmt.Errorf(" %s is not installed by 'go install' (or permission incorrect)", p.Name)
} else {
if err = goutil.Install(p.ImportPath); err != nil {
err = fmt.Errorf(" %s %w", p.Name, err)
}
}
p.SetLatestVer()
r := updateResult{
pkg: p,
err: err,
}
result <- r
}
// update all package
for _, v := range pkgs {
go updater(v, ch)
}
// print result
for i := 0; i < len(pkgs); i++ {
v := <-ch
if v.err == nil {
print.Info(fmt.Sprintf(countFmt+" %s (%s)",
i+1, len(pkgs), v.pkg.ImportPath, v.pkg.CurrentToLatestStr()))
} else {
result = 1
print.Err(fmt.Errorf(countFmt+"%s", i+1, len(pkgs), v.err.Error()))
}
}
if dryRun {
if err := dryRunManager.EndDryRunMode(); err != nil {
print.Err(fmt.Errorf("can not change dry run mode to normal mode: %w", err))
return 1
}
close(signals)
}
desktopNotifyIfNeeded(result, notification)
return result
}
func desktopNotifyIfNeeded(result int, enable bool) {
if enable {
if result == 0 {
notify.Info("gup", "All update success")
} else {
notify.Warn("gup", "Some package can't update")
}
}
}
func catchSignal(c chan os.Signal, dryRunManager *goutil.GoPaths) {
for {
select {
case <-c:
if err := dryRunManager.EndDryRunMode(); err != nil {
print.Err(fmt.Errorf("can not change dry run mode to normal mode: %w", err))
}
return
default:
time.Sleep(1 * time.Second)
}
}
}
func pkgDigit(pkgs []goutil.Package) string {
return strconv.Itoa(len(strconv.Itoa(len(pkgs))))
}
func getPackageInfo() ([]goutil.Package, error) {
goBin, err := goutil.GoBin()
if err != nil {
return nil, fmt.Errorf("%s: %w", "can't find installed binaries", err)
}
binList, err := goutil.BinaryPathList(goBin)
if err != nil {
return nil, fmt.Errorf("%s: %w", "can't get binary-paths installed by 'go install'", err)
}
return goutil.GetPackageInformation(binList), nil
}
func extractUserSpecifyPkg(pkgs []goutil.Package, targets []string) []goutil.Package {
result := []goutil.Package{}
tmp := []string{}
if len(targets) == 0 {
return pkgs
}
for _, v := range pkgs {
if slice.Contains(targets, v.Name) {
result = append(result, v)
tmp = append(tmp, v.Name)
}
}
if len(tmp) != len(targets) {
for _, target := range targets {
if !slice.Contains(tmp, target) {
print.Warn("not found '" + target + "' package in $GOPATH/bin or $GOBIN")
}
}
}
return result
}