-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathclient.go
52 lines (50 loc) · 1.44 KB
/
client.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
package main
import (
"bufio"
"errors"
"io"
"net/http"
"strings"
)
func getDockerfile(imageId string) (io.Reader, string, error) {
url := ""
repository := strings.Split(imageId, ":")
if len(repository) > 1 || strings.Index(imageId, "/") < 0 {
if strings.Index(imageId, "/") < 0 {
repository = append(repository, "latest")
}
resp, err := http.Get("https://raw.githubusercontent.com/docker-library/official-images/master/library/" + repository[0])
if err == nil {
scanner := bufio.NewScanner(resp.Body)
scanner.Split(bufio.ScanLines)
for scanner.Scan() {
image := strings.Split(scanner.Text(), " ")
if image[0] == repository[1]+":" {
r := strings.NewReplacer("git://github.com", "https://raw.githubusercontent.com", "@", "/")
for len(image[1]) == 0 {
image = append(image[:0],image[1:]...)
}
url = r.Replace(image[1])
if len(image) > 2 {
url += "/" + image[2] + "/Dockerfile"
} else {
url += "/Dockerfile"
}
}
}
if url == "" {
return nil, url, errors.New("Failed to fetch " + imageId)
}
} else {
return nil, url, errors.New("Failed to fetch " + imageId)
}
} else {
url = "https://registry.hub.docker.com/u/" + imageId + "/dockerfile/raw"
}
resp, err := http.Get(url)
if err == nil && strings.Index(resp.Header["Content-Type"][0], "text/plain") >= 0 {
return resp.Body, url, err
} else {
return nil, url, errors.New("Failed to fetch " + imageId)
}
}