This repository was archived by the owner on Jul 27, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathdockerhub.go
147 lines (131 loc) · 4.46 KB
/
dockerhub.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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"sort"
"strings"
"github.com/dustin/go-humanize"
"github.com/hashicorp/go-version"
"github.com/k0kubun/pp"
)
// RequestError interface
type RequestError interface {
Error() string
}
type tagInfo struct {
Name string `json:"name"`
FullSize uint64 `json:"full_size"`
HumanSize string `json:"human_size"`
Version *version.Version `json:"version"`
VersionNumber string `json:"version-number"`
Architecture string `json:"arch"`
Os string `json:"os"`
}
type TagsInfoResponse struct {
Count int `json:"count"`
Next interface{} `json:"next"`
Previous interface{} `json:"previous"`
Results []TagInfoResult `json:"results"`
}
type TagInfoResult struct {
Creator float64 `json:"creator"`
FullSize uint64 `json:"full_size"`
ID float64 `json:"id"`
ImageID interface{} `json:"image_id"`
Images []TagInfoResultImage `json:"images"`
LastUpdated string `json:"last_updated"`
LastUpdater float64 `json:"last_updater"`
LastUpdaterUsername string `json:"last_updater_username"`
Name string `json:"name"`
Repository float64 `json:"repository"`
V2 bool `json:"v2"`
}
type TagInfoResultImage struct {
Architecture string `json:"architecture"`
Digest string `json:"digest"`
Features string `json:"features"`
Os string `json:"os"`
OsFeatures string `json:"os_features"`
OsVersion interface{} `json:"os_version"`
Size float64 `json:"size"`
Variant interface{} `json:"variant"`
}
func getImagesInfo(dockerRepository, vcsRepository string) (string, error) {
r, err := request(fmt.Sprintf("https://hub.docker.com/v2/repositories/%s/tags/?page_size=100", dockerRepository))
if err != nil || r.StatusCode != 200 {
fmt.Printf(" [%s]\n", ("Failed"))
fmt.Println("Failed to connect to docker-hub repository.")
return "", err
} else {
defer r.Body.Close()
}
byt := readResponseBody(r)
var tagsInfoResponse TagsInfoResponse
if err := json.Unmarshal(byt, &tagsInfoResponse); err != nil {
return "", err
}
var tagInfos []*tagInfo
for _, t := range tagsInfoResponse.Results {
ti := &tagInfo{
Name: t.Name,
HumanSize: humanize.Bytes(t.FullSize),
FullSize: t.FullSize,
Architecture: t.Images[0].Architecture,
Os: t.Images[0].Os,
}
v, _ := version.NewVersion(t.Name)
if v != nil {
ti.Version = v
ti.VersionNumber = v.String()
}
tagInfos = append(tagInfos, ti)
}
sort.Slice(tagInfos, func(i, j int) bool { return tagInfos[i].VersionNumber < tagInfos[j].VersionNumber })
if cfg.DebugMode {
pp.Println(tagInfos)
}
branch, err := getCurrentBranch(".")
if err != nil {
return "", err
}
var dockerImageTable string
dockerImageTable = fmt.Sprint("| Image | Size | Arch | Os | Link |\n")
dockerImageTable += fmt.Sprint("|----------|:-------------:|------|------|------|\n")
for _, tagInfoResponse := range tagInfos {
linkVersion := strings.Replace(tagInfoResponse.Name, "-", "/", -1)
link := fmt.Sprintf("[`./dockerfiles/%s`](https://github.com/%s/tree/%s/dockerfiles/%s/)", linkVersion, vcsRepository, branch, linkVersion)
dockerImageTable += fmt.Sprint("| docker pull ", dockerRepository+":"+tagInfoResponse.Name, "|**", tagInfoResponse.HumanSize, "**|", tagInfoResponse.Architecture, "|", tagInfoResponse.Os, "|", link, "|\n")
}
return dockerImageTable, nil
}
// Request makes an HTTP request
func request(target string) (*http.Response, RequestError) {
request, err := http.NewRequest("GET", target, nil)
if err != nil {
return nil, err
}
// request.Header.Set("User-Agent", userAgent)
client := &http.Client{}
return client.Do(request)
}
// ReadResponseBody reads response body and return string
func readResponseBody(response *http.Response) []byte {
bodyBytes, err := ioutil.ReadAll(response.Body)
if err != nil {
panic(err)
}
return bodyBytes
}
// hasElement reports whether elements is within array.
func hasElement(array []string, targets ...string) (bool, int) {
for index, item := range array {
for _, target := range targets {
if item == target {
return true, index
}
}
}
return false, -1
}