-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathuninstall.go
96 lines (89 loc) · 2.01 KB
/
uninstall.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
// Copyright (c) 2015, Daniel Martí <[email protected]>
// See LICENSE for licensing information
package main
import (
"errors"
"fmt"
"strconv"
"mvdan.cc/fdroidcl/adb"
)
var cmdUninstall = &Command{
UsageLine: "uninstall <appid...>",
Short: "Uninstall an app",
}
var (
uninstallUser = cmdUninstall.Fset.String("user", "all", "Uninstall for specified user <USER_ID|current|all>")
)
func init() {
cmdUninstall.Run = runUninstall
}
func runUninstall(args []string) error {
if len(args) < 1 {
return fmt.Errorf("no package names given")
}
device, err := oneDevice()
if err != nil {
return err
}
inst, err := device.Installed()
if err != nil {
return err
}
if *uninstallUser != "all" && *uninstallUser != "current" {
n, err := strconv.Atoi(*uninstallUser)
if err != nil {
return fmt.Errorf("-user has to be <USER_ID|current|all>")
}
if n < 0 {
return fmt.Errorf("-user cannot have a negative number as USER_ID")
}
allUids := adb.AllUserIds(inst)
if _, exists := allUids[n]; !exists {
return fmt.Errorf("user %d does not exist", n)
}
}
if *uninstallUser == "current" {
uid, err := device.CurrentUserId()
if err != nil {
return err
}
*uninstallUser = strconv.Itoa(uid)
}
for _, id := range args {
var err error
fmt.Printf("Uninstalling %s\n", id)
app, installed := inst[id]
if installed {
installedForUser := false
if *uninstallUser == "all" {
installedForUser = true
} else {
uid, err := strconv.Atoi(*uninstallUser)
if err != nil {
return err
}
for _, appUser := range app.InstalledForUsers {
if appUser == uid {
installedForUser = true
break
}
}
}
if installedForUser {
if *uninstallUser == "all" {
err = device.Uninstall(id)
} else {
err = device.UninstallUser(id, *uninstallUser)
}
} else {
err = errors.New("not installed for user")
}
} else {
err = errors.New("not installed")
}
if err != nil {
return fmt.Errorf("could not uninstall %s: %v", id, err)
}
}
return nil
}