forked from genuinetools/img
-
Notifications
You must be signed in to change notification settings - Fork 0
/
remove.go
74 lines (57 loc) · 1.57 KB
/
remove.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"
"fmt"
"github.com/spf13/cobra"
"github.com/containerd/containerd/namespaces"
"github.com/genuinetools/img/client"
"github.com/moby/buildkit/identity"
"github.com/moby/buildkit/session"
)
const removeUsageShortHelp = `Remove one or more images.`
const removeUsageLongHelp = `Remove one or more images.`
func newRemoveCommand() *cobra.Command {
remove := &removeCommand{}
cmd := &cobra.Command{
Use: "rm [OPTIONS] IMAGE [IMAGE...]",
DisableFlagsInUseLine: true,
SilenceUsage: true,
Short: removeUsageShortHelp,
Long: removeUsageLongHelp,
Args: remove.ValidateArgs,
RunE: func(cmd *cobra.Command, args []string) error {
return remove.Run(args)
},
}
return cmd
}
type removeCommand struct{}
func (cmd *removeCommand) ValidateArgs(c *cobra.Command, args []string) error {
if len(args) < 1 {
return fmt.Errorf("must pass an image to remove")
}
return nil
}
func (cmd *removeCommand) Run(args []string) (err error) {
reexec()
// Create the context.
id := identity.NewID()
ctx := session.NewContext(context.Background(), id)
ctx = namespaces.WithNamespace(ctx, "buildkit")
// Create the client.
c, err := client.New(stateDir, backend, nil)
if err != nil {
return err
}
defer c.Close()
// Loop over the arguments as images and run remove.
for _, image := range args {
fmt.Printf("Removing %s...\n", image)
err = c.RemoveImage(ctx, image)
if err != nil {
return err
}
fmt.Printf("Successfully removed %s\n", image)
}
return nil
}