Browse Source

add install -u and -n flags

-u makes it upgrade all apps which can be upgraded, which is a common
operation

However, we don't want device.txt to run a full install + upgrade +
uninstall cycle again, as that's too slow. For that reason, introduce an
dry-run flag in install, so that we can check that -u would do the right
thing.

Fixes #22.
pull/44/head
Daniel Martí 7 years ago
parent
commit
bcd0811526
  1. 40
      install.go
  2. 8
      show.go
  3. 3
      testdata/scripts/cmds.txt
  4. 4
      testdata/scripts/device.txt

40
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

8
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
}

3
testdata/scripts/cmds.txt vendored

@ -21,3 +21,6 @@ stderr '^usage: fdroidcl search .*regexp'
stderr '-i.*Filter installed apps'
! fdroidcl
! fdroidcl install -u some.app
stderr 'without arguments'

4
testdata/scripts/device.txt vendored

@ -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

Loading…
Cancel
Save