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 }