-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgomup.go
164 lines (136 loc) Β· 2.84 KB
/
gomup.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
package main
import (
"fmt"
"log"
"os"
"os/exec"
"path/filepath"
"strings"
"sync"
"time"
"github.com/briandowns/spinner"
"github.com/urfave/cli/v2"
)
const gomod = "go.mod"
var args = []string{
"list",
"-u",
"-mod=mod",
"-f",
"'{{if (and (not (or .Main .Indirect)) .Update)}}{{.Path}} {{.Version}} {{.Update.Version}}{{end}}'",
"-m",
"all",
}
func main() {
var (
path string
list bool
)
app := &cli.App{
Name: "gomUP",
Usage: "go module dependency upgrade tool",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "path",
Aliases: []string{"p"},
Usage: "directory path of go projects",
Required: true,
Destination: &path,
},
&cli.BoolFlag{
Name: "list",
Aliases: []string{"l"},
Usage: "list all dependencies",
Required: false,
Destination: &list,
},
},
Action: func(c *cli.Context) error {
err := checkPath(path)
if err != nil {
return err
}
s := spinner.New(spinner.CharSets[36], 250*time.Millisecond)
s.Prefix = "Starting gomUP "
s.Start()
dependencies := find(path)
s.Stop()
if len(dependencies) == 0 {
fmt.Println("everything up-to-date")
return nil
}
if list {
drawTable(dependencies)
return nil
}
startUI(dependencies)
return nil
},
UseShortOptionHandling: true,
EnableBashCompletion: true,
}
err := app.Run(os.Args)
if err != nil {
log.Fatal(err)
}
}
func checkPath(path string) error {
info, err := os.Stat(path)
if os.IsNotExist(err) {
return err
}
if !info.IsDir() {
return fmt.Errorf("%s is not a directory", path)
}
return nil
}
func find(path string) []dependency {
var wg sync.WaitGroup
var dependencies []dependency
err := filepath.Walk(path,
func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if info.Name() == gomod {
wg.Add(1)
go func() {
defer wg.Done()
modPath := path[:(len(path) - len(gomod))]
dep, err := findDepencencies(modPath)
if err != nil {
return
}
dependencies = append(dependencies, dep...)
}()
}
return nil
})
if err != nil {
log.Println(err)
}
wg.Wait()
return dependencies
}
func findDepencencies(path string) ([]dependency, error) {
var dependencies []dependency
cmd := exec.Command("go", args...)
cmd.Dir = path
list, err := cmd.Output()
if err != nil {
return nil, fmt.Errorf("cannot get %s available minor and patch upgrades. error: %w", path, err)
}
for _, dep := range strings.Split(string(list), "\n") {
if dep != "''" && dep != "" {
dep = strings.Trim(dep, "'")
d := strings.Split(strings.Trim(dep, "'"), " ")
dependencies = append(dependencies, dependency{
path: path,
name: d[0],
version: d[1],
updateVersion: d[2],
})
}
}
return dependencies, nil
}