-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathinstall.go
263 lines (249 loc) · 6.98 KB
/
install.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
// Copyright (c) 2015, Daniel Martí <[email protected]>
// See LICENSE for licensing information
package main
import (
"encoding/csv"
"fmt"
"io"
"os"
"strconv"
"strings"
"mvdan.cc/fdroidcl/adb"
"mvdan.cc/fdroidcl/fdroid"
)
var cmdInstall = &Command{
UsageLine: "install [<appid...>]",
Short: "Install or upgrade apps",
Long: `
Install or upgrade apps. When given no arguments, it reads a comma-separated
list of apps to install from standard input, like:
packageName,versionCode,versionName
foo.bar,120,1.2.0
`[1:],
}
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")
installUpdatesExclude = cmdInstall.Fset.String("e", "", "Exclude apps from upgrading (comma-separated list)")
installSkipError = cmdInstall.Fset.Bool("s", false, "Skip to the next application if a download or install error occurs")
installUser = cmdInstall.Fset.String("user", "", `Install/upgrade for specified user <USER_ID|current|all>
default: installs app for the current user; upgrades apps of all users and installs the new version only for the users of the old version
USER_ID: installs app for USER_ID; upgrades only apps of USER_ID and installs the new version only for USER_ID
current: installs app for the current user; upgrades only apps of the current user and installs the new version only for the current user
all: installs app for all users; upgrades apps of all users and installs the new version for all users`)
)
func init() {
cmdInstall.Run = runInstall
}
func runInstall(args []string) error {
if *installUpdates && len(args) > 0 {
return fmt.Errorf("-u can only be used without arguments")
}
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 {
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 *installUser == "current" || (*installUser == "" && !*installUpdates) {
uid, err := device.CurrentUserId()
if err != nil {
return err
}
*installUser = strconv.Itoa(uid)
}
if *installUpdates {
apps, err := loadIndexes()
if err != nil {
return err
}
var filterUser *int
if *installUser == "all" || *installUser == "" {
filterUser = nil
} else {
n, err := strconv.Atoi(*installUser)
if err != nil {
return err
}
filterUser = &n
}
apps = filterAppsUpdates(apps, inst, device, filterUser)
if *installUpdatesExclude != "" {
excludeApps := strings.Split(*installUpdatesExclude, ",")
installApps := make([]fdroid.App, 0)
for _, app := range apps {
shouldExclude := false
for _, exclude := range excludeApps {
if app.PackageName == exclude {
shouldExclude = true
break
}
}
if shouldExclude {
continue
}
installApps = append(installApps, app)
}
apps = installApps
}
if len(apps) == 0 {
fmt.Fprintln(os.Stderr, "All apps up to date.")
}
return downloadAndDo(apps, inst, device)
}
if len(args) == 0 {
// The CSV input is as follows:
//
// packageName,versionCode,versionName
// foo.bar,120,1.2.0
// ...
r := csv.NewReader(os.Stdin)
r.FieldsPerRecord = 3
r.Read()
for {
record, err := r.Read()
if err == io.EOF {
break
}
if err != nil {
return fmt.Errorf("error parsing CSV: %v", err)
}
// convert "foo.bar,120" into "foo.bar:120" for findApps
args = append(args, record[0]+":"+record[1])
}
}
apps, err := findApps(args)
if err != nil {
return err
}
var toInstall []fdroid.App
for _, app := range apps {
p, e := inst[app.PackageName]
if !e {
// installing an app from scratch
toInstall = append(toInstall, app)
continue
}
suggested := app.SuggestedApk(device)
if suggested == nil {
return fmt.Errorf("no suitable APKs found for %s", app.PackageName)
}
if p.VersCode >= suggested.VersCode {
if !(*installUser == "all" && len(p.NotInstalledForUsers) > 0) { // ensure that it can't install for other user
okSkip := *installUser == "all"
if !okSkip {
n, err := strconv.Atoi(*installUser)
if err != nil {
return err
}
isInstalledForUser := false
for _, uid := range p.InstalledForUsers {
if uid == n {
isInstalledForUser = true
break
}
}
if isInstalledForUser {
okSkip = true
}
}
if okSkip {
fmt.Printf("%s is up to date\n", app.PackageName)
// app is already up to date
continue
}
}
}
// upgrading an existing app
toInstall = append(toInstall, app)
}
return downloadAndDo(toInstall, inst, device)
}
func downloadAndDo(apps []fdroid.App, installed map[string]adb.Package, device *adb.Device) error {
type downloaded struct {
apk *fdroid.Apk
app fdroid.App
path string
}
toInstall := make([]downloaded, 0)
for _, app := range apps {
apk := app.SuggestedApk(device)
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 {
if *installSkipError {
fmt.Printf("Downloading %s failed, skipping...\n", app.PackageName)
continue
}
return err
}
toInstall = append(toInstall, downloaded{apk: apk, app: app, path: path})
}
if *installDryRun {
return nil
}
for _, t := range toInstall {
var installedPkg *adb.Package = nil
if p, e := installed[t.app.PackageName]; e {
installedPkg = &p
}
if err := installApk(device, t.apk, installedPkg, t.path); err != nil {
if *installSkipError {
fmt.Printf("Installing %s failed, skipping...\n", t.apk.AppID)
continue
}
return err
}
}
return nil
}
func installApk(device *adb.Device, apk *fdroid.Apk, devicePkg *adb.Package, path string) error {
fmt.Printf("Installing %s\n", apk.AppID)
userId := "all"
if *installUser != "all" {
if *installUpdates && *installUser == "" {
if devicePkg == nil {
return fmt.Errorf("failed to get device package although it should be installed (please report this error)")
}
if len((*devicePkg).InstalledForUsers) > 0 {
userId = strconv.Itoa((*devicePkg).InstalledForUsers[0])
}
} else {
userId = *installUser
}
}
if userId == "all" {
if err := device.Install(path); err != nil {
return fmt.Errorf("could not install %s: %v", apk.AppID, err)
}
} else {
if err := device.InstallUser(path, userId); err != nil {
return fmt.Errorf("could not install %s: %v", apk.AppID, err)
}
}
return nil
}