-
Notifications
You must be signed in to change notification settings - Fork 16
/
apps.go
73 lines (62 loc) · 1.92 KB
/
apps.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
package apps
import (
"fmt"
"net/url"
"os"
"regexp"
"strings"
"github.com/saucelabs/saucectl/internal/msg"
)
var (
reFileID = regexp.MustCompile(`(storage:(//)?)?(?P<fileID>[a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12})$`)
reFilePattern = regexp.MustCompile(`^(storage:filename=)(?P<filename>[\S][\S ]+(\.ipa|\.apk))$`)
reHTTPSchemePattern = regexp.MustCompile(`(?i)^https?`)
)
func hasValidExtension(file string, exts []string) bool {
for _, ext := range exts {
if strings.HasSuffix(file, ext) {
return true
}
}
return false
}
// IsRemote (naively) checks if the given string is a remote url
func IsRemote(name string) bool {
parsedURL, err := url.Parse(name)
if err != nil {
return false
}
return reHTTPSchemePattern.MatchString(parsedURL.Scheme) && parsedURL.Host != ""
}
// IsStorageReference checks if a link is an entry of app-storage.
func IsStorageReference(link string) bool {
return reFileID.MatchString(link) || reFilePattern.MatchString(link)
}
// StandardizeReferenceLink standardize the provided storageID reference to make it work for VMD and RDC.
func StandardizeReferenceLink(storageRef string) string {
if reFileID.MatchString(storageRef) {
if !strings.HasPrefix(storageRef, "storage:") {
return fmt.Sprintf("storage:%s", storageRef)
}
if strings.HasPrefix(storageRef, "storage://") {
return strings.Replace(storageRef, "storage://", "storage:", 1)
}
}
return storageRef
}
// Validate validates that the apps is valid (storageID / File / URL).
func Validate(kind, app string, validExt []string) error {
if IsStorageReference(app) {
return nil
}
if IsRemote(app) {
return nil
}
if !hasValidExtension(app, validExt) {
return fmt.Errorf("invalid %s file: %s, make sure extension is one of the following: %s", kind, app, strings.Join(validExt, ", "))
}
if _, err := os.Stat(app); err == nil {
return nil
}
return fmt.Errorf(msg.FileNotFound, app)
}