From ec56136d61c30b5be86f3e47e831400f1be6f4b6 Mon Sep 17 00:00:00 2001 From: Zander Mackie Date: Wed, 20 Feb 2019 07:12:24 -0500 Subject: [PATCH] Use closure for common pulling and tagging bevior in container creation Signed-off-by: Zander Mackie --- cli/command/container/create.go | 30 +++++++++++++----------------- 1 file changed, 13 insertions(+), 17 deletions(-) diff --git a/cli/command/container/create.go b/cli/command/container/create.go index e2043425f7..0141775b5a 100644 --- a/cli/command/container/create.go +++ b/cli/command/container/create.go @@ -223,35 +223,31 @@ func createContainer(ctx context.Context, dockerCli command.Cli, containerConfig } } - //create the container, pulling the image (or not) based on opts.pull - var response container.ContainerCreateCreatedBody - - if opts.pull == PullImageAlways { + pullAndTagImage := func() error { if err := pullImage(ctx, dockerCli, config.Image, opts.platform, stderr); err != nil { - return nil, err + return err } if taggedRef, ok := namedRef.(reference.NamedTagged); ok && trustedRef != nil { - if err := image.TagTrusted(ctx, dockerCli, trustedRef, taggedRef); err != nil { - return nil, err - } + return image.TagTrusted(ctx, dockerCli, trustedRef, taggedRef) + } + return nil + } + + if opts.pull == PullImageAlways { + if err := pullAndTagImage(); err != nil { + return nil, err } } - response, err = dockerCli.Client().ContainerCreate(ctx, config, hostConfig, networkingConfig, opts.name) - + response, err := dockerCli.Client().ContainerCreate(ctx, config, hostConfig, networkingConfig, opts.name) if err != nil { // Pull image if it does not exist locally and we have the PullImageMissing option. Default behavior. if apiclient.IsErrNotFound(err) && namedRef != nil && opts.pull == PullImageMissing { - fmt.Fprintf(stderr, "Unable to find image '%s' locally\n", reference.FamiliarString(namedRef)) // we don't want to write to stdout anything apart from container.ID - if err := pullImage(ctx, dockerCli, config.Image, opts.platform, stderr); err != nil { + fmt.Fprintf(stderr, "Unable to find image '%s' locally\n", reference.FamiliarString(namedRef)) + if err := pullAndTagImage(); err != nil { return nil, err } - if taggedRef, ok := namedRef.(reference.NamedTagged); ok && trustedRef != nil { - if err := image.TagTrusted(ctx, dockerCli, trustedRef, taggedRef); err != nil { - return nil, err - } - } var retryErr error response, retryErr = dockerCli.Client().ContainerCreate(ctx, config, hostConfig, networkingConfig, opts.name)