xplshn
·
2025-08-13
list.go
Go
1package main
2
3import (
4 "context"
5 "fmt"
6 "strings"
7
8 "github.com/urfave/cli/v3"
9 "github.com/zeebo/errs"
10)
11
12var (
13 errListBinariesFailed = errs.Class("list binaries failed")
14)
15
16// filterBEntries applies a filter function to a []binaryEntry
17func filterBEntries(entries *[]binaryEntry, filterFunc func(binaryEntry) bool) {
18 if entries == nil {
19 return
20 }
21
22 filtered := make([]binaryEntry, 0, len(*entries))
23 for _, entry := range *entries {
24 if filterFunc(entry) {
25 filtered = append(filtered, entry)
26 }
27 }
28 *entries = filtered
29}
30
31func listCommand() *cli.Command {
32 return &cli.Command{
33 Name: "list",
34 Usage: "List all available binaries",
35 Flags: []cli.Flag{
36 &cli.BoolFlag{
37 Name: "detailed",
38 Aliases: []string{"d"},
39 Usage: "List binaries with their descriptions",
40 },
41 &cli.StringFlag{
42 Name: "repo",
43 Aliases: []string{"repos", "r"},
44 Usage: "Filter binaries by repository name",
45 },
46 },
47 Action: func(_ context.Context, c *cli.Command) error {
48 config, err := loadConfig()
49 if err != nil {
50 return errListBinariesFailed.Wrap(err)
51 }
52 uRepoIndex, err := fetchRepoIndex(config)
53 if err != nil {
54 return errListBinariesFailed.Wrap(err)
55 }
56 if c.Bool("detailed") {
57 return fSearch(config, []string{""}, uRepoIndex)
58 }
59 bEntries, err := listBinaries(uRepoIndex)
60 if err != nil {
61 return errListBinariesFailed.Wrap(err)
62 }
63
64 // Apply repository filter if specified
65 if repoNames := c.String("repo"); repoNames != "" {
66 repoSet := make(map[string]struct{})
67 for _, repo := range strings.Split(repoNames, ",") {
68 repoSet[strings.TrimSpace(repo)] = struct{}{}
69 }
70 filterBEntries(&bEntries, func(entry binaryEntry) bool {
71 _, ok := repoSet[entry.Repository.Name]
72 return ok
73 })
74 }
75
76 for _, binary := range binaryEntriesToArrString(bEntries, true) {
77 fmt.Println(binary)
78 }
79 return nil
80 },
81 }
82}
83
84func listBinaries(uRepoIndex []binaryEntry) ([]binaryEntry, error) {
85 filterBEntries(&uRepoIndex, func(entry binaryEntry) bool {
86 return entry.Name != "" //&& entry.Description != ""
87 })
88
89 if len(uRepoIndex) == 0 {
90 return nil, errListBinariesFailed.New("no binaries found in the repository index")
91 }
92
93 return uRepoIndex, nil
94}