-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathinfo.go
More file actions
211 lines (185 loc) · 5.07 KB
/
Copy pathinfo.go
File metadata and controls
211 lines (185 loc) · 5.07 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
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
package main
import (
"context"
"fmt"
"path/filepath"
"strings"
"github.com/fxamacker/cbor/v2"
"github.com/goccy/go-json"
"github.com/goccy/go-yaml"
"github.com/urfave/cli/v3"
"github.com/zeebo/errs"
)
var (
errBinaryInfoNotFound = errs.Class("binary info not found")
)
func infoCommand() *cli.Command {
return &cli.Command{
Name: "info",
Usage: "Show information about a specific binary OR display installed binaries",
Flags: []cli.Flag{
&cli.BoolFlag{
Name: "json",
Usage: "Print output as JSON",
},
&cli.BoolFlag{
Name: "cbor",
Usage: "Print output as CBOR",
},
&cli.BoolFlag{
Name: "yaml",
Usage: "Print output as YAML",
},
},
Action: func(_ context.Context, c *cli.Command) error {
config, err := loadConfig()
if err != nil {
return err
}
var bEntry binaryEntry
if c.Args().First() != "" {
uRepoIndex, err := fetchRepoIndex(config)
if err != nil {
return err
}
bEntry = stringToBinaryEntry(c.Args().First())
binaryInfo, err := getBinaryInfo(config, bEntry, uRepoIndex)
if err != nil {
return errBinaryInfoNotFound.Wrap(err)
}
if c.Bool("json") {
jsonData, err := json.MarshalIndent(binaryInfo, "", " ")
if err != nil {
return err
}
fmt.Println(string(jsonData))
return nil
}
if c.Bool("cbor") {
cborData, err := cbor.Marshal(binaryInfo)
if err != nil {
return err
}
fmt.Println(string(cborData))
return nil
}
if c.Bool("yaml") {
yamlData, err := yaml.Marshal(binaryInfo)
if err != nil {
return err
}
fmt.Println(string(yamlData))
return nil
}
printBEntry(binaryInfo)
} else {
binaryEntries, err := validateProgramsFrom(config, nil, nil)
if err != nil {
return err
}
for _, program := range binaryEntries {
fmt.Println(parseBinaryEntry(program, true))
}
}
return nil
},
}
}
func findBinaryInfo(bEntry binaryEntry, uRepoIndex []binaryEntry) (binaryEntry, bool) {
matchingBins := findMatchingBins(bEntry, uRepoIndex)
if len(matchingBins) == 0 {
return binaryEntry{}, false
}
return matchingBins[0], true
}
func getBinaryInfo(config *config, bEntry binaryEntry, uRepoIndex []binaryEntry) (*binaryEntry, error) {
if instBEntry := bEntryOfinstalledBinary(filepath.Join(config.InstallDir, bEntry.Name)); bEntry.PkgID == "" && instBEntry.PkgID != "" {
bEntry = instBEntry
}
binInfo, found := findBinaryInfo(bEntry, uRepoIndex)
if found {
return &binInfo, nil
}
return nil, errBinaryInfoNotFound.New("info for the requested binary ('%s') not found in any of the repository index files", parseBinaryEntry(bEntry, false))
}
func printBEntry(bEntry *binaryEntry) {
fields := []struct {
label string
value any
}{
// Most important to the user
{"Name", bEntry.Name + "#" + bEntry.PkgID},
{"Pkg ID", bEntry.PkgID},
{"Pretty Name", bEntry.PrettyName},
{"Description", bEntry.Description},
{"Version", bEntry.Version},
{"Size", bEntry.Size},
{"Categories", bEntry.Categories},
{"Download URL", bEntry.DownloadURL},
{"WebURLs", bEntry.WebURLs},
{"SrcURLs", bEntry.SrcURLs},
{"B3SUM", bEntry.Bsum},
{"SHA256", bEntry.Shasum},
{"Build Date", bEntry.BuildDate},
{"Build Script", bEntry.BuildScript},
{"Build Log", bEntry.BuildLog},
// ------------------------------------
// Clutter:
// Useless in the context of `dbin`
// These are shown only in the complete
// repository index (non-lite version)
{"Screenshots", bEntry.Screenshots},
{"Icon URL", bEntry.Icon},
{"Web Manifest", bEntry.WebManifest},
{"Extra Bins", bEntry.ExtraBins},
// Clutter, but useful:
{"Snapshots", bEntry.Snapshots},
// SBUILD meta
{"Maintainers", bEntry.Maintainers},
{"Notes", bEntry.Notes},
{"License", bEntry.License},
{"Rank", bEntry.Rank},
}
for _, field := range fields {
switch v := field.value.(type) {
case []string:
for n, str := range v {
prefixLength := len(field.label)
prefix := blueBgWhiteFg + field.label + resetColor
if n > 0 {
prefix = strings.Repeat(" ", prefixLength)
}
fmt.Printf("%s: %s\n", prefix, str)
}
case []snapshot:
for n, snap := range v {
prefix := blueBgWhiteFg + field.label + resetColor
if n > 0 {
prefix = " "
}
if snap.Commit != "" {
fmt.Printf("%s: %s %s\n", prefix, snap.Commit, ternary(snap.Version != "", "["+cyanColor+snap.Version+resetColor+"]", ""))
} else {
fmt.Printf("%s: %s\n", prefix, "["+cyanColor+snap.Version+resetColor+"]")
}
}
case uint16:
if v != 0 {
switch v {
case 1:
fmt.Printf("%s\x1b[0m: 🥇(%v)\n", blueBgWhiteFg+field.label+resetColor, v)
case 2:
fmt.Printf("%s\x1b[0m: 🥈(%v)\n", blueBgWhiteFg+field.label+resetColor, v)
case 3:
fmt.Printf("%s\x1b[0m: 🥉(%v)\n", blueBgWhiteFg+field.label+resetColor, v)
default:
fmt.Printf("%s\x1b[0m: %v\n", blueBgWhiteFg+field.label+resetColor, v)
}
}
default:
if v != "" && v != 0 {
fmt.Printf("%s\x1b[0m: %v\n", blueBgWhiteFg+field.label+resetColor, v)
}
}
}
}