-
Notifications
You must be signed in to change notification settings - Fork 40
/
Copy pathgooget_remove.go
98 lines (88 loc) · 2.88 KB
/
googet_remove.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
/*
Copyright 2016 Google Inc. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package main
// The remove subcommand handles the uninstallation of a package.
import (
"bytes"
"context"
"flag"
"fmt"
"os"
"path/filepath"
"github.com/google/googet/v2/goolib"
"github.com/google/googet/v2/remove"
"github.com/google/logger"
"github.com/google/subcommands"
)
type removeCmd struct {
dbOnly bool
}
func (cmd *removeCmd) Name() string { return "remove" }
func (cmd *removeCmd) Synopsis() string { return "uninstall a package" }
func (cmd *removeCmd) Usage() string {
return fmt.Sprintf("%s remove <name>\n", os.Args[0])
}
func (cmd *removeCmd) SetFlags(f *flag.FlagSet) {
f.BoolVar(&cmd.dbOnly, "db_only", false, "only make changes to DB, don't perform uninstall system actions")
}
func (cmd *removeCmd) Execute(ctx context.Context, flags *flag.FlagSet, _ ...interface{}) subcommands.ExitStatus {
exitCode := subcommands.ExitSuccess
sf := filepath.Join(rootDir, stateFile)
state, err := readState(sf)
if err != nil {
logger.Error(err)
}
for _, arg := range flags.Args() {
pi := goolib.PkgNameSplit(arg)
var ins []string
for _, ps := range *state {
if ps.Match(pi) {
ins = append(ins, ps.PackageSpec.Name+"."+ps.PackageSpec.Arch)
}
}
if len(ins) == 0 {
logger.Errorf("Package %q not installed, cannot remove.", arg)
continue
}
if len(ins) > 1 {
fmt.Fprintf(os.Stderr, "More than one %s installed, chose one of:\n%s\n", arg, ins)
return subcommands.ExitFailure
}
pi = goolib.PkgNameSplit(ins[0])
deps, dl := remove.EnumerateDeps(pi, *state)
if !noConfirm {
var b bytes.Buffer
fmt.Fprintln(&b, "The following packages will be removed:")
for _, d := range dl {
fmt.Fprintln(&b, " "+d)
}
fmt.Fprintf(&b, "Do you wish to remove %s and all dependencies?", pi.Name)
if !confirmation(b.String()) {
fmt.Println("canceling removal...")
continue
}
}
fmt.Printf("Removing %s and all dependencies...\n", pi.Name)
if err = remove.All(ctx, pi, deps, state, cmd.dbOnly, proxyServer); err != nil {
logger.Errorf("error removing %s, %v", arg, err)
exitCode = subcommands.ExitFailure
continue
}
logger.Infof("Removal of %q and dependant packages completed", pi.Name)
fmt.Printf("Removal of %s completed\n", pi.Name)
if err := writeState(state, sf); err != nil {
logger.Fatalf("error writing state file: %v", err)
}
}
return exitCode
}