github.com/containers/libpod@v1.9.4-0.20220419124438-4284fd425507/cmd/podman/tag.go (about)

     1  package main
     2  
     3  import (
     4  	"github.com/containers/libpod/cmd/podman/cliconfig"
     5  	"github.com/containers/libpod/pkg/adapter"
     6  	"github.com/pkg/errors"
     7  	"github.com/spf13/cobra"
     8  )
     9  
    10  var (
    11  	tagCommand cliconfig.TagValues
    12  
    13  	tagDescription = "Adds one or more additional names to locally-stored image."
    14  	_tagCommand    = &cobra.Command{
    15  		Use:   "tag [flags] IMAGE TARGET_NAME [TARGET_NAME...]",
    16  		Short: "Add an additional name to a local image",
    17  		Long:  tagDescription,
    18  		RunE: func(cmd *cobra.Command, args []string) error {
    19  			tagCommand.InputArgs = args
    20  			tagCommand.GlobalFlags = MainGlobalOpts
    21  			tagCommand.Remote = remoteclient
    22  			return tagCmd(&tagCommand)
    23  		},
    24  		Example: `podman tag 0e3bbc2 fedora:latest
    25    podman tag imageID:latest myNewImage:newTag
    26    podman tag httpd myregistryhost:5000/fedora/httpd:v2`,
    27  	}
    28  )
    29  
    30  func init() {
    31  	tagCommand.Command = _tagCommand
    32  	tagCommand.SetHelpTemplate(HelpTemplate())
    33  	tagCommand.SetUsageTemplate(UsageTemplate())
    34  }
    35  
    36  func tagCmd(c *cliconfig.TagValues) error {
    37  	args := c.InputArgs
    38  	if len(args) < 2 {
    39  		return errors.Errorf("image name and at least one new name must be specified")
    40  	}
    41  	runtime, err := adapter.GetRuntime(getContext(), &c.PodmanCommand)
    42  	if err != nil {
    43  		return errors.Wrapf(err, "could not create runtime")
    44  	}
    45  	defer runtime.DeferredShutdown(false)
    46  
    47  	newImage, err := runtime.NewImageFromLocal(args[0])
    48  	if err != nil {
    49  		return err
    50  	}
    51  
    52  	for _, tagName := range args[1:] {
    53  		if err := newImage.TagImage(tagName); err != nil {
    54  			return errors.Wrapf(err, "error adding %q to image %q", tagName, newImage.InputName)
    55  		}
    56  	}
    57  	return nil
    58  }