From 2712c1017e0a6d82ca2e1b48044bb1189faf7634 Mon Sep 17 00:00:00 2001 From: Alex Goodman Date: Mon, 7 Oct 2019 12:41:57 -0400 Subject: [PATCH] remove reverse iteration for layer display and processing --- dive/image/docker/image_archive.go | 16 ++++++---------- dive/image/podman/image_directory.go | 28 ++++++++++++---------------- dive/image/podman/layer.go | 10 +++++++--- runtime/ui/layer_controller.go | 4 +--- 4 files changed, 26 insertions(+), 32 deletions(-) diff --git a/dive/image/docker/image_archive.go b/dive/image/docker/image_archive.go index 106bd4a..8d93301 100644 --- a/dive/image/docker/image_archive.go +++ b/dive/image/docker/image_archive.go @@ -147,16 +147,13 @@ func (img *ImageArchive) ToImage() (*image.Image, error) { } // build the layers array - layers := make([]*image.Layer, len(trees)) + layers := make([]*image.Layer, 0) // note that the resolver config stores images in reverse chronological order, so iterate backwards through layers // as you iterate chronologically through history (ignoring history items that have no layer contents) // Note: history is not required metadata in a docker image! - tarPathIdx := 0 histIdx := 0 - for layerIdx := len(trees) - 1; layerIdx >= 0; layerIdx-- { - tree := trees[(len(trees)-1)-layerIdx] - + for idx, tree := range trees { // ignore empty layers, we are only observing layers with content historyObj := historyEntry{ CreatedBy: "(missing)", @@ -176,14 +173,13 @@ func (img *ImageArchive) ToImage() (*image.Image, error) { dockerLayer := layer{ history: historyObj, - index: tarPathIdx, - tree: trees[layerIdx], + index: idx, + tree: tree, } - layers[layerIdx] = dockerLayer.ToLayer() - - tarPathIdx++ + layers = append(layers, dockerLayer.ToLayer()) } + return &image.Image{ Trees: trees, Layers: layers, diff --git a/dive/image/podman/image_directory.go b/dive/image/podman/image_directory.go index 4e2d1d5..333e967 100644 --- a/dive/image/podman/image_directory.go +++ b/dive/image/podman/image_directory.go @@ -99,29 +99,25 @@ func processLayer(name, rootDir string) (*filetree.FileTree, error) { func (img *ImageDirectoryRef) ToImage() (*image.Image, error) { trees := make([]*filetree.FileTree, 0) + layers := make([]*image.Layer, 0) + // build the content tree - // todo: this isn't needed! - for _, id := range img.layerOrder { - tr, exists := img.treeMap[id] - if exists { - trees = append(trees, tr) - continue - } - return nil, fmt.Errorf("could not find '%s' in parsed trees", id) - } - - layers := make([]*image.Layer, len(trees)) - - // note that the resolver config stores images in reverse chronological order, so iterate backwards through layers - // as you iterate chronologically through history (ignoring history items that have no layer contents) - // Note: history is not required metadata in an OCI image! for layerIdx, id := range img.layerOrder { + tr, exists := img.treeMap[id] + if !exists { + return nil, fmt.Errorf("could not find '%s' in parsed trees", id) + } + trees = append(trees, tr) + + // note that the resolver config stores images in reverse chronological order, so iterate backwards through layers + // as you iterate chronologically through history (ignoring history items that have no layer contents) + // Note: history is not required metadata in an OCI image! podmanLayer := layer{ obj: img.layerMap[id], index: layerIdx, tree: trees[layerIdx], } - layers[layerIdx] = podmanLayer.ToLayer() + layers = append(layers, podmanLayer.ToLayer()) } return &image.Image{ diff --git a/dive/image/podman/layer.go b/dive/image/podman/layer.go index de173a5..daad3d3 100644 --- a/dive/image/podman/layer.go +++ b/dive/image/podman/layer.go @@ -1,6 +1,7 @@ package podman import ( + "context" podmanImage "github.com/containers/libpod/libpod/image" "github.com/wagoodman/dive/dive/filetree" "github.com/wagoodman/dive/dive/image" @@ -16,9 +17,12 @@ type layer struct { // ShortId returns the truncated id of the current layer. func (l *layer) Command() string { - if len(l.obj.ImageData.History) > 0 { - hist := l.obj.ImageData.History - return strings.TrimPrefix(hist[len(hist)-1].CreatedBy, "/bin/sh -c ") + history, err := l.obj.History(context.TODO()) + if err != nil { + return "error: " + err.Error() + } + if len(history) > 0 { + return strings.TrimPrefix(history[0].CreatedBy, "/bin/sh -c ") } return "unknown" } diff --git a/runtime/ui/layer_controller.go b/runtime/ui/layer_controller.go index b1868c9..56cce36 100644 --- a/runtime/ui/layer_controller.go +++ b/runtime/ui/layer_controller.go @@ -291,9 +291,7 @@ func (controller *LayerController) Render() error { // update contents controller.view.Clear() - for revIdx := len(controller.Layers) - 1; revIdx >= 0; revIdx-- { - layer := controller.Layers[revIdx] - idx := (len(controller.Layers) - 1) - revIdx + for idx, layer := range controller.Layers { layerStr := layer.String() compareBar := controller.renderCompareBar(idx)