-
Notifications
You must be signed in to change notification settings - Fork 0
/
prune.go
51 lines (46 loc) · 1.13 KB
/
prune.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
package composectl
import (
"encoding/json"
"fmt"
v1 "github.com/foundriesio/composeapp/pkg/compose/v1"
"github.com/spf13/cobra"
)
var pruneCmd = &cobra.Command{
Use: "prune",
Short: "prune dangling blobs",
Long: ``,
Args: cobra.NoArgs,
}
type (
pruneOptions struct {
Format string
}
)
func init() {
opts := pruneOptions{}
pruneCmd.Flags().StringVar(&opts.Format, "format", "plain", "format the output. Values: [plain | json]")
pruneCmd.Run = func(cmd *cobra.Command, args []string) {
if opts.Format != "plain" && opts.Format != "json" {
DieNotNil(fmt.Errorf("invalid value of `--format` option: %s", opts.Format))
}
pruneApps(cmd, args, &opts)
}
rootCmd.AddCommand(pruneCmd)
}
func pruneApps(cmd *cobra.Command, args []string, opts *pruneOptions) {
cs, err := v1.NewAppStore(config.StoreRoot, config.Platform)
DieNotNil(err)
prunedBlobs, err := cs.Prune(cmd.Context())
DieNotNil(err)
if opts.Format == "json" {
if b, err := json.MarshalIndent(prunedBlobs, "", " "); err == nil {
fmt.Println(string(b))
} else {
DieNotNil(err)
}
} else {
for _, b := range prunedBlobs {
fmt.Println(b)
}
}
}