-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
219 lines (180 loc) · 5.33 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
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
package main
import (
"bytes"
"flag"
"fmt"
"os"
"os/exec"
"path/filepath"
"strings"
"github.com/mitchellh/go-homedir"
)
const commentSymbol = "#"
type PluginMap map[string]struct{}
type IndexMap map[string]string
type InvalidKrewfileLineError struct {
line string
}
func (e InvalidKrewfileLineError) Error() string {
return fmt.Sprintf("parsing krew file, invalid line: %q", e.line)
}
func main() {
if err := run(); err != nil {
fmt.Fprintf(os.Stderr, "an error occurred: %s\n", err)
os.Exit(1)
}
}
func run() error {
home, err := homedir.Dir()
if err != nil {
return err
}
var command string
var krewfileLocation string
var upgrade, dryRun bool
flag.StringVar(&command, "command", "krew", "command to be used for krew")
flag.StringVar(&krewfileLocation, "file", filepath.Join(home, ".krewfile"), "location of the krewfile")
flag.BoolVar(&upgrade, "upgrade", false, "runs krew upgrade after syncing plugins")
flag.BoolVar(&dryRun, "dry-run", false, "shows only output, doesn't modify anything")
flag.Parse()
fmt.Println("reading krewfile")
krewfile, err := os.ReadFile(krewfileLocation)
if err != nil {
return err
}
wantedPlugins, wantedIndexes, err := readKrewfile(krewfile)
if err != nil {
return err
}
fmt.Println("getting installed indexes")
krewIndexList, err := runKrewCommand(false, command, "index", "list")
if err != nil {
return err
}
installedIndexes := readIndexesFromKrew(krewIndexList)
// find indexes that are installed but not wanted anymore
// find indexes that are not installed but wanted
// -> remove all entries from both maps that are in both maps
for k := range installedIndexes {
if _, ok := wantedIndexes[k]; ok {
delete(installedIndexes, k)
delete(wantedIndexes, k)
}
}
// now installedIndexes only holds indexes that are not wanted anymore -> to be deleted
// wantedIndexes are the ones that need to be installed
for index := range installedIndexes {
// default index is special case, it's always wanted
if index == "default" {
continue
}
fmt.Printf("removing index %q\n", index)
if _, err := runKrewCommand(dryRun, command, "index", "remove", index); err != nil {
return err
}
}
for index, url := range wantedIndexes {
fmt.Printf("adding index %q\n", index)
if _, err := runKrewCommand(dryRun, command, "index", "add", index, url); err != nil {
return err
}
}
fmt.Println("updating krew")
if _, err := runKrewCommand(dryRun, command, "update"); err != nil {
return err
}
fmt.Println("getting installed plugins")
krewPluginList, err := runKrewCommand(false, command, "list")
if err != nil {
return err
}
installedPlugins := readPluginsFromKrew(krewPluginList)
// find plugins that are installed but not wanted anymore
// find plugins that are not installed but wanted
// -> remove all entries from both maps that are in both maps
for k := range installedPlugins {
if _, ok := wantedPlugins[k]; ok {
delete(installedPlugins, k)
delete(wantedPlugins, k)
}
}
// now installedPlugins only holds plugins that are not wanted anymore -> to be deleted
// wantedPlugins are the ones that need to be installed
for plugin := range installedPlugins {
fmt.Printf("removing plugin %q\n", plugin)
pluginItems := strings.Split(plugin, "/")
if _, err := runKrewCommand(dryRun, command, "uninstall", pluginItems[len(pluginItems)-1]); err != nil {
return err
}
}
for plugin := range wantedPlugins {
fmt.Printf("installing plugin %q\n", plugin)
if _, err := runKrewCommand(dryRun, command, "install", plugin); err != nil {
return err
}
}
if upgrade {
fmt.Println("upgrading plugins")
if _, err := runKrewCommand(dryRun, command, "upgrade"); err != nil {
return err
}
}
return nil
}
func readKrewfile(input []byte) (PluginMap, IndexMap, error) {
pluginMap := PluginMap{}
indexMap := IndexMap{}
for _, line := range strings.Split(string(input), "\n") {
lineWithComments := strings.SplitN(line, commentSymbol, 2)
if parsedLine := strings.TrimSpace(lineWithComments[0]); parsedLine != "" {
items := strings.Fields(parsedLine)
if len(items) == 1 {
pluginMap[items[0]] = struct{}{}
continue
}
if len(items) == 3 && items[0] == "index" {
indexMap[items[1]] = items[2]
continue
}
return nil, nil, InvalidKrewfileLineError{line: line}
}
}
return pluginMap, indexMap, nil
}
func readPluginsFromKrew(input []byte) PluginMap {
pluginMap := PluginMap{}
for _, line := range strings.Split(string(input), "\n") {
items := strings.Fields(line)
if len(items) < 1 || items[0] == "PLUGIN" {
continue
}
pluginMap[items[0]] = struct{}{}
}
return pluginMap
}
func readIndexesFromKrew(input []byte) IndexMap {
indexMap := IndexMap{}
for _, line := range strings.Split(string(input), "\n") {
items := strings.Fields(line)
if len(items) < 2 || items[0] == "INDEX" {
continue
}
indexMap[items[0]] = items[1]
}
return indexMap
}
func runKrewCommand(dryRun bool, krewCommand string, args ...string) ([]byte, error) {
fullCommand := append(strings.Split(krewCommand, " "), args...)
if dryRun {
fmt.Printf("will run: %q\n", strings.Join(fullCommand, " "))
return nil, nil
}
cmd := exec.Command(fullCommand[0], fullCommand[1:]...)
stderr := bytes.Buffer{}
cmd.Stderr = &stderr
out, err := cmd.Output()
if err != nil {
return nil, fmt.Errorf("krew error:\n%s", stderr.String())
}
return out, nil
}