-
Notifications
You must be signed in to change notification settings - Fork 0
/
image_remove.go
64 lines (51 loc) · 1.4 KB
/
image_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
package cli
import (
"context"
"github.com/archimoebius/fishler/util"
"github.com/docker/docker/api/types"
"github.com/docker/docker/client"
"github.com/spf13/cobra"
)
var RootImageRemoveCmd = &cobra.Command{
Use: "rm",
Short: "Remove the fishler Docker image",
Long: `If the fishler Docker image is not yet built, build it. Use --force to rebuild.`,
PersistentPreRun: func(cmd *cobra.Command, args []string) {
CallPersistentPreRun(cmd, args)
},
Run: func(cmd *cobra.Command, args []string) {
forceRemove, _ := cmd.Flags().GetBool("force")
dockerClient, err := client.NewClientWithOpts(
client.FromEnv,
client.WithAPIVersionNegotiation(),
)
if err != nil {
util.Logger.Error(err)
return
}
ctx := context.Background()
images, err := util.GetDockerImage(dockerClient, ctx)
if err != nil {
util.Logger.Error(err)
return
}
if len(images) == 0 {
util.Logger.Info("No fishler image's found for removal")
return
}
for _, image := range images {
_, err = dockerClient.ImageRemove(ctx, image.ID, types.ImageRemoveOptions{
Force: forceRemove,
PruneChildren: true,
})
if err != nil {
util.Logger.Errorf("Failed to remove image %s %v", image.ID, err)
continue
}
util.Logger.Infof("Image Removed %s", image.ID)
}
},
}
func init() {
RootImageRemoveCmd.Flags().BoolP("force", "f", false, "Force the fishler image to be removed")
}