()
| 12 | ) |
| 13 | |
| 14 | func cacheCleanCmd() *cobra.Command { |
| 15 | var ( |
| 16 | publishedOnly bool |
| 17 | ) |
| 18 | cmd := &cobra.Command{ |
| 19 | Use: "clean", |
| 20 | Short: "empty the linuxkit cache", |
| 21 | Long: `Empty the linuxkit cache.`, |
| 22 | RunE: func(cmd *cobra.Command, args []string) error { |
| 23 | // did we limit to published only? |
| 24 | if !publishedOnly { |
| 25 | if err := os.RemoveAll(cacheDir); err != nil { |
| 26 | return fmt.Errorf("uUnable to clean cache %s: %v", cacheDir, err) |
| 27 | } |
| 28 | log.Infof("Cache emptied: %s", cacheDir) |
| 29 | return nil |
| 30 | } |
| 31 | |
| 32 | // list all of the images and content in the cache |
| 33 | p, err := cachepkg.NewProvider(cacheDir) |
| 34 | if err != nil { |
| 35 | return fmt.Errorf("unable to read a local cache: %v", err) |
| 36 | } |
| 37 | images, err := p.List() |
| 38 | |
| 39 | if err != nil { |
| 40 | return fmt.Errorf("error reading image names: %v", err) |
| 41 | } |
| 42 | removeImagesFromCache(images, p, publishedOnly) |
| 43 | return nil |
| 44 | }, |
| 45 | } |
| 46 | |
| 47 | cmd.Flags().BoolVar(&publishedOnly, "published-only", false, "Only clean images that linuxkit can confirm at the time of running have been published to the registry") |
| 48 | |
| 49 | return cmd |
| 50 | } |
| 51 | |
| 52 | // removeImagesFromCache removes images from the cache. |
| 53 | func removeImagesFromCache(images map[string]string, p *cachepkg.Provider, publishedOnly bool) { |
no test coverage detected