-
Notifications
You must be signed in to change notification settings - Fork 51
/
uninstall_cmd.go
59 lines (53 loc) · 1.38 KB
/
uninstall_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
package app
import (
"fmt"
"os"
"github.com/pkg/errors"
"github.com/cashapp/hermit"
"github.com/cashapp/hermit/envars"
"github.com/cashapp/hermit/manifest"
"github.com/cashapp/hermit/shell"
"github.com/cashapp/hermit/ui"
)
type uninstallCmd struct {
Packages []string `arg:"" help:"Packages to uninstall from this environment." predictor:"installed-package"`
}
func (u *uninstallCmd) Run(l *ui.UI, env *hermit.Env) error {
installed, err := env.ListInstalled(l)
if err != nil {
return errors.WithStack(err)
}
selectors := []manifest.Selector{}
for _, pkg := range u.Packages {
globs, err := manifest.GlobSelector(pkg)
if err != nil {
return errors.WithStack(err)
}
selectors = append(selectors, globs)
}
w := l.WriterAt(ui.LevelInfo)
defer w.Sync() // nolint
changes := shell.NewChanges(envars.Parse(os.Environ()))
next:
for _, selector := range selectors {
for _, pkg := range installed {
if selector.Matches(pkg.Reference) {
c, err := env.Uninstall(l, pkg)
if err != nil {
return errors.WithStack(err)
}
changes = changes.Merge(c)
messages, err := env.TriggerForPackage(l, manifest.EventUninstall, pkg)
if err != nil {
return errors.WithStack(err)
}
for _, message := range messages {
fmt.Fprintln(w, message)
}
continue next
}
}
return errors.Errorf("package %s is not installed", selector)
}
return nil
}