-
Notifications
You must be signed in to change notification settings - Fork 51
/
list_cmd.go
176 lines (149 loc) · 4.02 KB
/
list_cmd.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
package app
import (
"encoding/json"
"fmt"
"go/doc"
"os"
"sort"
"strings"
"github.com/alecthomas/colour"
"golang.org/x/crypto/ssh/terminal"
"github.com/cashapp/hermit"
"github.com/cashapp/hermit/errors"
"github.com/cashapp/hermit/manifest"
"github.com/cashapp/hermit/ui"
)
// JSONFormattable contains the shared JSON boolean flag for Kong
type JSONFormattable struct {
JSON bool `help:"Format information as a JSON array" default:"false"`
}
type listCmd struct {
Short bool `short:"s" help:"Short listing."`
JSONFormattable
}
func buildListJSONResult(byName map[string][]*manifest.Package, names []string) interface{} {
packages := make([]*manifest.Package, 0)
for _, name := range names {
pg := byName[name]
for _, pkg := range pg {
if pkg.Linked {
packages = append(packages, pkg)
}
}
}
return packages
}
func (cmd *listCmd) Run(l *ui.UI, env *hermit.Env) error {
pkgs, err := env.ListInstalled(l)
if err != nil {
return errors.WithStack(err)
}
if cmd.Short {
for _, pkg := range pkgs {
fmt.Println(pkg)
}
return nil
}
err = listPackages(pkgs, &listPackageOption{
AllVersions: false,
TransformJSON: buildListJSONResult,
UI: l,
JSON: cmd.JSON,
})
if err != nil {
return errors.Wrapf(err, "error listing packages")
}
return nil
}
func groupPackages(pkgs []*manifest.Package) (map[string][]*manifest.Package, []string) {
byName := map[string][]*manifest.Package{}
for _, pkg := range pkgs {
name := pkg.Reference.Name
byName[name] = append(byName[name], pkg)
}
names := make([]string, 0, len(byName))
for name := range byName {
names = append(names, name)
}
sort.Strings(names)
return byName, names
}
// transformPackagesToJSON transforms the given grouped packages and ordered names into the output JSON struct
type transformPackagesToJSON func(byName map[string][]*manifest.Package, names []string) interface{}
func listPackagesInJSONFormat(pkgs manifest.Packages, option *listPackageOption) error {
byName, names := groupPackages(pkgs)
val := option.TransformJSON(byName, names)
content, err := json.Marshal(val)
if err != nil {
return errors.Wrapf(err, "error formatting packages output to json")
}
option.UI.Printf("%s\n", string(content))
return nil
}
type listPackageOption struct {
AllVersions bool
TransformJSON transformPackagesToJSON
UI *ui.UI
JSON bool
Prefix string
}
func sortSliceWithPrefix(names []string, prefix string) {
sort.Slice(names, func(i, j int) bool {
if prefix != "" {
left := strings.HasPrefix(names[i], prefix)
right := strings.HasPrefix(names[j], prefix)
if left && right {
return names[i] < names[j]
} else if left {
return true
} else if right {
return false
}
}
return names[i] < names[j]
})
}
func listPackagesInCLI(pkgs manifest.Packages, option *listPackageOption) {
byName, names := groupPackages(pkgs)
sortSliceWithPrefix(names, option.Prefix)
w, _, _ := terminal.GetSize(0)
if w == -1 {
w = 80
}
for _, name := range names {
printPackage(byName[name], option, name, w)
}
}
func printPackage(pkgs []*manifest.Package, option *listPackageOption, name string, w int) {
var versions []string
for _, pkg := range pkgs {
if !option.AllVersions && !pkg.Linked {
continue
}
clr := ""
suffix := ""
if pkg.Unsupported() {
clr = "^1"
suffix = " (architecture not supported)"
} else if pkg.Linked {
switch pkg.State {
case manifest.PackageStateRemote:
clr = "^1"
case manifest.PackageStateDownloaded:
clr = "^3"
case manifest.PackageStateInstalled:
clr = "^2"
}
}
versions = append(versions, fmt.Sprintf("%s%s%s^R", clr, pkg.Reference.StringNoName(), suffix))
}
colour.Println("^B^2" + name + "^R (" + strings.Join(versions, ", ") + ")")
doc.ToText(os.Stdout, pkgs[0].Description, " ", "", w-2)
}
func listPackages(pkgs manifest.Packages, option *listPackageOption) error {
if option.JSON {
return errors.WithStack(listPackagesInJSONFormat(pkgs, option))
}
listPackagesInCLI(pkgs, option)
return nil
}