From e37d814ce96b01393a400c081666ea1cca2eb8bd Mon Sep 17 00:00:00 2001 From: Sebastiaan van Stijn Date: Wed, 5 Mar 2025 16:16:01 +0100 Subject: [PATCH] cli/command/image: deprecate TagTrusted, move to cli/trust This function was shared between "image" and "container" packages, all of which needed the trust package, so move it there instead. Signed-off-by: Sebastiaan van Stijn --- cli/command/container/create.go | 3 ++- cli/command/image/build.go | 3 ++- cli/command/image/trust.go | 20 +++++++++++--------- cli/trust/trust_tag.go | 22 ++++++++++++++++++++++ 4 files changed, 37 insertions(+), 11 deletions(-) create mode 100644 cli/trust/trust_tag.go diff --git a/cli/command/container/create.go b/cli/command/container/create.go index 42dab18983..ac8e6083fe 100644 --- a/cli/command/container/create.go +++ b/cli/command/container/create.go @@ -15,6 +15,7 @@ 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" @@ -242,7 +243,7 @@ func createContainer(ctx context.Context, dockerCli command.Cli, containerCfg *c return err } if taggedRef, ok := namedRef.(reference.NamedTagged); ok && trustedRef != nil { - return image.TagTrusted(ctx, dockerCli, trustedRef, taggedRef) + return trust.TagTrusted(ctx, dockerCli.Client(), dockerCli.Err(), trustedRef, taggedRef) } return nil } diff --git a/cli/command/image/build.go b/cli/command/image/build.go index f8355fbe96..039e7619ba 100644 --- a/cli/command/image/build.go +++ b/cli/command/image/build.go @@ -22,6 +22,7 @@ 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" @@ -406,7 +407,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 := TagTrusted(ctx, dockerCli, resolved.digestRef, resolved.tagRef); err != nil { + if err := trust.TagTrusted(ctx, dockerCli.Client(), dockerCli.Err(), resolved.digestRef, resolved.tagRef); err != nil { return err } } diff --git a/cli/command/image/trust.go b/cli/command/image/trust.go index aef21d47d7..b4a6d84a00 100644 --- a/cli/command/image/trust.go +++ b/cli/command/image/trust.go @@ -104,7 +104,11 @@ func trustedPull(ctx context.Context, cli command.Cli, imgRefAndAuth trust.Image return err } - if err := TagTrusted(ctx, cli, trustedRef, tagged); err != nil { + // 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 { return err } } @@ -225,15 +229,13 @@ func convertTarget(t client.Target) (target, error) { }, nil } -// TagTrusted tags a trusted 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 tagging +// and 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 { - // Use familiar references when interacting with client and output - familiarRef := reference.FamiliarString(ref) - trustedFamiliarRef := reference.FamiliarString(trustedRef) - - _, _ = fmt.Fprintf(cli.Err(), "Tagging %s as %s\n", trustedFamiliarRef, familiarRef) - - return cli.Client().ImageTag(ctx, trustedFamiliarRef, familiarRef) + return trust.TagTrusted(ctx, cli.Client(), cli.Err(), trustedRef, ref) } // AuthResolver returns an auth resolver function from a command.Cli diff --git a/cli/trust/trust_tag.go b/cli/trust/trust_tag.go new file mode 100644 index 0000000000..053f9317d1 --- /dev/null +++ b/cli/trust/trust_tag.go @@ -0,0 +1,22 @@ +package trust + +import ( + "context" + "fmt" + "io" + + "github.com/distribution/reference" + "github.com/docker/docker/client" +) + +// 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 { + // 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) +}