github.com/itscaro/cli@v0.0.0-20190705081621-c9db0fe93829/cli/command/image/tag.go (about)

     1  package image
     2  
     3  import (
     4  	"context"
     5  
     6  	"github.com/docker/cli/cli"
     7  	"github.com/docker/cli/cli/command"
     8  	"github.com/spf13/cobra"
     9  )
    10  
    11  type tagOptions struct {
    12  	image string
    13  	name  string
    14  }
    15  
    16  // NewTagCommand creates a new `docker tag` command
    17  func NewTagCommand(dockerCli command.Cli) *cobra.Command {
    18  	var opts tagOptions
    19  
    20  	cmd := &cobra.Command{
    21  		Use:   "tag SOURCE_IMAGE[:TAG] TARGET_IMAGE[:TAG]",
    22  		Short: "Create a tag TARGET_IMAGE that refers to SOURCE_IMAGE",
    23  		Args:  cli.ExactArgs(2),
    24  		RunE: func(cmd *cobra.Command, args []string) error {
    25  			opts.image = args[0]
    26  			opts.name = args[1]
    27  			return runTag(dockerCli, opts)
    28  		},
    29  	}
    30  
    31  	flags := cmd.Flags()
    32  	flags.SetInterspersed(false)
    33  
    34  	return cmd
    35  }
    36  
    37  func runTag(dockerCli command.Cli, opts tagOptions) error {
    38  	ctx := context.Background()
    39  
    40  	return dockerCli.Client().ImageTag(ctx, opts.image, opts.name)
    41  }