-
Notifications
You must be signed in to change notification settings - Fork 51
/
list_cmd.go
80 lines (74 loc) · 1.7 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
package app
import (
"fmt"
"go/doc"
"os"
"sort"
"strings"
"github.com/alecthomas/colour"
"github.com/pkg/errors"
"golang.org/x/crypto/ssh/terminal"
"github.com/cashapp/hermit"
"github.com/cashapp/hermit/manifest"
"github.com/cashapp/hermit/ui"
)
type listCmd struct {
Short bool `short:"s" help:"Short listing."`
}
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
}
listPackages(pkgs, false)
return nil
}
func listPackages(pkgs manifest.Packages, allVersions bool) {
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)
w, _, _ := terminal.GetSize(0)
if w == -1 {
w = 80
}
for _, name := range names {
pkgs := byName[name]
var versions []string
for _, pkg := range pkgs {
if !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)
}
}