|
| 1 | +package cmd |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "os" |
| 7 | + "path/filepath" |
| 8 | + |
| 9 | + "github.com/docker/docker/api/types" |
| 10 | + "github.com/hashicorp/go-hclog" |
| 11 | + "github.com/shipyard-run/shipyard/pkg/clients" |
| 12 | + "github.com/shipyard-run/shipyard/pkg/utils" |
| 13 | + "github.com/spf13/cobra" |
| 14 | +) |
| 15 | + |
| 16 | +func newPurgeCmd(dt clients.Docker, il clients.ImageLog, l hclog.Logger) *cobra.Command { |
| 17 | + purgeCmd := &cobra.Command{ |
| 18 | + Use: "purge", |
| 19 | + Short: "Purges Docker images, Helm charts, and Blueprints downloaded by Shipyard", |
| 20 | + Long: "Purges Docker images, Helm charts, and Blueprints downloaded by Shipyard", |
| 21 | + Example: ` |
| 22 | + shipyard purge |
| 23 | + `, |
| 24 | + Args: cobra.ArbitraryArgs, |
| 25 | + RunE: newPurgeCmdFunc(dt, il, l), |
| 26 | + SilenceUsage: true, |
| 27 | + } |
| 28 | + |
| 29 | + return purgeCmd |
| 30 | +} |
| 31 | + |
| 32 | +func newPurgeCmdFunc(dt clients.Docker, il clients.ImageLog, l hclog.Logger) func(cmd *cobra.Command, args []string) error { |
| 33 | + return func(cmd *cobra.Command, args []string) error { |
| 34 | + images, _ := il.Read(clients.ImageTypeDocker) |
| 35 | + |
| 36 | + for _, i := range images { |
| 37 | + l.Info("Removing image", "image", i) |
| 38 | + |
| 39 | + _, err := dt.ImageRemove(context.Background(), i, types.ImageRemoveOptions{Force: true, PruneChildren: true}) |
| 40 | + if err != nil { |
| 41 | + return fmt.Errorf("Unable to delete image: %s, error: %s", i, err) |
| 42 | + } |
| 43 | + } |
| 44 | + il.Clear() |
| 45 | + |
| 46 | + hcp := filepath.Join(utils.ShipyardHome(), "helm_charts") |
| 47 | + l.Info("Removing Helm charts", "path", hcp) |
| 48 | + err := os.RemoveAll(hcp) |
| 49 | + if err != nil { |
| 50 | + return fmt.Errorf("Unable to remove cached Helm charts: %s", err) |
| 51 | + } |
| 52 | + |
| 53 | + bcp := filepath.Join(utils.ShipyardHome(), "helm_charts") |
| 54 | + l.Info("Removing Blueprints", "path", bcp) |
| 55 | + err = os.RemoveAll(bcp) |
| 56 | + if err != nil { |
| 57 | + return fmt.Errorf("Unable to remove cached Blueprints: %s", err) |
| 58 | + } |
| 59 | + |
| 60 | + return nil |
| 61 | + } |
| 62 | +} |
0 commit comments