Merge pull request #2449 from jennydaman/manifest-rm

`docker manifest rm` command to remove manifest list draft from local storage
This commit is contained in:
Sebastiaan van Stijn 2020-09-17 20:52:52 +02:00 committed by GitHub
commit b747821ace
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 125 additions and 0 deletions

View File

@ -27,6 +27,7 @@ func NewManifestCommand(dockerCli command.Cli) *cobra.Command {
newInspectCommand(dockerCli), newInspectCommand(dockerCli),
newAnnotateCommand(dockerCli), newAnnotateCommand(dockerCli),
newPushListCommand(dockerCli), newPushListCommand(dockerCli),
newRmManifestListCommand(dockerCli),
) )
return cmd return cmd
} }

View File

@ -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
}
_, 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
}

View File

@ -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")
}

View File

@ -4182,6 +4182,7 @@ _docker_manifest() {
create create
inspect inspect
push push
rm
" "
__docker_subcommands "$subcommands" && return __docker_subcommands "$subcommands" && return
@ -4280,6 +4281,17 @@ _docker_manifest_push() {
esac esac
} }
_docker_network_rm() {
case "$cur" in
-*)
COMPREPLY=( $( compgen -W "--help" -- "$cur" ) )
;;
*)
__docker_complete_images --force-tag --id
;;
esac
}
_docker_node() { _docker_node() {
local subcommands=" local subcommands="
demote demote