-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathmobile_upload.go
107 lines (89 loc) · 2.96 KB
/
mobile_upload.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
package main
import (
"fmt"
"os"
"path/filepath"
"strconv"
"strings"
"github.com/rainforestapp/rainforest-cli/rainforest"
"github.com/urfave/cli"
)
// mobileUploadAPI is part of the API connected to mobile uploads
type mobileUploadAPI interface {
GetPresignedPOST(fileExt string, siteID int, environmentID int, appSlot int) (*rainforest.RFPresignedPostData, error)
UploadToS3(postData *rainforest.RFPresignedPostData, filePath string) error
UpdateURL(siteID int, environmentID int, appSlot int, newURL string) error
}
// uploadMobileApp takes a path to a mobile app file and uploads it to S3 then sets
// the site-id specified's URL to the magic url
func uploadMobileApp(api mobileUploadAPI, filePath string, siteID int, environmentID int, appSlot int) error {
presignedPostData, err := api.GetPresignedPOST(filepath.Ext(filePath), siteID, environmentID, appSlot)
if err != nil {
return err
}
err = api.UploadToS3(presignedPostData, filePath)
if err != nil {
return err
}
err = api.UpdateURL(siteID, environmentID, appSlot, presignedPostData.RainforestURL)
if err != nil {
return err
}
return nil
}
func isAllowedExtension(extension string) bool {
switch extension {
case ".apk", ".aab", ".ipa", ".zip", ".gz", ".tar.gz":
return true
}
return false
}
const allowedExtensionsPretty = ".apk, .aab, .ipa, .zip, .gz, .tar.gz"
// mobileAppUpload is a wrapper around uploadMobileApp to function with mobile-upload cli command
func mobileAppUpload(c cliContext, api mobileUploadAPI) error {
// Get the filepath from the arg
filePath := c.Args().First()
if filePath == "" {
return cli.NewExitError("Mobile app file path not specified", 1)
}
// verify extension
fileExt := strings.ToLower(filepath.Ext(filePath))
if !isAllowedExtension(fileExt) {
return cli.NewExitError(fmt.Sprintf("Invalid file extension. - %v. Allowed Extensions: %v", fileExt, allowedExtensionsPretty), 1)
}
siteIDString := c.String("site-id")
if siteIDString == "" {
return cli.NewExitError("site-id flag required", 1)
}
siteID, err := strconv.Atoi(siteIDString)
if err != nil {
return cli.NewExitError("site-id must be an integer", 1)
}
envIDstring := c.String("environment-id")
if envIDstring == "" {
return cli.NewExitError("environment-id flag required", 1)
}
environmentID, err := strconv.Atoi(envIDstring)
if err != nil {
return cli.NewExitError("environment-id must be an integer", 1)
}
appSlot := 1 // Default to 1, optional param
appSlotString := c.String("app-slot")
if appSlotString != "" {
appSlot, err = strconv.Atoi(appSlotString)
if err != nil || appSlot < 1 || appSlot > 100 {
return cli.NewExitError("app-slot must be an integer (1 to 100)", 1)
}
}
// Open app and return early with an error if we fail
f, err := os.Open(filePath)
if err != nil {
return cli.NewExitError(err.Error(), 1)
}
defer f.Close()
err = uploadMobileApp(api, filePath, siteID, environmentID, appSlot)
if err != nil {
return cli.NewExitError(err.Error(), 1)
}
return nil
}