-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
141 lines (119 loc) · 3.19 KB
/
main.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
package main
import (
"fmt"
"io/ioutil"
"sort"
"github.com/abiosoft/ishell"
"github.com/fatih/color"
"golang.org/x/mod/modfile"
)
// FileName is the main file used for parsing
var FileName = "go.mod"
func main() {
// Read in go mod file
dat, err := ioutil.ReadFile("./" + FileName)
if err != nil {
panic(err)
}
// Pares file
file, err := modfile.Parse(FileName, dat, nil)
if err != nil {
panic(err)
}
// Colors
red := color.New(color.FgRed).SprintFunc()
yellow := color.New(color.FgYellow).SprintFunc()
green := color.New(color.FgGreen).SprintFunc()
blue := color.New(color.FgBlue).SprintFunc()
var mods Mods
urlLength := 0
curVersionLength := 0
latestVersionLength := 0
// Loop through required mods and exclude indirect ones
for _, r := range file.Require {
if r.Indirect {
continue
}
// Check if mod is on the most current version
mod, err := NewMod(r.Mod.Path, r.Mod.Version)
if err != nil {
continue
}
// Do nothing if mod is current
if mod.Status == "current" {
continue
}
// Check url length for option padding
if urlLength < len(mod.Path) {
urlLength = len(mod.Path)
}
if curVersionLength < len(mod.CurrentVersion.cleanString()) {
curVersionLength = len(mod.CurrentVersion.cleanString())
}
if latestVersionLength < len(mod.AvailableVersions[0].cleanString()) {
latestVersionLength = len(mod.AvailableVersions[0].cleanString())
}
mods = append(mods, mod)
}
// Sort mods by status
sort.Sort(mods)
// Create options
var options []string
hasIncompatible := false
for _, m := range mods {
urlStr := strPadding(m.Path, urlLength) + " "
versionStr := strPadding(m.CurrentVersion.cleanString(), curVersionLength) + " -> " + strPadding(m.AvailableVersions[0].cleanString(), latestVersionLength)
if m.AvailableVersions[0].incompatible {
versionStr += " " + blue("I")
hasIncompatible = true
}
if m.Status == "major" {
options = append(options, red(urlStr)+versionStr)
} else if m.Status == "minor" {
options = append(options, yellow(urlStr)+versionStr)
} else if m.Status == "patch" {
options = append(options, green(urlStr)+versionStr)
}
}
if len(options) == 0 {
fmt.Println(green("You are all up to date!!!"))
return
}
shell := ishell.New()
shell.AddCmd(&ishell.Cmd{
Name: "checklist",
Help: "checklist prompt",
Func: func(c *ishell.Context) {
text := "Hit space to select modules you want to update. Ctrl + c to cancel\n"
text += green("Patch") + " " + yellow("Minor") + " " + red("Major") + " "
if hasIncompatible {
text += blue("I = Incompatible")
}
choices := c.Checklist(options, text, nil)
if len(choices) > 0 && choices[0] != -1 {
c.ClearScreen()
c.Println(green("Modules that were updated!!!"))
for _, i := range choices {
err := file.AddRequire(mods[i].Path, mods[i].AvailableVersions[0].original)
if err != nil {
c.Err(err)
}
c.Println(options[i])
}
file.Cleanup()
dat, err := file.Format()
if err != nil {
c.Err(err)
}
// Write back to file
err = ioutil.WriteFile("./"+FileName, dat, 644)
if err != nil {
c.Err(err)
}
}
shell.Close()
},
})
shell.Process("checklist")
shell.Run()
}