-
Notifications
You must be signed in to change notification settings - Fork 0
/
list.go
134 lines (114 loc) · 2.87 KB
/
list.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
package actions
import (
"context"
"fmt"
"strings"
"github.com/act3-ai/hops/internal/o"
"github.com/act3-ai/hops/internal/platform"
"github.com/act3-ai/hops/internal/prefix/keg"
)
// List represents the action and its options.
type List struct {
*Hops
// // Print formulae with fully-qualified names.
// // Unless --full-name, --versions or
// // --pinned are passed, other options (i.e.
// // -1, -l, -r and -t) are passed to
// // ls(1) which produces the actual output
// FullName bool
// Show the version number for installed
// formulae, or only the specified formulae if
// formula are provided
Versions bool
// Only show formulae with multiple versions
// installed
Multiple bool
// // List only pinned formulae, or only the
// // specified (pinned) formulae if formula are
// // provided. See also pin, unpin
// Pinned bool
// // Force output to be one entry per line. This
// // is the default when output is not to a
// // terminal
// OnePerLine bool
// // List formulae and/or casks in long format.
// // Has no effect when a formula or cask name is
// // passed as an argument
// Long bool
// // Reverse the order of the formulae and/or
// // casks sort to list the oldest entries first.
// // Has no effect when a formula or cask name is
// // passed as an argument
// Reverse bool
// // Sort formulae and/or casks by time modified,
// // listing most recently modified first. Has no
// // effect when a formula or cask name is passed
// // as an argument
// TimeSort bool
}
// Run runs the action.
func (action *List) Run(ctx context.Context, names ...string) error {
switch {
case len(names) > 0:
return action.names(ctx, names)
case action.Multiple:
kegs, err := action.Prefix().Kegs()
if err != nil {
return err
}
multiple(kegs)
default:
racks, err := action.Prefix().Racks()
if err != nil {
return err
}
o.Hai("Installed:")
for _, r := range racks {
fmt.Println(r.Name())
}
}
return nil
}
func multiple(kegs []keg.Keg) {
count := map[string]int{}
for _, k := range kegs {
count[k.Name()]++
}
for _, k := range kegs {
if count[k.Name()] > 1 {
fmt.Println(k.Name() + " " + k.Version())
}
}
}
func (action *List) names(ctx context.Context, args []string) error {
formulae, err := action.fetchFromArgs(ctx, args, platform.SystemPlatform())
if err != nil {
return err
}
for _, f := range formulae {
kegs, err := action.Prefix().InstalledKegs(f)
if err != nil {
return err
}
switch {
case action.Multiple:
// Only output this info if there are multiple kegs
if len(kegs) > 1 {
multiple(kegs)
}
case action.Versions:
for _, k := range kegs {
fmt.Println(k.Name() + " " + k.Version())
}
default:
for _, k := range kegs {
kpaths, err := k.Paths()
if err != nil {
return err
}
fmt.Println(strings.Join(kpaths, "\n"))
}
}
}
return nil
}