1
0
mirror of https://github.com/regclient/regclient.git synced 2025-04-18 22:44:00 +03:00
regclient/scheme/reg/repo.go
Brandon Mitchell eea06e2a5c
Refactoring the type package
I feel like I need to explain, this is all to move the descriptor package.
The platform package could not use the predefined errors in types because of a circular dependency from descriptor.
The most appropriate way to reorg this is to move descriptor out of the type package since it was more complex than a self contained type.
When doing that, type aliases were needed to avoid breaking changes to existing users.
Those aliases themselves caused circular dependency loops because of the media types and errors, so those were also pulled out to separate packages.
All of the old values were aliased and deprecated, and to fix the linter, those deprecations were fixed by updating the imports... everywhere.

Signed-off-by: Brandon Mitchell <git@bmitch.net>
2024-03-04 15:43:18 -05:00

85 lines
2.1 KiB
Go

package reg
import (
"context"
"fmt"
"io"
"net/http"
"net/url"
"strconv"
"github.com/sirupsen/logrus"
"github.com/regclient/regclient/internal/reghttp"
"github.com/regclient/regclient/scheme"
"github.com/regclient/regclient/types/mediatype"
"github.com/regclient/regclient/types/repo"
)
// RepoList returns a list of repositories on a registry
// Note the underlying "_catalog" API is not supported on many cloud registries
func (reg *Reg) RepoList(ctx context.Context, hostname string, opts ...scheme.RepoOpts) (*repo.RepoList, error) {
config := scheme.RepoConfig{}
for _, opt := range opts {
opt(&config)
}
query := url.Values{}
if config.Last != "" {
query.Set("last", config.Last)
}
if config.Limit > 0 {
query.Set("n", strconv.Itoa(config.Limit))
}
headers := http.Header{
"Accept": []string{"application/json"},
}
req := &reghttp.Req{
Host: hostname,
NoMirrors: true,
APIs: map[string]reghttp.ReqAPI{
"": {
Method: "GET",
Path: "_catalog",
NoPrefix: true,
Query: query,
Headers: headers,
},
},
}
resp, err := reg.reghttp.Do(ctx, req)
if err != nil {
return nil, fmt.Errorf("failed to list repositories for %s: %w", hostname, err)
}
defer resp.Close()
if resp.HTTPResponse().StatusCode != 200 {
return nil, fmt.Errorf("failed to list repositories for %s: %w", hostname, reghttp.HTTPError(resp.HTTPResponse().StatusCode))
}
respBody, err := io.ReadAll(resp)
if err != nil {
reg.log.WithFields(logrus.Fields{
"err": err,
"host": hostname,
}).Warn("Failed to read repo list")
return nil, fmt.Errorf("failed to read repo list for %s: %w", hostname, err)
}
mt := mediatype.Base(resp.HTTPResponse().Header.Get("Content-Type"))
rl, err := repo.New(
repo.WithMT(mt),
repo.WithRaw(respBody),
repo.WithHost(hostname),
repo.WithHeaders(resp.HTTPResponse().Header),
)
if err != nil {
reg.log.WithFields(logrus.Fields{
"err": err,
"body": string(respBody),
"host": hostname,
}).Warn("Failed to unmarshal repo list")
return nil, fmt.Errorf("failed to parse repo list for %s: %w", hostname, err)
}
return rl, nil
}