-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
237 lines (200 loc) · 5.98 KB
/
main.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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
package main
import (
"encoding/json"
"flag"
"fmt"
"io"
"net/http"
"os"
"time"
)
type RepositoryResponse struct {
Count int `json:"count"`
Next string `json:"next"`
Previous string `json:"previous"`
Results []Repository `json:"results"`
}
type Repository struct {
Name string `json:"name"`
Namespace string `json:"namespace"`
Repository_type string `json:"repository_type"`
Status int `json:"status"`
Status_description string `json:"status_description"`
Description string `json:"description"`
Is_private bool `json:"is_private"`
Star_count int `json:"star_count"`
Pull_count int `json:"pull_count"`
Last_updated string `json:"last_updated"`
Date_registered string `json:"date_registered"`
Affiliation string `json:"affiliation"`
Media_types []string `json:"media_types"`
Content_types []string `json:"content_types"`
Categories []string `json:"categories"`
}
type TagResponse struct {
Count int `json:"count"`
Next string `json:"next"`
Previous string `json:"previous"`
Results []Tag `json:"results"`
}
type Tag struct {
Creator int `json:"creator"`
Id int `json:"id"`
Images string `json:"images"`
Last_updated string `json:"last_updated"`
Last_updater int `json:"last_updater"`
Last_updater_username string `json:"last_updater_username"`
Name string `json:"name"`
Repository int `json:"repository"`
Full_size int `json:"full_size"`
V2 bool `json:"v2"`
Tag_status string `json:"tag_status"`
Tag_last_pulled string `json:"tag_last_pulled"`
Tag_last_pushed string `json:"tag_last_pushed"`
Media_type string `json:"media_type"`
Content_type string `json:"content_type"`
}
var silent bool
func main() {
// Parse the flags
lastDays := flag.Int("ld", 0, "Filter images uploaded in the last 'n' days")
help := flag.Bool("help", false, "Show help message")
debug := flag.Bool("debug", false, "Show debug messages")
flag.BoolVar(&silent, "silent", false, "Suppress debug and error messages")
flag.Parse()
if *help {
fmt.Println("Usage: dockerhub-ls [options]")
fmt.Println("Options:")
fmt.Println(" -ld int")
fmt.Println(" Filter images uploaded in the last 'n' days (default 0, which means all images)")
fmt.Println(" -help")
fmt.Println(" Show this help message")
fmt.Println(" -debug")
fmt.Println(" Show debug messages")
fmt.Println(" -silent")
fmt.Println(" Suppress debug and error messages")
return
}
// Check for stdin input
stat, _ := os.Stdin.Stat()
if (stat.Mode() & os.ModeCharDevice) != 0 {
fmt.Fprintln(os.Stderr, "No users detected. Hint: cat users.txt | dockerhub-ls")
os.Exit(1)
}
// Read stdin
var users []string
for {
var user string
_, err := fmt.Scan(&user)
if err != nil {
break
}
users = append(users, user)
}
for _, user := range users {
check_user_exist(user, *lastDays, *debug)
}
}
func get_tags(username string, repository string, lastDays int, debug bool) {
docker_hub_tags_url := fmt.Sprintf("https://hub.docker.com/v2/namespaces/%s/repositories/%s/tags", username, repository)
resp, err := makeRequestWithRetries(docker_hub_tags_url)
if err != nil {
log("Error checking for tags")
return
}
if debug {
log(fmt.Sprintf("Get tags - Status code: %v", resp.StatusCode))
}
if resp.StatusCode == 200 {
resp_body, err := io.ReadAll(resp.Body)
if err != nil {
log("Error reading tag response")
return
}
if debug {
log(fmt.Sprintf("Response body: %s", resp_body))
}
var tag_resp TagResponse
json.Unmarshal(resp_body, &tag_resp)
if tag_resp.Count != 0 {
for s := range tag_resp.Results {
lastUpdated, err := time.Parse(time.RFC3339, tag_resp.Results[s].Last_updated)
if err != nil {
log(fmt.Sprintf("Error parsing time: %v", err))
continue
}
if lastDays == 0 || time.Since(lastUpdated) <= time.Duration(lastDays)*24*time.Hour {
fmt.Printf("%s/%s:%s\n", username, repository, tag_resp.Results[s].Name)
}
}
}
// else {
// // Print the image without a tag if none exist
// fmt.Printf("%s/%s\n", username, repository)
// }
}
}
func get_repositories(username string, lastDays int, debug bool) {
docker_hub_repositories_url := fmt.Sprintf("https://hub.docker.com/v2/namespaces/%s/repositories", username)
resp, err := makeRequestWithRetries(docker_hub_repositories_url)
if err != nil {
log("Error checking for repositories")
return
}
if debug {
log(fmt.Sprintf("Get repositories - Status code: %v", resp.StatusCode))
}
if resp.StatusCode == 200 {
resp_body, err := io.ReadAll(resp.Body)
if err != nil {
log("Error reading repository response")
return
}
if debug {
log(fmt.Sprintf("Response body: %s", resp_body))
}
var repo_resp RepositoryResponse
json.Unmarshal(resp_body, &repo_resp)
if repo_resp.Count != 0 {
for s := range repo_resp.Results {
get_tags(username, repo_resp.Results[s].Name, lastDays, debug)
}
}
}
}
func check_user_exist(username string, lastDays int, debug bool) {
docker_hub_user_base_url := "https://hub.docker.com/u/"
resp, err := makeRequestWithRetries(docker_hub_user_base_url + username)
if err != nil {
log("Error checking for user existence")
return
}
if debug {
log(fmt.Sprintf("Check if user exist - Status code: %v", resp.StatusCode))
}
if resp.StatusCode == 200 {
get_repositories(username, lastDays, debug)
}
}
func makeRequestWithRetries(url string) (*http.Response, error) {
var resp *http.Response
var err error
delay := time.Second
for {
resp, err = http.Get(url)
if err != nil {
return nil, err
}
if resp.StatusCode != 429 {
break
}
time.Sleep(delay)
delay *= 2
}
return resp, nil
}
func log(message string) {
if !silent {
fmt.Println(message)
}
}