dive-zfs/dive/image/layer.go
Ohad Lutzky ceb9688d92 Fix rendering for multi-line commands
Heredocs can be used to create multi-line commands, which broke rendering.

Details about heredocs: https://www.docker.com/blog/introduction-to-heredocs-in-dockerfiles/
2023-07-06 10:48:06 -04:00

52 lines
897 B
Go

package image
import (
"fmt"
"strings"
"github.com/dustin/go-humanize"
"github.com/wagoodman/dive/dive/filetree"
)
const (
LayerFormat = "%7s %s"
)
type Layer struct {
Id string
Index int
Command string
Size uint64
Tree *filetree.FileTree
Names []string
Digest string
}
func (l *Layer) ShortId() string {
rangeBound := 15
id := l.Id
if length := len(id); length < 15 {
rangeBound = length
}
id = id[0:rangeBound]
return id
}
func (l *Layer) commandPreview() string {
// Layers using heredocs can be multiple lines; rendering relies on
// Layer.String to be a single line.
return strings.Replace(l.Command, "\n", "↵", -1)
}
func (l *Layer) String() string {
if l.Index == 0 {
return fmt.Sprintf(LayerFormat,
humanize.Bytes(l.Size),
"FROM "+l.ShortId())
}
return fmt.Sprintf(LayerFormat,
humanize.Bytes(l.Size),
l.commandPreview())
}