github.com/containers/libpod@v1.9.4-0.20220419124438-4284fd425507/cmd/podman/untag.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/sirupsen/logrus"
     8  	"github.com/spf13/cobra"
     9  )
    10  
    11  var (
    12  	untagCommand cliconfig.UntagValues
    13  
    14  	_untagCommand = &cobra.Command{
    15  		Use:   "untag [flags] IMAGE [NAME...]",
    16  		Short: "Remove a name from a local image",
    17  		Long:  "Removes one or more names from a locally-stored image.",
    18  		RunE: func(cmd *cobra.Command, args []string) error {
    19  			untagCommand.InputArgs = args
    20  			untagCommand.GlobalFlags = MainGlobalOpts
    21  			untagCommand.Remote = remoteclient
    22  			return untag(&untagCommand)
    23  		},
    24  		Example: `podman untag 0e3bbc2
    25    podman untag imageID:latest otherImageName:latest
    26    podman untag httpd myregistryhost:5000/fedora/httpd:v2`,
    27  	}
    28  )
    29  
    30  func init() {
    31  	untagCommand.Command = _untagCommand
    32  	untagCommand.SetHelpTemplate(HelpTemplate())
    33  	untagCommand.SetUsageTemplate(UsageTemplate())
    34  }
    35  
    36  func untag(c *cliconfig.UntagValues) error {
    37  	args := c.InputArgs
    38  
    39  	if len(args) == 0 {
    40  		return errors.Errorf("at least one image name needs to be specified")
    41  	}
    42  
    43  	runtime, err := adapter.GetRuntime(getContext(), &c.PodmanCommand)
    44  	if err != nil {
    45  		return errors.Wrapf(err, "could not create runtime")
    46  	}
    47  	defer runtime.DeferredShutdown(false)
    48  
    49  	newImage, err := runtime.NewImageFromLocal(args[0])
    50  	if err != nil {
    51  		return err
    52  	}
    53  
    54  	tags := args[1:]
    55  	if len(args) == 1 {
    56  		// Remove all tags if not explicitly specified
    57  		tags = newImage.Names()
    58  	}
    59  	logrus.Debugf("Tags to be removed: %v", tags)
    60  
    61  	for _, tag := range tags {
    62  		if err := newImage.UntagImage(tag); err != nil {
    63  			return errors.Wrapf(err, "removing %q from %q", tag, newImage.InputName)
    64  		}
    65  	}
    66  	return nil
    67  }