cleanup layer id/digest & add tags support

This commit is contained in:
Alex Goodman 2019-10-07 13:57:59 -04:00
parent 2712c1017e
commit 7f786eb725
No known key found for this signature in database
GPG key ID: 98AF011C5C78EB7E
7 changed files with 60 additions and 22 deletions

View file

@ -46,12 +46,23 @@ func NewFileInfo(realPath, path string, info os.FileInfo) FileInfo {
// todo: don't use tar types here, create our own...
var fileType byte
var linkName string
var size int64
if info.Mode() & os.ModeSymlink != 0 {
fileType = tar.TypeSymlink
linkName, err = os.Readlink(realPath)
if err != nil {
logrus.Panic("unable to read link:", realPath, err)
}
} else if info.IsDir() {
fileType = tar.TypeDir
} else {
fileType = tar.TypeReg
size = info.Size()
}
var hash uint64
@ -64,20 +75,12 @@ func NewFileInfo(realPath, path string, info os.FileInfo) FileInfo {
hash = getHashFromReader(file)
}
var linkName string
if fileType == tar.TypeSymlink {
linkName, err = os.Readlink(realPath)
if err != nil {
logrus.Panic("unable to read link:", realPath, err)
}
}
return FileInfo{
Path: path,
TypeFlag: fileType,
Linkname: linkName,
hash: hash,
Size: info.Size(),
Size: size,
Mode: info.Mode(),
// todo: support UID/GID
Uid: -1,

View file

@ -17,11 +17,15 @@ type layer struct {
// String represents a layer in a columnar format.
func (l *layer) ToLayer() *image.Layer {
id := strings.Split(l.tree.Name, "/")[0]
return &image.Layer{
Id: l.history.ID,
Id: id,
Index: l.index,
Command: strings.TrimPrefix(l.history.CreatedBy, "/bin/sh -c "),
Size: l.history.Size,
Tree: l.tree,
// todo: query docker api for tags
Names: []string{"(unavailable)"},
Digest: l.history.ID,
}
}

View file

@ -16,6 +16,8 @@ type Layer struct {
Command string
Size uint64
Tree *filetree.FileTree
Names []string
Digest string
}
func (l *Layer) ShortId() string {

View file

@ -28,9 +28,6 @@ func NewImageDirectoryRef(img *podmanImage.Image) (*ImageDirectoryRef, error) {
curImg := img
for {
// h, _ := img.History(ctx)
// fmt.Printf("%+v %+v %+v\n", img.ID(), h[0].Size, h[0].CreatedBy)
driver, err := curImg.DriverData()
if err != nil {
return nil, fmt.Errorf("graph driver error: %+v", err)
@ -79,7 +76,8 @@ func processLayer(name, rootDir string) (*filetree.FileTree, error) {
return err
}
// add this file to the tree...
fileInfo := filetree.NewFileInfo(path, "/"+strings.TrimPrefix(path, rootDir), info)
relativeImagePath := "/"+strings.TrimPrefix(strings.TrimPrefix(path, rootDir), "/")
fileInfo := filetree.NewFileInfo(path, relativeImagePath, info)
tree.FileSize += uint64(fileInfo.Size)

View file

@ -2,6 +2,7 @@ package podman
import (
"context"
"fmt"
podmanImage "github.com/containers/libpod/libpod/image"
"github.com/wagoodman/dive/dive/filetree"
"github.com/wagoodman/dive/dive/image"
@ -11,20 +12,42 @@ import (
// Layer represents a Docker image layer and metadata
type layer struct {
obj *podmanImage.Image
history *podmanImage.History
index int
tree *filetree.FileTree
}
func (l *layer) getHistory() (*podmanImage.History, error) {
if l.history != nil {
return l.history, nil
}
history, err := l.obj.History(context.TODO())
if err != nil {
return nil, err
}
if len(history) > 0 {
l.history = history[0]
return history[0], nil
}
return nil, fmt.Errorf("could not find history")
}
func (l *layer) Size() uint64 {
history, err := l.getHistory()
if err != nil {
// todo: what should be done here???
panic(err)
}
return uint64(history.Size)
}
// ShortId returns the truncated id of the current layer.
func (l *layer) Command() string {
history, err := l.obj.History(context.TODO())
history, err := l.getHistory()
if err != nil {
return "error: " + err.Error()
}
if len(history) > 0 {
return strings.TrimPrefix(history[0].CreatedBy, "/bin/sh -c ")
}
return "unknown"
return strings.TrimPrefix(history.CreatedBy, "/bin/sh -c ")
}
// ShortId returns the truncated id of the current layer.
@ -45,7 +68,9 @@ func (l *layer) ToLayer() *image.Layer {
Id: l.obj.ID(),
Index: l.index,
Command: l.Command(),
Size: uint64(l.obj.ImageData.Size),
Size: l.Size(),
Tree: l.tree,
Names: l.obj.Names(),
Digest: l.obj.Digest().String(),
}
}

View file

@ -131,7 +131,13 @@ func (controller *DetailsController) Render() error {
controller.view.Clear()
var lines = make([]string, 0)
lines = append(lines, Formatting.Header("Digest: ")+currentLayer.Id)
if currentLayer.Names != nil && len(currentLayer.Names) > 0 {
lines = append(lines, Formatting.Header("Tags: ")+strings.Join(currentLayer.Names, ", "))
} else {
lines = append(lines, Formatting.Header("Tags: ")+ "(none)")
}
lines = append(lines, Formatting.Header("Id: ")+currentLayer.Id)
lines = append(lines, Formatting.Header("Digest: ")+currentLayer.Digest)
lines = append(lines, Formatting.Header("Command:"))
lines = append(lines, currentLayer.Command)
lines = append(lines, "\n"+Formatting.Header(vtclean.Clean(imageHeaderStr, false)))

View file

@ -210,7 +210,7 @@ func (controller *LayerController) SetCursor(layer int) error {
// currentLayer returns the Layer object currently selected.
func (controller *LayerController) currentLayer() *image.Layer {
return controller.Layers[(len(controller.Layers)-1)-controller.LayerIndex]
return controller.Layers[controller.LayerIndex]
}
// setCompareMode switches the layer comparison between a single-layer comparison to an aggregated comparison.