mirror of
https://github.com/docker/cli.git
synced 2026-01-13 18:22:35 +03:00
Merge pull request #6574 from vvoland/img-dangling
image/list: Hide untagged images without `--all`
This commit is contained in:
@@ -1,3 +1,5 @@
|
||||
//go:build go1.23
|
||||
|
||||
package image
|
||||
|
||||
import (
|
||||
@@ -5,6 +7,7 @@ import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"slices"
|
||||
|
||||
"github.com/docker/cli/cli"
|
||||
"github.com/docker/cli/cli/command"
|
||||
@@ -79,6 +82,7 @@ func newListCommand(dockerCLI command.Cli) *cobra.Command {
|
||||
return &cmd
|
||||
}
|
||||
|
||||
//nolint:gocyclo
|
||||
func runImages(ctx context.Context, dockerCLI command.Cli, options imagesOptions) error {
|
||||
filters := options.filter.Value()
|
||||
if options.matchName != "" {
|
||||
@@ -98,21 +102,33 @@ func runImages(ctx context.Context, dockerCLI command.Cli, options imagesOptions
|
||||
if options.format != "" {
|
||||
return errors.New("--format is not yet supported with --tree")
|
||||
}
|
||||
}
|
||||
|
||||
listOpts := client.ImageListOptions{
|
||||
All: options.all,
|
||||
Filters: filters,
|
||||
Manifests: options.tree,
|
||||
}
|
||||
|
||||
res, err := dockerCLI.Client().ImageList(ctx, listOpts)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
images := res.Items
|
||||
if !options.all {
|
||||
if _, ok := filters["dangling"]; ok {
|
||||
images = slices.DeleteFunc(images, isDangling)
|
||||
}
|
||||
}
|
||||
|
||||
if options.tree {
|
||||
return runTree(ctx, dockerCLI, treeOptions{
|
||||
images: images,
|
||||
all: options.all,
|
||||
filters: filters,
|
||||
})
|
||||
}
|
||||
|
||||
images, err := dockerCLI.Client().ImageList(ctx, client.ImageListOptions{
|
||||
All: options.all,
|
||||
Filters: filters,
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
format := options.format
|
||||
if len(format) == 0 {
|
||||
if len(dockerCLI.ConfigFile().ImagesFormat) > 0 && !options.quiet {
|
||||
@@ -130,10 +146,10 @@ func runImages(ctx context.Context, dockerCLI command.Cli, options imagesOptions
|
||||
},
|
||||
Digest: options.showDigests,
|
||||
}
|
||||
if err := formatter.ImageWrite(imageCtx, images.Items); err != nil {
|
||||
if err := formatter.ImageWrite(imageCtx, images); err != nil {
|
||||
return err
|
||||
}
|
||||
if options.matchName != "" && len(images.Items) == 0 && options.calledAs == "images" {
|
||||
if options.matchName != "" && len(images) == 0 && options.calledAs == "images" {
|
||||
printAmbiguousHint(dockerCLI.Err(), options.matchName)
|
||||
}
|
||||
return nil
|
||||
|
||||
@@ -7,7 +7,6 @@ import (
|
||||
"context"
|
||||
"fmt"
|
||||
"os"
|
||||
"slices"
|
||||
"sort"
|
||||
"strings"
|
||||
|
||||
@@ -24,6 +23,7 @@ import (
|
||||
)
|
||||
|
||||
type treeOptions struct {
|
||||
images []imagetypes.Summary
|
||||
all bool
|
||||
filters client.Filters
|
||||
}
|
||||
@@ -36,24 +36,17 @@ type treeView struct {
|
||||
}
|
||||
|
||||
func runTree(ctx context.Context, dockerCLI command.Cli, opts treeOptions) error {
|
||||
res, err := dockerCLI.Client().ImageList(ctx, client.ImageListOptions{
|
||||
All: opts.all,
|
||||
Filters: opts.filters,
|
||||
Manifests: true,
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if !opts.all {
|
||||
res.Items = slices.DeleteFunc(res.Items, isDangling)
|
||||
}
|
||||
images := opts.images
|
||||
|
||||
view := treeView{
|
||||
images: make([]topImage, 0, len(res.Items)),
|
||||
images: make([]topImage, 0, len(images)),
|
||||
}
|
||||
attested := make(map[digest.Digest]bool)
|
||||
|
||||
for _, img := range res.Items {
|
||||
for _, img := range images {
|
||||
if ctx.Err() != nil {
|
||||
return ctx.Err()
|
||||
}
|
||||
details := imageDetails{
|
||||
ID: img.ID,
|
||||
DiskUsage: units.HumanSizeWithPrecision(float64(img.Size), 3),
|
||||
|
||||
Reference in New Issue
Block a user