-
Notifications
You must be signed in to change notification settings - Fork 53
/
predictors.go
65 lines (53 loc) · 1.58 KB
/
predictors.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
package hermit
import (
"github.com/posener/complete"
"github.com/cashapp/hermit/manifest"
"github.com/cashapp/hermit/state"
"github.com/cashapp/hermit/ui"
)
// PackagePredictor is a shell completion predictor for all package names
type PackagePredictor struct {
state *state.State
env *Env
l *ui.UI
}
// NewPackagePredictor returns a new PackagePredictor
func NewPackagePredictor(s *state.State, e *Env, l *ui.UI) *PackagePredictor {
return &PackagePredictor{s, e, l}
}
func (p *PackagePredictor) Predict(args complete.Args) []string { // nolint: golint
p.l.SetLevel(ui.LevelFatal)
var pkgs []*manifest.Package
// if there is an error, just quietly return an empty list
if p.env == nil {
ps, _ := p.state.Search(p.l, ".*")
pkgs = ps
} else {
ps, _ := p.env.Search(p.l, ".*")
pkgs = ps
}
res := make([]string, len(pkgs))
for i, pkg := range pkgs {
res[i] = pkg.Reference.Name
}
return res
}
// InstalledPackagePredictor is a shell completion predictor for installed package names
type InstalledPackagePredictor struct {
env *Env
l *ui.UI
}
// NewInstalledPackagePredictor returns a new InstalledPackagePredictor
func NewInstalledPackagePredictor(e *Env, l *ui.UI) *InstalledPackagePredictor {
return &InstalledPackagePredictor{e, l}
}
func (p *InstalledPackagePredictor) Predict(args complete.Args) []string { // nolint: golint
p.l.SetLevel(ui.LevelFatal)
// if there is an error, just quietly return an empty list
pkgs, _ := p.env.ListInstalled(p.l)
res := make([]string, len(pkgs))
for i, pkg := range pkgs {
res[i] = pkg.Reference.Name
}
return res
}