-
Notifications
You must be signed in to change notification settings - Fork 59
/
Copy pathinstall_version_switch_queue.go
76 lines (61 loc) · 1.93 KB
/
install_version_switch_queue.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
package install
import (
"fmt"
"crawshaw.io/sqlite"
"github.com/itchio/butler/butlerd/messages"
"github.com/itchio/butler/endpoints/fetch"
itchio "github.com/itchio/go-itchio"
"github.com/itchio/butler/butlerd"
"github.com/itchio/butler/cmd/operate"
"github.com/pkg/errors"
)
func InstallVersionSwitchQueue(rc *butlerd.RequestContext, params butlerd.InstallVersionSwitchQueueParams) (*butlerd.InstallVersionSwitchQueueResult, error) {
consumer := rc.Consumer
cave := operate.ValidateCave(rc, params.CaveID)
consumer.Infof("Looking for other versions of %s", operate.GameToString(cave.Game))
upload := cave.Upload
if upload == nil {
return nil, fmt.Errorf("No other versions available for %s", operate.GameToString(cave.Game))
}
var access *operate.GameAccess
rc.WithConn(func(conn *sqlite.Conn) {
access = operate.AccessForGameID(conn, cave.Game.ID)
})
client := rc.Client(access.APIKey)
buildsRes, err := client.ListUploadBuilds(itchio.ListUploadBuildsParams{
UploadID: upload.ID,
Credentials: access.Credentials,
})
if err != nil {
return nil, errors.WithStack(err)
}
var formattedCave *butlerd.Cave
rc.WithConn(func(conn *sqlite.Conn) {
formattedCave = fetch.FormatCave(conn, cave)
})
pickRes, err := messages.InstallVersionSwitchPick.Call(rc, butlerd.InstallVersionSwitchPickParams{
Cave: formattedCave,
Upload: upload,
Builds: buildsRes.Builds,
})
if err != nil {
return nil, errors.WithStack(err)
}
if pickRes.Index < 0 {
return nil, errors.WithStack(butlerd.CodeOperationAborted)
}
build := buildsRes.Builds[pickRes.Index]
_, err = InstallQueue(rc, butlerd.InstallQueueParams{
CaveID: params.CaveID,
Game: cave.Game,
Upload: cave.Upload,
Build: build,
Reason: butlerd.DownloadReasonVersionSwitch,
QueueDownload: true,
})
if err != nil {
return nil, errors.WithStack(err)
}
res := &butlerd.InstallVersionSwitchQueueResult{}
return res, nil
}