github.com/docker/app@v0.9.1-beta3.0.20210611140623-a48f773ab002/internal/commands/image/rm.go (about)

     1  package image
     2  
     3  import (
     4  	"errors"
     5  	"fmt"
     6  	"strings"
     7  
     8  	"github.com/docker/app/internal/store"
     9  	"github.com/docker/cli/cli"
    10  	"github.com/docker/cli/cli/config"
    11  	"github.com/docker/distribution/reference"
    12  	"github.com/spf13/cobra"
    13  )
    14  
    15  const rmExample = `- $ docker app image rm myapp
    16  - $ docker app image rm myapp:1.0.0
    17  - $ docker app image rm myrepo/myapp@sha256:c0de...
    18  - $ docker app image rm 34be4a0c5f50
    19  - $ docker app image rm --force 34be4a0c5f50`
    20  
    21  type rmOptions struct {
    22  	force bool
    23  }
    24  
    25  func rmCmd() *cobra.Command {
    26  	options := rmOptions{}
    27  	cmd := &cobra.Command{
    28  		Short:   "Remove an App image",
    29  		Use:     "rm APP_IMAGE [APP_IMAGE...]",
    30  		Aliases: []string{"remove"},
    31  		Args:    cli.RequiresMinArgs(1),
    32  		Example: rmExample,
    33  		RunE: func(cmd *cobra.Command, args []string) error {
    34  			appstore, err := store.NewApplicationStore(config.Dir())
    35  			if err != nil {
    36  				return err
    37  			}
    38  
    39  			imageStore, err := appstore.ImageStore()
    40  			if err != nil {
    41  				return err
    42  			}
    43  
    44  			errs := []string{}
    45  			for _, arg := range args {
    46  				if err := runRm(imageStore, arg, options); err != nil {
    47  					errs = append(errs, fmt.Sprintf("Error: %s", err))
    48  				}
    49  			}
    50  			if len(errs) > 0 {
    51  				return errors.New(strings.Join(errs, "\n"))
    52  			}
    53  			return nil
    54  		},
    55  	}
    56  	cmd.Flags().BoolVarP(&options.force, "force", "f", false, "")
    57  	return cmd
    58  }
    59  
    60  func runRm(imageStore store.ImageStore, app string, options rmOptions) error {
    61  	ref, err := imageStore.LookUp(app)
    62  	if err != nil {
    63  		return err
    64  	}
    65  
    66  	if err := imageStore.Remove(ref, options.force); err != nil {
    67  		return err
    68  	}
    69  
    70  	fmt.Println("Deleted: " + reference.FamiliarString(ref))
    71  	return nil
    72  }