aboutsummaryrefslogtreecommitdiff
path: root/cmd/podman/untag.go
blob: 9ff62b8085b34a81581baa6fa64ea0dfeec89966 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package main

import (
	"github.com/containers/libpod/cmd/podman/cliconfig"
	"github.com/containers/libpod/pkg/adapter"
	"github.com/pkg/errors"
	"github.com/sirupsen/logrus"
	"github.com/spf13/cobra"
)

var (
	untagCommand cliconfig.UntagValues

	_untagCommand = &cobra.Command{
		Use:   "untag [flags] IMAGE [NAME...]",
		Short: "Remove a name from a local image",
		Long:  "Removes one or more names from a locally-stored image.",
		RunE: func(cmd *cobra.Command, args []string) error {
			untagCommand.InputArgs = args
			untagCommand.GlobalFlags = MainGlobalOpts
			untagCommand.Remote = remoteclient
			return untag(&untagCommand)
		},
		Example: `podman untag 0e3bbc2
  podman untag imageID:latest otherImageName:latest
  podman untag httpd myregistryhost:5000/fedora/httpd:v2`,
	}
)

func init() {
	untagCommand.Command = _untagCommand
	untagCommand.SetHelpTemplate(HelpTemplate())
	untagCommand.SetUsageTemplate(UsageTemplate())
}

func untag(c *cliconfig.UntagValues) error {
	args := c.InputArgs

	if len(args) == 0 {
		return errors.Errorf("at least one image name needs to be specified")
	}

	runtime, err := adapter.GetRuntime(getContext(), &c.PodmanCommand)
	if err != nil {
		return errors.Wrapf(err, "could not create runtime")
	}
	defer runtime.DeferredShutdown(false)

	newImage, err := runtime.NewImageFromLocal(args[0])
	if err != nil {
		return err
	}

	tags := args[1:]
	if len(args) == 1 {
		// Remove all tags if not explicitly specified
		tags = newImage.Names()
	}
	logrus.Debugf("Tags to be removed: %v", tags)

	for _, tag := range tags {
		if err := newImage.UntagImage(tag); err != nil {
			return errors.Wrapf(err, "removing %q from %q", tag, newImage.InputName)
		}
	}
	return nil
}