forked from hashicorp/go-getter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
detect_s3.go
89 lines (73 loc) · 2.16 KB
/
detect_s3.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
package getter
import (
"fmt"
"net/url"
"strings"
)
const (
vhostFormat = ""
)
// S3Detector implements Detector to detect S3 URLs and turn
// them into URLs that the S3 getter can understand.
type S3Detector struct{}
func (d *S3Detector) Detect(src, _ string) (string, bool, error) {
if len(src) == 0 {
return "", false, nil
}
if strings.Contains(src, ".amazonaws.com/") {
return d.detectHTTP(src)
}
return "", false, nil
}
func (d *S3Detector) detectHTTP(src string) (string, bool, error) {
parts := strings.Split(src, "/")
if len(parts) < 0 {
return "", false, fmt.Errorf(
"URL is not a valid S3 URL")
}
hostParts := strings.Split(parts[0], ".")
if len(hostParts) == 3 {
return d.detectPathStyle(hostParts[0], parts[1:])
} else if len(hostParts) == 4 {
return d.detectVhostStyle(hostParts[1], hostParts[0], parts[1:])
} else {
return "", false, fmt.Errorf(
"URL is not a valid S3 URL")
}
}
func (d *S3Detector) detectPathStyle(region string, parts []string) (string, bool, error) {
urlStr := fmt.Sprintf("https://%s.amazonaws.com/%s", region, strings.Join(parts, "/"))
url, err := url.Parse(urlStr)
if err != nil {
return "", true, fmt.Errorf("error parsing GitHub URL: %s", err)
}
return "s3::" + url.String(), true, nil
}
func (d *S3Detector) detectVhostStyle(region, bucket string, parts []string) (string, bool, error) {
urlStr := fmt.Sprintf("https://%s.amazonaws.com/%s/%s", region, bucket, strings.Join(parts, "/"))
url, err := url.Parse(urlStr)
if err != nil {
return "", true, fmt.Errorf("error parsing S3 URL: %s", err)
}
return "s3::" + url.String(), true, nil
}
// func (d *S3Detector) detectSSH(src string) (string, bool, error) {
// idx := strings.Index(src, ":")
// qidx := strings.Index(src, "?")
// if qidx == -1 {
// qidx = len(src)
// }
// var u url.URL
// u.Scheme = "ssh"
// u.User = url.User("git")
// u.Host = "github.com"
// u.Path = src[idx+1 : qidx]
// if qidx < len(src) {
// q, err := url.ParseQuery(src[qidx+1:])
// if err != nil {
// return "", true, fmt.Errorf("error parsing GitHub SSH URL: %s", err)
// }
// u.RawQuery = q.Encode()
// }
// return "git::" + u.String(), true, nil
// }