11package context
22
33import (
4+ "errors"
45 "fmt"
56 "os"
6- "strings"
77
88 "github.com/docker/cli/cli"
99 "github.com/docker/cli/cli/command"
1010 "github.com/docker/docker/errdefs"
11- "github.com/pkg/errors"
1211 "github.com/spf13/cobra"
1312)
1413
@@ -33,28 +32,25 @@ func newRemoveCommand(dockerCli command.Cli) *cobra.Command {
3332}
3433
3534// RunRemove removes one or more contexts
36- func RunRemove (dockerCli command.Cli , opts RemoveOptions , names []string ) error {
37- var errs []string
38- currentCtx := dockerCli .CurrentContext ()
35+ func RunRemove (dockerCLI command.Cli , opts RemoveOptions , names []string ) error {
36+ var errs []error
37+ currentCtx := dockerCLI .CurrentContext ()
3938 for _ , name := range names {
4039 if name == "default" {
41- errs = append (errs , `default: context "default" cannot be removed` )
42- } else if err := doRemove (dockerCli , name , name == currentCtx , opts .Force ); err != nil {
43- errs = append (errs , err . Error () )
40+ errs = append (errs , errors . New ( ` context "default" cannot be removed`) )
41+ } else if err := doRemove (dockerCLI , name , name == currentCtx , opts .Force ); err != nil {
42+ errs = append (errs , err )
4443 } else {
45- fmt .Fprintln (dockerCli .Out (), name )
44+ _ , _ = fmt .Fprintln (dockerCLI .Out (), name )
4645 }
4746 }
48- if len (errs ) > 0 {
49- return errors .New (strings .Join (errs , "\n " ))
50- }
51- return nil
47+ return errors .Join (errs ... )
5248}
5349
5450func doRemove (dockerCli command.Cli , name string , isCurrent , force bool ) error {
5551 if isCurrent {
5652 if ! force {
57- return errors .Errorf ("context %q is in use, set -f flag to force remove" , name )
53+ return fmt .Errorf ("context %q is in use, set -f flag to force remove" , name )
5854 }
5955 // fallback to DOCKER_HOST
6056 cfg := dockerCli .ConfigFile ()
@@ -65,6 +61,7 @@ func doRemove(dockerCli command.Cli, name string, isCurrent, force bool) error {
6561 }
6662
6763 if ! force {
64+ // TODO(thaJeztah): instead of checking before removing, can we make ContextStore().Remove() return a proper errdef and ignore "not found" errors?
6865 if err := checkContextExists (dockerCli , name ); err != nil {
6966 return err
7067 }
@@ -77,7 +74,7 @@ func checkContextExists(dockerCli command.Cli, name string) error {
7774 contextDir := dockerCli .ContextStore ().GetStorageInfo (name ).MetadataPath
7875 _ , err := os .Stat (contextDir )
7976 if os .IsNotExist (err ) {
80- return errdefs .NotFound (errors .Errorf ("context %q does not exist" , name ))
77+ return errdefs .NotFound (fmt .Errorf ("context %q does not exist" , name ))
8178 }
8279 // Ignore other errors; if relevant, they will produce an error when
8380 // performing the actual delete.
0 commit comments