44package container_group
55
66import (
7+ "context"
78 "log/slog"
89
10+ "github.com/docker/docker/api/types/filters"
11+ "github.com/docker/go-units"
912 "github.com/spf13/cobra"
1013 "github.com/thin-edge/tedge-container-plugin/pkg/cli"
14+ "github.com/thin-edge/tedge-container-plugin/pkg/container"
1115)
1216
1317func NewFinalizeCommand (ctx cli.Cli ) * cobra.Command {
@@ -16,6 +20,25 @@ func NewFinalizeCommand(ctx cli.Cli) *cobra.Command {
1620 Short : "Finalize container install/remove operation" ,
1721 RunE : func (cmd * cobra.Command , args []string ) error {
1822 slog .Info ("Executing" , "cmd" , cmd .CalledAs (), "args" , args )
23+
24+ pruneImages := ctx .GetBool ("container.pruneImages" )
25+ if ! pruneImages {
26+ return nil
27+ }
28+ cli , err := container .NewContainerClient ()
29+ if err != nil {
30+ return err
31+ }
32+ slog .Info ("Pruning images" )
33+ ctx := context .Background ()
34+ resp , err := cli .Client .ImagesPrune (ctx , filters.Args {})
35+ if err != nil {
36+ return err
37+ }
38+ for _ , image := range resp .ImagesDeleted {
39+ slog .Info ("Deleted image." , "deleted" , image .Deleted , "untagged" , image .Untagged )
40+ }
41+ slog .Info ("Reclaimed space." , "size" , units .HumanSizeWithPrecision (float64 (resp .SpaceReclaimed ), 3 ))
1942 return nil
2043 },
2144 }
0 commit comments