-
Notifications
You must be signed in to change notification settings - Fork 0
/
scoopfz.go
348 lines (314 loc) · 7.88 KB
/
scoopfz.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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
package main
import (
"bufio"
"bytes"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"log"
"os"
"os/exec"
"path"
"path/filepath"
"sort"
"strings"
)
type Manifest struct {
Description string `json:"description"`
Home string `json:"homepage"`
Version string `json:"version"`
ModTime int64
Bucket string
App string
Installed bool
Updated bool
Hold bool
}
type InstallJson struct {
Hold bool `json:"hold"`
}
func scoopUpdate() string {
var buf bytes.Buffer
cmd := exec.Command("scoop", "update")
//f, err := os.Create("scoop.out")
//if err != nil {
// log.Fatal(err)
//}
//defer f.Close()
w := io.MultiWriter(&buf, os.Stdout) //, f)
//cmd.Stdout = os.Stdout
cmd.Stdout = w
if err := cmd.Run(); err != nil {
log.Fatal(err, " cmd.Run() failed.")
}
return buf.String()
}
func hasArg(arg string) bool {
if len(os.Args) > 1 {
for _, a := range os.Args {
if a == arg {
return true
}
}
}
return false
}
func main() {
var err error
scoopRoot := path.Join(os.Getenv("USERPROFILE"), "scoop")
bucketDir := scoopRoot + "/buckets"
fzfpath, err := exec.LookPath("fzf")
if err != nil {
fmt.Println("Could not find fzf.")
os.Exit(1)
}
log.SetFlags(log.Lshortfile)
var (
//ColorRed = "[91m"
ColorGreen = "[92m"
ColorYellow = "[93m"
ColorCyan = "[96m"
)
var buf string
if hasArg("u") || hasArg("update") {
buf = scoopUpdate()
} else if hasArg("-p") {
fmt.Println("Reading previous output.")
b, err := os.ReadFile("scoop.out")
if err != nil {
log.Fatal(err)
}
buf = string(b)
fmt.Println(buf)
pressEnterToContinue()
}
//log.Println("buf:", buf)
var updated []string
for _, line := range strings.Split(buf, "\n") {
//log.Println(line)
if strings.HasPrefix(line, " * ") {
app := strings.Split(line, " ")[3]
app = strings.TrimSuffix(app, ":")
app = strings.Split(app, "@")[0]
updated = append(updated, app)
}
}
//log.Println("updated:", updated)
if len(updated) != 0 {
pressEnterToContinue()
}
fmt.Println("Building app list..")
bucketNames, err := ioutil.ReadDir(bucketDir)
if err != nil {
log.Fatal(err, "could not read bucket dir:", bucketDir)
}
app_dirs, err := os.ReadDir(scoopRoot + "/apps")
if err != nil {
log.Fatal(err, "Could not read installed apps dir")
}
var installed []string
for _, f := range app_dirs {
installed = append(installed, f.Name())
}
// scan for held apps
var held []string
for _, appdir := range app_dirs {
// log.Println("appdir:", appdir.Name())
installFile := filepath.Join(scoopRoot, "apps", appdir.Name(), "current", "install.json")
jsonStr, err := os.ReadFile(installFile)
if err != nil {
log.Println(err)
log.Println("Could not read install file", installFile)
log.Println("installFile:", installFile)
continue
}
// log.Println("jsonStr:", string(jsonStr))
var jsn InstallJson
err = json.Unmarshal(jsonStr, &jsn)
if err != nil {
log.Println("Could not unmarshal install json")
}
// log.Println("hold:", jsn.Hold)
if jsn.Hold {
held = append(held, appdir.Name())
}
}
var entries []Manifest
var bucketLocation = make(map[string]string)
for _, f := range bucketNames {
bucketName := f.Name()
bdir := bucketDir + "/" + bucketName + "/bucket"
bucketLocation[bucketName] = bdir
files, err := os.ReadDir(bdir)
if err != nil {
log.Println("Could not read bucket dir.", bdir, err)
}
var manifest Manifest
for _, v := range files {
path := bdir + "/" + v.Name()
if strings.HasSuffix(v.Name(), ".json") {
appName := strings.TrimSuffix(v.Name(), filepath.Ext(v.Name()))
//log.Println("appName:", appName)
contents, err := ioutil.ReadFile(path)
if err != nil {
log.Fatal(err, "could not read file", path)
}
err = json.Unmarshal(contents, &manifest)
if err != nil {
log.Println("skipping", appName, "Could not unmarshal json string.\n", string(contents))
continue
}
info, err := v.Info()
if err != nil {
log.Fatal(err, "could not get info")
}
manifest.ModTime = info.ModTime().Unix()
manifest.Bucket = bucketName
manifest.App = appName
manifest.Installed = contains(installed, appName)
manifest.Updated = contains(updated, appName)
manifest.Hold = contains(held, appName)
entries = append(entries, manifest)
}
}
}
// sort.Slice(entries, func(i, j int) bool {
// return entries[i].ModTime > entries[j].ModTime
// })
sort.Slice(entries, func(i, j int) bool {
return entries[i].App < entries[j].App
})
var sb strings.Builder
var color string
for _, manifest := range entries {
if manifest.Installed {
sb.WriteString("i")
} else {
sb.WriteString(" ")
}
if manifest.Updated {
sb.WriteString("u")
} else {
sb.WriteString(" ")
}
if manifest.Hold {
sb.WriteString("h")
} else {
sb.WriteString(" ")
}
fmt.Fprintf(&sb, "| b:%-8s", manifest.Bucket)
color = ""
if manifest.Installed {
color = ColorGreen
}
if manifest.Updated {
if manifest.Installed {
color = ColorYellow
} else {
color = ColorCyan
}
}
fmt.Fprintf(&sb, "| %s%-25s[0m", color, manifest.App)
sb.WriteString(
fmt.Sprintf("| %s |%s|%s\n",
manifest.Description,
manifest.Home,
manifest.Version))
}
// Call fzf with sb as stdin
/*
var fzf_default_cmd = " --ansi" +
" --reverse" +
` --delimiter "\|"` +
" --no-hscroll" +
" --no-sort" +
" --multi" +
` --header "F1:Homepage | F2:Install | F3:Uninstall | F4:Update | F5:Hold | F6:Unhold | Esc:Exit"` +
` --bind "f1:execute(scoop home {3})"` +
` --bind "f2:execute(scoop install {+3})"` +
` --bind "f3:execute(scoop uninstall {+3})"` +
` --bind "f4:execute(scoop update {+3})"` +
` --bind "f5:execute(scoop hold {+3})"` +
` --bind "f6:execute(scoop unhold {+3})"` +
` --bind "ctrl-a:toggle-all"` +
` --bind "change:top"` +
` --preview-window "top:4:wrap"` +
` --preview "echo {3} {-1} {2} && echo {5} && echo {4}"`
log.Println(fzf_default_cmd)
_ = fzfpath
cmd := exec.Command("fzf", fzf_default_cmd)
*/
cmd := exec.Command(fzfpath,
"--ansi", "--reverse", `--delimiter=\|`,
"--no-hscroll", "--no-sort", "--multi",
"--header", "F1:Homepage | F2:Install | F3:Uninstall | F4:Update | F5:Hold | F6:Unhold | Esc:Exit",
"--bind", "f1:execute(scoop home {3})",
"--bind", "f2:execute(scoop install {+3})",
"--bind", "f3:execute(scoop uninstall {+3})",
"--bind", "f4:execute(scoop update {+3})",
"--bind", "f5:execute(scoop hold {+3})",
"--bind", "f6:execute(scoop unhold {+3})",
"--bind", "ctrl-a:toggle-all",
"--bind", "change:top",
"--bind", "ctrl-i:change-query(^i)",
"--preview-window=top:4:wrap",
"--preview=echo {3} {-1} {2} && echo {5} && echo {4}")
log.Println(cmd.String())
stdin, err := cmd.StdinPipe()
if err != nil {
log.Fatal(err, " could not pipe stdin")
}
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stdout
go func() {
defer stdin.Close()
io.WriteString(stdin, sb.String())
}()
if err = cmd.Run(); err != nil {
log.Printf("%v could not get combined output.\n", err)
}
}
func pressEnterToContinue() {
fmt.Print("\n> Press Enter to continue ")
fmt.Scanln()
}
func contains(s []string, e string) bool {
for _, a := range s {
if a == e {
//log.Println(a, "==", e)
return true
}
}
return false
}
func RemoveIndex(items []string, index int) []string {
return append(items[:index], items[index+1:]...)
}
// StringsFromFile()
func ReadLines(path string) ([]string, error) {
file, err := os.Open(path)
if err != nil {
log.Fatal(err)
}
defer file.Close()
var lines []string
scanner := bufio.NewScanner(file)
for scanner.Scan() {
lines = append(lines, scanner.Text())
}
return lines, scanner.Err()
}
// StringsToFile
func WriteLines(path string, lines []string) error {
file, err := os.Create(path)
if err != nil {
return err
}
defer file.Close()
w := bufio.NewWriter(file)
for _, line := range lines {
fmt.Fprintln(w, line)
}
return w.Flush()
}