From 9622806bee9c12d1cff2e45bffb4095a3af207db Mon Sep 17 00:00:00 2001 From: Linus789 Date: Mon, 9 Jan 2023 04:31:23 +0100 Subject: [PATCH] add option to only install for specific user related issue: #37 --- adb/device.go | 13 +++++++++++++ install.go | 11 +++++++++-- 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/adb/device.go b/adb/device.go index 320772f..969ac2c 100644 --- a/adb/device.go +++ b/adb/device.go @@ -149,6 +149,19 @@ func (d *Device) Install(path string) error { return parseError(getFailureCode(installFailureRegex, line)) } +func (d *Device) InstallUser(path, user string) error { + cmd := d.AdbCmd(append([]string{"install", "-r", "--user"}, user, path)...) + output, err := cmd.CombinedOutput() + if err != nil { + return err + } + line := getResultLine(output) + if line == "Success" { + return nil + } + return parseError(getFailureCode(installFailureRegex, line)) +} + func getResultLine(output []byte) string { scanner := bufio.NewScanner(bytes.NewReader(output)) for scanner.Scan() { diff --git a/install.go b/install.go index 49cd982..13d3b31 100644 --- a/install.go +++ b/install.go @@ -28,6 +28,7 @@ list of apps to install from standard input, like: var ( installUpdates = cmdInstall.Fset.Bool("u", false, "Upgrade all installed apps") installDryRun = cmdInstall.Fset.Bool("n", false, "Only print the operations that would be done") + installUser = cmdInstall.Fset.String("user", "", "Only install for specified user") ) func init() { @@ -143,8 +144,14 @@ func downloadAndDo(apps []fdroid.App, device *adb.Device) error { func installApk(device *adb.Device, apk *fdroid.Apk, path string) error { fmt.Printf("Installing %s\n", apk.AppID) - if err := device.Install(path); err != nil { - return fmt.Errorf("could not install %s: %v", apk.AppID, err) + if *installUser != "" { + if err := device.InstallUser(path, *installUser); err != nil { + return fmt.Errorf("could not install %s: %v", apk.AppID, err) + } + } else { + if err := device.Install(path); err != nil { + return fmt.Errorf("could not install %s: %v", apk.AppID, err) + } } return nil }