-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathlist.go
More file actions
94 lines (83 loc) · 2.19 KB
/
list.go
File metadata and controls
94 lines (83 loc) · 2.19 KB
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
package main
import (
"context"
"fmt"
"strings"
"github.com/urfave/cli/v3"
"github.com/zeebo/errs"
)
var (
errListBinariesFailed = errs.Class("list binaries failed")
)
// filterBEntries applies a filter function to a []binaryEntry
func filterBEntries(entries *[]binaryEntry, filterFunc func(binaryEntry) bool) {
if entries == nil {
return
}
filtered := make([]binaryEntry, 0, len(*entries))
for _, entry := range *entries {
if filterFunc(entry) {
filtered = append(filtered, entry)
}
}
*entries = filtered
}
func listCommand() *cli.Command {
return &cli.Command{
Name: "list",
Usage: "List all available binaries",
Flags: []cli.Flag{
&cli.BoolFlag{
Name: "detailed",
Aliases: []string{"d"},
Usage: "List binaries with their descriptions",
},
&cli.StringFlag{
Name: "repo",
Aliases: []string{"repos", "r"},
Usage: "Filter binaries by repository name",
},
},
Action: func(_ context.Context, c *cli.Command) error {
config, err := loadConfig()
if err != nil {
return errListBinariesFailed.Wrap(err)
}
uRepoIndex, err := fetchRepoIndex(config)
if err != nil {
return errListBinariesFailed.Wrap(err)
}
if c.Bool("detailed") {
return fSearch(config, []string{""}, uRepoIndex)
}
bEntries, err := listBinaries(uRepoIndex)
if err != nil {
return errListBinariesFailed.Wrap(err)
}
// Apply repository filter if specified
if repoNames := c.String("repo"); repoNames != "" {
repoSet := make(map[string]struct{})
for _, repo := range strings.Split(repoNames, ",") {
repoSet[strings.TrimSpace(repo)] = struct{}{}
}
filterBEntries(&bEntries, func(entry binaryEntry) bool {
_, ok := repoSet[entry.Repository.Name]
return ok
})
}
for _, binary := range binaryEntriesToArrString(bEntries, true) {
fmt.Println(binary)
}
return nil
},
}
}
func listBinaries(uRepoIndex []binaryEntry) ([]binaryEntry, error) {
filterBEntries(&uRepoIndex, func(entry binaryEntry) bool {
return entry.Name != "" //&& entry.Description != ""
})
if len(uRepoIndex) == 0 {
return nil, errListBinariesFailed.New("no binaries found in the repository index")
}
return uRepoIndex, nil
}