diff --git a/commands/check.go b/commands/check.go index 646ffba..fa93368 100644 --- a/commands/check.go +++ b/commands/check.go @@ -87,7 +87,7 @@ func (c *Check) Execute() error { } func check(source resource.Source, from *resource.Version) (resource.CheckResponse, error) { - repo, err := name.NewRepository(source.Repository) + repo, err := source.NewRepository() if err != nil { return resource.CheckResponse{}, fmt.Errorf("resolve repository: %w", err) } diff --git a/commands/in.go b/commands/in.go index cbffa4e..3fc63d7 100644 --- a/commands/in.go +++ b/commands/in.go @@ -71,7 +71,7 @@ func (i *In) Execute() error { } } - repo, err := name.NewRepository(req.Source.Repository) + repo, err := req.Source.NewRepository() if err != nil { return fmt.Errorf("failed to resolve repository: %w", err) } @@ -124,7 +124,7 @@ func (i *In) Execute() error { func downloadWithRetry(tag name.Tag, source resource.Source, params resource.GetParams, version resource.Version, dest string, stderr io.Writer) error { fmt.Fprintf(os.Stderr, "fetching %s@%s\n", color.GreenString(source.Repository), color.YellowString(version.Digest)) - repo, err := name.NewRepository(source.Repository) + repo, err := source.NewRepository() if err != nil { return fmt.Errorf("resolve repository name: %w", err) } diff --git a/commands/out.go b/commands/out.go index cde184d..1f87ea3 100644 --- a/commands/out.go +++ b/commands/out.go @@ -71,7 +71,7 @@ func (o *Out) Execute() error { tagsToPush := []name.Tag{} - repo, err := name.NewRepository(req.Source.Repository) + repo, err := req.Source.NewRepository() if err != nil { return fmt.Errorf("could not resolve repository: %w", err) } @@ -195,12 +195,7 @@ func put(req resource.OutRequest, img v1.Image, tags []name.Tag) error { identifiers = append(identifiers, tag.Identifier()) } - var repoOpts []name.Option - if req.Source.Insecure { - repoOpts = append(repoOpts, name.Insecure) - } - - repo, err := name.NewRepository(req.Source.Repository, repoOpts...) + repo, err := req.Source.NewRepository() if err != nil { return fmt.Errorf("resolve repository name: %w", err) } @@ -265,7 +260,7 @@ func createAuth(req resource.OutRequest) *authn.Basic { func aliasesToBump(req resource.OutRequest, repo name.Repository, ver *semver.Version) ([]name.Tag, error) { variant := req.Source.Variant - repo, err := name.NewRepository(req.Source.Repository) + repo, err := req.Source.NewRepository() if err != nil { return nil, fmt.Errorf("resolve repository name: %w", err) } diff --git a/types.go b/types.go index 8565ab7..c8f6f65 100644 --- a/types.go +++ b/types.go @@ -186,6 +186,18 @@ func (source Source) AuthOptions(repo name.Repository, scopeActions []string) ([ return []remote.Option{remote.WithAuth(auth), remote.WithTransport(rt)}, nil } +func (source Source) NewRepository() (name.Repository, error) { + return name.NewRepository(source.Repository, source.RepositoryOptions()...) +} + +func (source Source) RepositoryOptions() []name.Option { + var opts []name.Option + if source.Insecure { + opts = append(opts, name.Insecure) + } + return opts +} + type ContentTrust struct { Server string `json:"server"` RepositoryKeyID string `json:"repository_key_id"`