This repository has been archived by the owner on Jun 22, 2024. It is now read-only.
forked from SeleniumHQ/docker-selenium
-
Notifications
You must be signed in to change notification settings - Fork 26
/
get-all-related-tags.go
179 lines (154 loc) · 4.07 KB
/
get-all-related-tags.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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
"strconv"
"strings"
)
type Latest struct {
Images []struct {
Architecture string `json:"architecture"`
Digest string `json:"digest"`
}
}
type Result struct {
Results []struct {
Images []struct {
Digest string `json:"digest"`
}
Name string `json:"name"`
}
}
func main() {
argLen := len(os.Args)
if argLen < 2 {
showUsage()
os.Exit(1)
}
url := os.Args[1] // https://hub.docker.com/v2/repositories/selenium/standalone-chrome/tags/latest/
var inputArch string
if argLen >= 3 {
inputArch = os.Args[2]
}
if inputArch == "" {
fmt.Println("Get related tags using sha256 for random architecture...")
} else {
fmt.Println("Get related tags using sha256 for " + inputArch + " architecture...")
}
arch, tagDigest := getDigestForTag(url, inputArch)
fmt.Println("getting related tags for " + arch + " digest = " + tagDigest)
var allTagsUrl string
var theseRelatedTags []string
var relatedTagsStr string
allTagsUrl = getAllTagsUrl(url)
theseRelatedTags = getRelatedTagsFromDigest(allTagsUrl, tagDigest)
relatedTagsStr = strings.Join(theseRelatedTags, " ")
for next := true; next; next = (len(theseRelatedTags) != 0) {
allTagsUrl = getAllTagsUrlForNextPage(allTagsUrl)
theseRelatedTags = getRelatedTagsFromDigest(allTagsUrl, tagDigest)
relatedTagsStr = relatedTagsStr + " " + strings.Join(theseRelatedTags, " ")
// fmt.Println(theseRelatedTags)
}
fmt.Println(relatedTagsStr)
}
func getDigestForTag(url string, inputArch string) (string, string) {
resp, err := http.Get(url)
if err != nil {
log.Fatalln(err)
}
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Fatalln(err)
}
var jsonResp Latest
sb := string(body)
json.Unmarshal([]byte(sb), &jsonResp)
var digest string
var arch string
if inputArch != "" {
for _, image := range jsonResp.Images {
if inputArch == image.Architecture {
digest = image.Digest
arch = image.Architecture
}
}
} else {
for _, image := range jsonResp.Images {
digest = image.Digest
arch = image.Architecture
}
}
return arch, digest
}
func getAllTagsUrlForNextPage(specificTagUrl string) string {
urlPathArr := strings.Split(specificTagUrl, "?")
var pageNum string
if len(urlPathArr) > 1 {
pageKeyVal := strings.Split(urlPathArr[1], "=")
pageNumInt, err := strconv.Atoi(pageKeyVal[1])
if err != nil {
log.Fatalln(err)
}
pageNum = "?page=" + strconv.Itoa(pageNumInt+1)
} else {
pageNum = "?page=1"
}
return urlPathArr[0] + pageNum
}
func getAllTagsUrl(specificTagUrl string) string {
urlPathArr := strings.Split(specificTagUrl, "?")
var pageNum string
if len(urlPathArr) > 1 {
pageNum = "?" + urlPathArr[1]
} else {
pageNum = "?page=1"
}
specificTagUrlWithoutPage := urlPathArr[0]
urlArr := strings.Split(specificTagUrlWithoutPage, "/")
// fmt.Println(urlArr[len(urlArr)-1])
if urlArr[len(urlArr)-1] == "" {
urlArr[len(urlArr)-1] = ""
urlArr[len(urlArr)-2] = ""
} else {
urlArr[len(urlArr)-1] = ""
}
urlArr = urlArr[:len(urlArr)-1]
allTagsUrl := strings.Join(urlArr, "/") // // https://hub.docker.com/v2/repositories/selenium/standalone-chrome/tags/
return allTagsUrl + pageNum
}
func getRelatedTagsFromDigest(allTagsUrl string, digest string) []string {
resp, err := http.Get(allTagsUrl)
if err != nil {
log.Fatalln(err)
}
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Fatalln(err)
}
var jsonResp Result
sb := string(body)
json.Unmarshal([]byte(sb), &jsonResp)
var relatedTags []string
for _, results := range jsonResp.Results {
for _, image := range results.Images {
imageDigest := image.Digest
if imageDigest == digest {
relatedTags = append(relatedTags, results.Name)
}
}
}
return relatedTags
}
func showUsage() {
fmt.Println(`Usage:
get-all-related-tags TAG_URL [ARCH]
TAG_URL -> URL for a container image manifest (Required)
ARCH -> Architecture to use to obtain sha256 (Optional)
Example Usage:
$ get-all-related-tags https://hub.docker.com/v2/repositories/selenium/standalone-chrome/tags/latest/
`)
}