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 manifest rm command to remove manifest list draft from local storage #2449

Merged
merged 1 commit into from Sep 17, 2020
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions cli/command/manifest/cmd.go
Expand Up @@ -27,6 +27,7 @@ func NewManifestCommand(dockerCli command.Cli) *cobra.Command {
newInspectCommand(dockerCli),
newAnnotateCommand(dockerCli),
newPushListCommand(dockerCli),
newRmManifestListCommand(dockerCli),
)
return cmd
}
Expand Down
47 changes: 47 additions & 0 deletions cli/command/manifest/rm.go
@@ -0,0 +1,47 @@
package manifest

import (
"strings"

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

func newRmManifestListCommand(dockerCli command.Cli) *cobra.Command {
cmd := &cobra.Command{
Use: "rm MANIFEST_LIST [MANIFEST_LIST...]",
Short: "Delete one or more manifest lists from local storage",
Args: cli.RequiresMinArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
return runRm(dockerCli, args)
},
}

return cmd
}

func runRm(dockerCli command.Cli, targets []string) error {
var errs []string
for _, target := range targets {
targetRef, refErr := normalizeReference(target)
if refErr != nil {
errs = append(errs, refErr.Error())
continue
Copy link
Member

Choose a reason for hiding this comment

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

We could probably add a -f / --force option to switch between "failing immediately" if any of the references is invalid and/or doesn't exist, or only printing the error, but still completing the command successfully (see #2678 and #2677)

Not a blocker; this command is still experimental, so I'm ok on improving that in a follow-up

Copy link
Contributor Author

Choose a reason for hiding this comment

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

this command is still experimental

--force isn't ubiquitous either way

docker network rm --help

Usage:  docker network rm NETWORK [NETWORK...]

Remove one or more networks

Aliases:
rm, remove

Copy link
Member

Choose a reason for hiding this comment

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

I think for network, there had been some discussion, but the problem was that --force would also imply "if it's in use", which could mean that containers would have to be (automatically) detached from the network.

Copy link
Contributor

Choose a reason for hiding this comment

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

I agree with your proposal @thaJeztah, and also ok to improve that in a follow-up 👍

}
_, searchErr := dockerCli.ManifestStore().GetList(targetRef)
if searchErr != nil {
errs = append(errs, searchErr.Error())
continue
}
rmErr := dockerCli.ManifestStore().Remove(targetRef)
if rmErr != nil {
errs = append(errs, rmErr.Error())
}
}
if len(errs) > 0 {
return errors.New(strings.Join(errs, "\n"))
}
return nil
}
65 changes: 65 additions & 0 deletions cli/command/manifest/rm_test.go
@@ -0,0 +1,65 @@
package manifest

import (
"io/ioutil"
"testing"

"github.com/docker/cli/internal/test"
"gotest.tools/v3/assert"
)

// create two manifest lists and remove them both
func TestRmSeveralManifests(t *testing.T) {
store, cleanup := newTempManifestStore(t)
defer cleanup()

cli := test.NewFakeCli(nil)
cli.SetManifestStore(store)

list1 := ref(t, "first:1")
namedRef := ref(t, "alpine:3.0")
err := store.Save(list1, namedRef, fullImageManifest(t, namedRef))
assert.NilError(t, err)
namedRef = ref(t, "alpine:3.1")
err = store.Save(list1, namedRef, fullImageManifest(t, namedRef))
assert.NilError(t, err)

list2 := ref(t, "second:2")
namedRef = ref(t, "alpine:3.2")
err = store.Save(list2, namedRef, fullImageManifest(t, namedRef))
assert.NilError(t, err)

cmd := newRmManifestListCommand(cli)
cmd.SetArgs([]string{"example.com/first:1", "example.com/second:2"})
cmd.SetOut(ioutil.Discard)
err = cmd.Execute()
assert.NilError(t, err)

_, search1 := cli.ManifestStore().GetList(list1)
_, search2 := cli.ManifestStore().GetList(list2)
assert.Error(t, search1, "No such manifest: example.com/first:1")
assert.Error(t, search2, "No such manifest: example.com/second:2")
}

// attempt to remove a manifest list which was never created
func TestRmManifestNotCreated(t *testing.T) {
store, cleanup := newTempManifestStore(t)
defer cleanup()

cli := test.NewFakeCli(nil)
cli.SetManifestStore(store)

list2 := ref(t, "second:2")
namedRef := ref(t, "alpine:3.2")
err := store.Save(list2, namedRef, fullImageManifest(t, namedRef))
assert.NilError(t, err)

cmd := newRmManifestListCommand(cli)
cmd.SetArgs([]string{"example.com/first:1", "example.com/second:2"})
cmd.SetOut(ioutil.Discard)
err = cmd.Execute()
assert.Error(t, err, "No such manifest: example.com/first:1")

_, err = cli.ManifestStore().GetList(list2)
assert.Error(t, err, "No such manifest: example.com/second:2")
}
12 changes: 12 additions & 0 deletions contrib/completion/bash/docker
Expand Up @@ -4128,6 +4128,7 @@ _docker_manifest() {
create
inspect
push
rm
"
__docker_subcommands "$subcommands" && return

Expand Down Expand Up @@ -4226,6 +4227,17 @@ _docker_manifest_push() {
esac
}

_docker_network_rm() {
jennydaman marked this conversation as resolved.
Show resolved Hide resolved
case "$cur" in
-*)
COMPREPLY=( $( compgen -W "--help" -- "$cur" ) )
;;
*)
__docker_complete_images --force-tag --id
;;
esac
}

_docker_node() {
local subcommands="
demote
Expand Down