diff --git a/install.go b/install.go index e8ae784..dc6b3cb 100644 --- a/install.go +++ b/install.go @@ -5,6 +5,7 @@ package main import ( "fmt" + "os" "mvdan.cc/fdroidcl/adb" "mvdan.cc/fdroidcl/fdroid" @@ -15,27 +16,49 @@ var cmdInstall = &Command{ Short: "Install or upgrade an app", } +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") +) + func init() { cmdInstall.Run = runInstall } func runInstall(args []string) error { - if len(args) < 1 { + if *installUpdates { + if len(args) > 0 { + return fmt.Errorf("-u can only be used without arguments") + } + } else if len(args) < 1 { return fmt.Errorf("no package names given") } device, err := oneDevice() if err != nil { return err } - apps, err := findApps(args) + inst, err := device.Installed() if err != nil { return err } - inst, err := device.Installed() + + if *installUpdates { + apps, err := loadIndexes() + if err != nil { + return err + } + apps = filterAppsUpdates(apps, inst, device) + if len(apps) == 0 { + fmt.Fprintln(os.Stderr, "All apps up to date.") + } + return downloadAndDo(apps, device) + } + + apps, err := findApps(args) if err != nil { return err } - var toInstall []*fdroid.App + var toInstall []fdroid.App for _, app := range apps { p, e := inst[app.PackageName] if !e { @@ -58,7 +81,7 @@ func runInstall(args []string) error { return downloadAndDo(toInstall, device) } -func downloadAndDo(apps []*fdroid.App, device *adb.Device) error { +func downloadAndDo(apps []fdroid.App, device *adb.Device) error { type downloaded struct { apk *fdroid.Apk path string @@ -69,12 +92,19 @@ func downloadAndDo(apps []*fdroid.App, device *adb.Device) error { if apk == nil { return fmt.Errorf("no suitable APKs found for %s", app.PackageName) } + if *installDryRun { + fmt.Printf("install %s:%d\n", app.PackageName, apk.VersCode) + continue + } path, err := downloadApk(apk) if err != nil { return err } toInstall[i] = downloaded{apk: apk, path: path} } + if *installDryRun { + return nil + } for _, t := range toInstall { if err := installApk(device, t.apk, t.path); err != nil { return err diff --git a/show.go b/show.go index 4b06f43..d2d2ba7 100644 --- a/show.go +++ b/show.go @@ -33,7 +33,7 @@ func runShow(args []string) error { if i > 0 { fmt.Printf("\n--\n\n") } - printAppDetailed(*app) + printAppDetailed(app) } return nil } @@ -47,13 +47,13 @@ func appsMap(apps []fdroid.App) map[string]*fdroid.App { return m } -func findApps(ids []string) ([]*fdroid.App, error) { +func findApps(ids []string) ([]fdroid.App, error) { apps, err := loadIndexes() if err != nil { return nil, err } byId := appsMap(apps) - result := make([]*fdroid.App, len(ids)) + result := make([]fdroid.App, len(ids)) for i, id := range ids { var vcode = -1 j := strings.Index(id, ":") @@ -83,7 +83,7 @@ func findApps(ids []string) ([]*fdroid.App, error) { return nil, fmt.Errorf("could not find version %d for app with ID '%s'", vcode, id) } } - result[i] = app + result[i] = *app } return result, nil } diff --git a/testdata/scripts/cmds.txt b/testdata/scripts/cmds.txt index 70ce214..1e518f6 100644 --- a/testdata/scripts/cmds.txt +++ b/testdata/scripts/cmds.txt @@ -21,3 +21,6 @@ stderr '^usage: fdroidcl search .*regexp' stderr '-i.*Filter installed apps' ! fdroidcl + +! fdroidcl install -u some.app +stderr 'without arguments' diff --git a/testdata/scripts/device.txt b/testdata/scripts/device.txt index 294559f..0856851 100644 --- a/testdata/scripts/device.txt +++ b/testdata/scripts/device.txt @@ -35,6 +35,8 @@ fdroidcl search -i -q stdout 'org\.vi_server\.red_screen' fdroidcl search -u -q stdout 'org\.vi_server\.red_screen' +fdroidcl install -u -n +stdout 'install org\.vi_server\.red_screen:2' # upgrade app to version code 2 fdroidcl install org.vi_server.red_screen @@ -45,6 +47,8 @@ stdout 'Installing' # app does not show up as upgradable fdroidcl search -u -q ! stdout 'org\.vi_server\.red_screen' +fdroidcl install -u -n +! stdout 'install org\.vi_server\.red_screen:2' # nothing to install or upgrade fdroidcl install org.vi_server.red_screen