-
Notifications
You must be signed in to change notification settings - Fork 950
/
Copy pathrmi.go
74 lines (63 loc) · 2.26 KB
/
rmi.go
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
68
69
70
71
72
73
74
package main
import (
"context"
"errors"
"fmt"
"strings"
"github.com/spf13/cobra"
)
var rmiDescription = "Remove one or more images by reference." +
"When the image is being used by a container, you must specify -f to delete it. " +
"But it is strongly discouraged, because the container will be in abnormal status."
// RmiCommand use to implement 'rmi' command, it remove one or more images by reference
type RmiCommand struct {
baseCommand
force bool
}
// Init initialize rmi command
func (rmi *RmiCommand) Init(c *Cli) {
rmi.cli = c
rmi.cmd = &cobra.Command{
Use: "rmi [OPTIONS] IMAGE [IMAGE...]",
Short: "Remove one or more images by reference",
Long: rmiDescription,
Args: cobra.MinimumNArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
return rmi.runRmi(args)
},
Example: rmiExample(),
}
rmi.addFlags()
}
// addFlags adds flags for specific command
func (rmi *RmiCommand) addFlags() {
rmi.cmd.Flags().BoolVarP(&rmi.force, "force", "f", false, "if image is being used, remove image and all associated resources")
}
// runRmi is the entry of rmi command
func (rmi *RmiCommand) runRmi(args []string) error {
ctx := context.Background()
apiClient := rmi.cli.Client()
var errs []string
for _, name := range args {
if err := apiClient.ImageRemove(ctx, name, rmi.force); err != nil {
errs = append(errs, err.Error())
continue
}
fmt.Printf("%s\n", name)
}
if len(errs) > 0 {
return errors.New("failed to remove images: " + strings.Join(errs, ""))
}
return nil
}
// rmiExample shows examples in rmi command, and is used in auto-generated cli docs.
func rmiExample() string {
return `$ pouch rmi registry.hub.docker.com/library/busybox:latest registry.hub.docker.com/library/busybox:1.28
registry.hub.docker.com/library/busybox:latest
registry.hub.docker.com/library/busybox:1.28
$ pouch create --name test registry.hub.docker.com/library/busybox:latest
container ID: e5952417f9ee94621bbeaec532be1803ae2dedeb11a80f578a6d621e04a95afd, name: test
$ pouch rmi registry.hub.docker.com/library/busybox:latest
Error: failed to remove image: {"message":"Unable to remove the image \"registry.hub.docker.com/library/busybox:latest\" (must force) - container e5952417f9ee94621bbeaec532be1803ae2dedeb11a80f578a6d621e04a95afd is using this image"}
`
}