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
|
package registry
import (
"encoding/json"
"fmt"
"io"
"net/http"
)
type RegistryClient struct {
URL string
Username string
Password string
}
type CatalogResponse struct {
Repositories []string `json:"repositories"`
}
func (c *RegistryClient) ListRepositories() ([]string, error) {
repos := []string{}
last := ""
pageSize := 100
for {
url := fmt.Sprintf("%s/v2/_catalog?n=%d", c.URL, pageSize)
if last != "" {
url += "&last=" + last
}
req, _ := http.NewRequest("GET", url, nil)
if c.Username != "" {
req.SetBasicAuth(c.Username, c.Password)
}
resp, err := http.DefaultClient.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
body, _ := io.ReadAll(resp.Body)
return nil, fmt.Errorf("registry returned %d: %s", resp.StatusCode, body)
}
var catalog CatalogResponse
if err := json.NewDecoder(resp.Body).Decode(&catalog); err != nil {
return nil, err
}
repos = append(repos, catalog.Repositories...)
if len(catalog.Repositories) < pageSize {
break
}
last = catalog.Repositories[len(catalog.Repositories)-1]
}
return repos, nil
}
|