diff --git a/adb/device.go b/adb/device.go index 8b527fd..6c09a28 100644 --- a/adb/device.go +++ b/adb/device.go @@ -336,3 +336,13 @@ func (d *Device) CurrentUserId() (int, error) { } return -1, fmt.Errorf("could not get current user id") } + +func AllUserIds(installed map[string]Package) map[int]struct{} { + uidMap := make(map[int]struct{}) + for _, pkg := range installed { + for _, uid := range pkg.InstalledForUsers { + uidMap[uid] = struct{}{} + } + } + return uidMap +} diff --git a/install.go b/install.go index e58f7b8..aca256e 100644 --- a/install.go +++ b/install.go @@ -50,6 +50,14 @@ func runInstall(args []string) error { if *installUpdatesExclude != "" && !*installUpdates { return fmt.Errorf("-e can only be used for upgrading (i.e. -u)") } + device, err := oneDevice() + if err != nil { + return err + } + inst, err := device.Installed() + if err != nil { + return err + } if *installUser != "" && *installUser != "all" && *installUser != "current" { n, err := strconv.Atoi(*installUser) if err != nil { @@ -58,10 +66,10 @@ func runInstall(args []string) error { if n < 0 { return fmt.Errorf("-user cannot have a negative number as USER_ID") } - } - device, err := oneDevice() - if err != nil { - return err + allUids := adb.AllUserIds(inst) + if _, exists := allUids[n]; !exists { + return fmt.Errorf("user %d does not exist", n) + } } if *installUser == "current" || (*installUser == "" && !*installUpdates) { uid, err := device.CurrentUserId() @@ -70,10 +78,6 @@ func runInstall(args []string) error { } *installUser = strconv.Itoa(uid) } - inst, err := device.Installed() - if err != nil { - return err - } if *installUpdates { apps, err := loadIndexes() diff --git a/search.go b/search.go index 4fe40d3..befe88a 100644 --- a/search.go +++ b/search.go @@ -75,6 +75,10 @@ func runSearch(args []string) error { 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) + } filterUser = &n } else { if *installUser == "current" { diff --git a/uninstall.go b/uninstall.go index 8cd7e40..3f1a605 100644 --- a/uninstall.go +++ b/uninstall.go @@ -7,6 +7,8 @@ import ( "errors" "fmt" "strconv" + + "mvdan.cc/fdroidcl/adb" ) var cmdUninstall = &Command{ @@ -26,6 +28,14 @@ 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 { @@ -34,10 +44,10 @@ func runUninstall(args []string) error { if n < 0 { return fmt.Errorf("-user cannot have a negative number as USER_ID") } - } - device, err := oneDevice() - if err != nil { - return err + 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() @@ -46,10 +56,6 @@ func runUninstall(args []string) error { } *uninstallUser = strconv.Itoa(uid) } - inst, err := device.Installed() - if err != nil { - return err - } for _, id := range args { var err error fmt.Printf("Uninstalling %s\n", id)