Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cli/trust: TagTrusted: lazily request APIClient #5905

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions cli/command/container/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import (
"github.com/docker/cli/cli/command/image"
"github.com/docker/cli/cli/internal/jsonstream"
"github.com/docker/cli/cli/streams"
"github.com/docker/cli/cli/trust"
"github.com/docker/cli/opts"
"github.com/docker/docker/api/types/container"
imagetypes "github.com/docker/docker/api/types/image"
Expand Down Expand Up @@ -243,7 +242,7 @@ func createContainer(ctx context.Context, dockerCli command.Cli, containerCfg *c
return err
}
if taggedRef, ok := namedRef.(reference.NamedTagged); ok && trustedRef != nil {
return trust.TagTrusted(ctx, dockerCli.Client(), dockerCli.Err(), trustedRef, taggedRef)
return tagTrusted(ctx, dockerCli, trustedRef, taggedRef)
}
return nil
}
Expand Down
16 changes: 16 additions & 0 deletions cli/command/container/trust.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package container

import (
"context"
"fmt"

"github.com/distribution/reference"
"github.com/docker/cli/cli/command"
)

// tagTrusted tags a trusted ref. It is a shallow wrapper around APIClient.ImageTag
// that updates the given image references to their familiar format for printing.
func tagTrusted(ctx context.Context, cli command.Cli, trustedRef reference.Canonical, ref reference.NamedTagged) error {
_, _ = fmt.Fprintf(cli.Err(), "Tagging %s as %s\n", reference.FamiliarString(trustedRef), reference.FamiliarString(ref))
return cli.Client().ImageTag(ctx, trustedRef.String(), ref.String())
}
3 changes: 1 addition & 2 deletions cli/command/image/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import (
"github.com/docker/cli/cli/command/image/build"
"github.com/docker/cli/cli/internal/jsonstream"
"github.com/docker/cli/cli/streams"
"github.com/docker/cli/cli/trust"
"github.com/docker/cli/opts"
"github.com/docker/docker/api"
"github.com/docker/docker/api/types"
Expand Down Expand Up @@ -407,7 +406,7 @@ func runBuild(ctx context.Context, dockerCli command.Cli, options buildOptions)
// Since the build was successful, now we must tag any of the resolved
// images from the above Dockerfile rewrite.
for _, resolved := range resolvedTags {
if err := trust.TagTrusted(ctx, dockerCli.Client(), dockerCli.Err(), resolved.digestRef, resolved.tagRef); err != nil {
if err := tagTrusted(ctx, dockerCli, resolved.digestRef, resolved.tagRef); err != nil {
return err
}
}
Expand Down
18 changes: 10 additions & 8 deletions cli/command/image/trust.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,11 +104,7 @@ func trustedPull(ctx context.Context, cli command.Cli, imgRefAndAuth trust.Image
return err
}

// Use familiar references when interacting with client and output
familiarRef := reference.FamiliarString(tagged)
trustedFamiliarRef := reference.FamiliarString(trustedRef)
_, _ = fmt.Fprintf(cli.Err(), "Tagging %s as %s\n", trustedFamiliarRef, familiarRef)
if err := cli.Client().ImageTag(ctx, trustedFamiliarRef, familiarRef); err != nil {
if err := tagTrusted(ctx, cli, trustedRef, tagged); err != nil {
return err
}
}
Expand Down Expand Up @@ -230,12 +226,18 @@ func convertTarget(t client.Target) (target, error) {
}

// TagTrusted tags a trusted ref. It is a shallow wrapper around APIClient.ImageTag
// that updates the given image references to their familiar format for tagging
// and printing.
// that updates the given image references to their familiar format for printing.
//
// Deprecated: this function was only used internally, and will be removed in the next release.
func TagTrusted(ctx context.Context, cli command.Cli, trustedRef reference.Canonical, ref reference.NamedTagged) error {
return trust.TagTrusted(ctx, cli.Client(), cli.Err(), trustedRef, ref)
return tagTrusted(ctx, cli, trustedRef, ref)
}

// tagTrusted tags a trusted ref. It is a shallow wrapper around APIClient.ImageTag
// that updates the given image references to their familiar format for printing.
func tagTrusted(ctx context.Context, cli command.Cli, trustedRef reference.Canonical, ref reference.NamedTagged) error {
_, _ = fmt.Fprintf(cli.Err(), "Tagging %s as %s\n", reference.FamiliarString(trustedRef), reference.FamiliarString(ref))
return cli.Client().ImageTag(ctx, trustedRef.String(), ref.String())
}

// AuthResolver returns an auth resolver function from a command.Cli
Expand Down
13 changes: 7 additions & 6 deletions cli/trust/trust_tag.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,15 @@ import (
"github.com/docker/docker/client"
)

type APIClientProvider interface {
Client() client.APIClient
}

// TagTrusted tags a trusted ref. It is a shallow wrapper around [client.Client.ImageTag]
// that updates the given image references to their familiar format for tagging
// and printing.
func TagTrusted(ctx context.Context, apiClient client.ImageAPIClient, out io.Writer, trustedRef reference.Canonical, ref reference.NamedTagged) error {
func TagTrusted(ctx context.Context, cli APIClientProvider, out io.Writer, trustedRef reference.Canonical, ref reference.NamedTagged) error {
// Use familiar references when interacting with client and output
familiarRef := reference.FamiliarString(ref)
trustedFamiliarRef := reference.FamiliarString(trustedRef)

_, _ = fmt.Fprintf(out, "Tagging %s as %s\n", trustedFamiliarRef, familiarRef)
return apiClient.ImageTag(ctx, trustedFamiliarRef, familiarRef)
_, _ = fmt.Fprintf(out, "Tagging %s as %s\n", reference.FamiliarString(trustedRef), reference.FamiliarString(ref))
return cli.Client().ImageTag(ctx, trustedRef.String(), ref.String())
}
Loading