cmd/dist: show real image size in list

As a demonstration of the power of the visitor implementation, we now
report the image size in the `dist images` command. This is the size of
the packed resources as would be pushed into a remote. A similar method
could be added to calculate the unpacked size.

Signed-off-by: Stephen J Day <stephen.day@docker.com>
This commit is contained in:
Stephen J Day 2017-03-21 17:24:45 -07:00
parent 5e5479718c
commit 1f21fb7f8b
No known key found for this signature in database
GPG key ID: 67B3DED84EDC823F
3 changed files with 56 additions and 3 deletions

View file

@ -86,3 +86,40 @@ func (image *Image) RootFS(ctx context.Context, provider content.Provider) ([]di
return diffIDs, nil
}
// Size returns the total size of an image's packed resources.
func (image *Image) Size(ctx context.Context, provider content.Provider) (int64, error) {
var size int64
return size, Walk(ctx, HandlerFunc(func(ctx context.Context, desc ocispec.Descriptor) ([]ocispec.Descriptor, error) {
switch image.Descriptor.MediaType {
case MediaTypeDockerSchema2Manifest, ocispec.MediaTypeImageManifest:
size += desc.Size
rc, err := provider.Reader(ctx, image.Descriptor.Digest)
if err != nil {
return nil, err
}
defer rc.Close()
p, err := ioutil.ReadAll(rc)
if err != nil {
return nil, err
}
var manifest ocispec.Manifest
if err := json.Unmarshal(p, &manifest); err != nil {
return nil, err
}
size += manifest.Config.Size
for _, layer := range manifest.Layers {
size += layer.Size
}
return nil, nil
default:
return nil, errors.New("unsupported type")
}
}), image.Descriptor)
}

View file

@ -12,7 +12,6 @@ import (
var (
errImageUnknown = fmt.Errorf("image: unknown")
errNoTx = fmt.Errorf("no transaction available")
)
var (