-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
165 lines (142 loc) · 3.45 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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
package main
import (
"fmt"
"log"
"os"
"github.com/Masterminds/semver"
"github.com/charmbracelet/huh/spinner"
"github.com/charmbracelet/lipgloss"
"github.com/charmbracelet/lipgloss/table"
"github.com/jon4hz/canihazgnomeupdate/extensions"
"github.com/jon4hz/canihazgnomeupdate/gnome"
"github.com/spf13/cobra"
)
var Version = "devel"
var rootCmd = &cobra.Command{
Use: "canihazgnomeupdate",
Run: root,
Args: cobra.ExactArgs(1),
Version: Version,
}
var rootCmdFlags struct {
enabled bool
}
func init() {
rootCmd.Flags().BoolVar(&rootCmdFlags.enabled, "enabled", true, "Only search for enabled extensions")
}
func main() {
if err := rootCmd.Execute(); err != nil {
os.Exit(1)
}
}
func root(cmd *cobra.Command, args []string) {
gnomeVersion := args[0]
// list local extensions
exts, err := extensions.List(rootCmdFlags.enabled)
if err != nil {
log.Fatalln("Failed to get installed extensions: ", err)
}
results := make([]checkResult, 0, len(exts))
spinner := spinner.New().
Type(spinner.Points).
Title(" Checking extensions...").
Style(lipgloss.NewStyle().Foreground(lipgloss.Color("99"))).
Action(func() {
for _, ext := range exts {
result, err := gnome.SearchExtension(ext)
if err != nil {
log.Fatalln("Failed to search extensions: ", err)
}
results = append(results, checkResult{
UUID: ext,
URL: result.URL,
LatestVersion: latestExtensionVersion(result),
UpdateSupported: supportsUpdate(result, gnomeVersion),
})
}
})
if err := spinner.Run(); err != nil {
log.Fatalln("Failed to run spinner: ", err)
}
printResults(results)
summary(results)
}
func latestExtensionVersion(ext *gnome.Extension) string {
newestSem := semver.MustParse("0")
var newest string
for shellVersion := range ext.ShellVersionMap {
ver, err := semver.NewVersion(shellVersion)
if err != nil {
log.Printf("Failed to parse version %q of extension %q: %s", shellVersion, ext.UUID, err)
continue
}
if ver.GreaterThan(newestSem) {
newestSem = ver
newest = shellVersion
}
}
return newest
}
func supportsUpdate(ext *gnome.Extension, gnomeVersion string) bool {
_, ok := ext.ShellVersionMap[gnomeVersion]
return ok
}
type checkResult struct {
UUID string
URL string
LatestVersion string
UpdateSupported bool
}
func printResults(res []checkResult) {
rows := make([][]string, 0, len(res))
for _, r := range res {
var updateSupported string
if r.UpdateSupported {
updateSupported = "✅"
} else {
updateSupported = "❌"
}
rows = append(rows, []string{
updateSupported,
r.UUID,
r.LatestVersion,
r.URL,
})
}
re := lipgloss.NewRenderer(os.Stdout)
var (
cellStyle = re.NewStyle().Padding(0, 1)
)
t := table.New().
Border(lipgloss.NormalBorder()).
BorderStyle(lipgloss.NewStyle().Foreground(lipgloss.Color("99"))).
StyleFunc(func(row, col int) lipgloss.Style {
switch {
case row == 0:
return cellStyle.Copy().Bold(true).Align(lipgloss.Center)
case col == 2:
return cellStyle.Copy().Align(lipgloss.Center)
default:
return cellStyle
}
}).
Headers("Ok?", "UUID", "Latest", "URL").
Rows(rows...)
fmt.Println()
fmt.Println(t)
fmt.Println()
}
func summary(res []checkResult) {
ok := true
for _, r := range res {
if !r.UpdateSupported {
ok = false
break
}
}
if ok {
fmt.Println("😸 You can haz gnome update!")
} else {
fmt.Println("😿 You can't haz gnome update!")
}
}