Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docker context rm: allow --force to ignore non-existing contexts #3791

Merged
merged 1 commit into from
Sep 29, 2022

Conversation

thaJeztah
Copy link
Member

Before this change, running docker context rm --force would fail if the context did not exist. This behavior was different from other commands, which allowed ignoring non-existing objects.

For example; when trying to remove a non-existing volume, the command would fail without "force":

docker volume rm nosuchvolume
Error: No such volume: nosuchvolume
echo $?
1

But using the -f / --force option would make the command complete successfully (the error itself is still printed for informational purposes);

docker volume rm -f nosuchvolume
nosuchvolume
echo $?
0

With this patch, docker context rm behaves the same:

docker context rm nosuchcontext
context "nosuchcontext" does not exist
echo $?
1
docker context rm -f nosuchcontext
nosuchcontext
echo $?
0

This patch also simplifies how we check if the context exists; previously we would try to read the context's metadata; this could fail if a context was corrupted, or if an empty directory was present. This patch now only checks if the directory exists, without first validating the context's data.

- Description for the changelog

- A picture of a cute animal (not mandatory but encouraged)

@codecov-commenter
Copy link

Codecov Report

Merging #3791 (b48d325) into master (3dad26c) will increase coverage by 0.01%.
The diff coverage is 90.90%.

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #3791      +/-   ##
==========================================
+ Coverage   59.28%   59.29%   +0.01%     
==========================================
  Files         288      288              
  Lines       24632    24639       +7     
==========================================
+ Hits        14602    14609       +7     
  Misses       9160     9160              
  Partials      870      870              

@@ -64,5 +63,21 @@ func doRemove(dockerCli command.Cli, name string, isCurrent, force bool) error {
return err
}
}

if err := checkContextExists(dockerCli, name); err != nil && !force {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: The result of checkContextExists doesn't matter when !force. We could avoid the call entirely by checking !force first.

Suggested change
if err := checkContextExists(dockerCli, name); err != nil && !force {
if !force {
if err := checkContextExists(dockerCli, name); err != nil {
return err
}
}

It also makes the !force check a bit more noticeable IMO.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LOL! I looked at my PR Yesterday evening and thought: "I should update that to only run if force is not enabled", and was about to make that change when I saw your comment 👍

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The reason I didn't have this in the first place was that some other commands still print the error on --force, but not return it as an error.

It felt a bit too verbose, so I decided not to do that, but then forgot to update this condition 😅

Either way; updated 👍

@@ -26,7 +28,7 @@ func newRemoveCommand(dockerCli command.Cli) *cobra.Command {
return RunRemove(dockerCli, opts, args)
},
}
cmd.Flags().BoolVarP(&opts.Force, "force", "f", false, "Force the removal of a context in use")
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll temporarily change this back to the old string; while it's not "fully" accurate, the docs need some improvements as well for this (no description for this flag), so I'll update the flag description together with improving the docs in a follow-up

Before this change, running `docker context rm --force` would fail if the context
did not exist. This behavior was different from other commands, which allowed
ignoring non-existing objects.

For example; when trying to remove a non-existing volume, the command would
fail without "force":

```bash
docker volume rm nosuchvolume
Error: No such volume: nosuchvolume
echo $?
1
```

But using the `-f` / `--force` option would make the command complete successfully
(the error itself is still printed for informational purposes);

```bash
docker volume rm -f nosuchvolume
nosuchvolume
echo $?
0
```

With this patch, `docker context rm` behaves the same:

```bash
docker context rm nosuchcontext
context "nosuchcontext" does not exist
echo $?
1
```

```bash
docker context rm -f nosuchcontext
nosuchcontext
echo $?
0
```

This patch also simplifies how we check if the context exists; previously we
would try to read the context's metadata; this could fail if a context was
corrupted, or if an empty directory was present. This patch now only checks
if the directory exists, without first validating the context's data.

Signed-off-by: Sebastiaan van Stijn <[email protected]>
@thaJeztah
Copy link
Member Author

CI is still happy; let me bring this one in, and rebase #3790

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants