Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 21 additions & 4 deletions cli/command/context/remove.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package context

import (
"errors"
"fmt"
"os"
"strings"

"github.com/docker/cli/cli"
"github.com/docker/cli/cli/command"
"github.com/docker/docker/errdefs"
"github.com/pkg/errors"
"github.com/spf13/cobra"
)

Expand Down Expand Up @@ -50,9 +52,6 @@ func RunRemove(dockerCli command.Cli, opts RemoveOptions, names []string) error
}

func doRemove(dockerCli command.Cli, name string, isCurrent, force bool) error {
if _, err := dockerCli.ContextStore().GetMetadata(name); err != nil {
return err
}
if isCurrent {
if !force {
return errors.New("context is in use, set -f flag to force remove")
Expand All @@ -64,5 +63,23 @@ func doRemove(dockerCli command.Cli, name string, isCurrent, force bool) error {
return err
}
}

if !force {
if err := checkContextExists(dockerCli, name); err != nil {
return err
}
}
return dockerCli.ContextStore().Remove(name)
}

// checkContextExists returns an error if the context directory does not exist.
func checkContextExists(dockerCli command.Cli, name string) error {
contextDir := dockerCli.ContextStore().GetStorageInfo(name).MetadataPath
_, err := os.Stat(contextDir)
if os.IsNotExist(err) {
return errdefs.NotFound(errors.Errorf("context %q does not exist", name))
}
// Ignore other errors; if relevant, they will produce an error when
// performing the actual delete.
return nil
}
3 changes: 3 additions & 0 deletions cli/command/context/remove_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ func TestRemoveNotAContext(t *testing.T) {
createTestContext(t, cli, "other")
err := RunRemove(cli, RemoveOptions{}, []string{"not-a-context"})
assert.ErrorContains(t, err, `context "not-a-context" does not exist`)

err = RunRemove(cli, RemoveOptions{Force: true}, []string{"not-a-context"})
assert.NilError(t, err)
}

func TestRemoveCurrent(t *testing.T) {
Expand Down