Skip to content

Commit

Permalink
feat: add layer-wise filesystem information to the analysis json file (
Browse files Browse the repository at this point in the history
…wagoodman#458)

Add layer-wise filesystem information to the analysis which is written to a JSON file
when running dive with `-j` or `--json` flag.

Co-authored-by: Akash Nayak <[email protected]>
  • Loading branch information
joschi and Akash-Nayak committed Nov 7, 2024
1 parent 8a6ee09 commit 0f48a70
Show file tree
Hide file tree
Showing 5 changed files with 4,587 additions and 29 deletions.
18 changes: 9 additions & 9 deletions dive/filetree/file_info.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@ import (

// FileInfo contains tar metadata for a specific FileNode
type FileInfo struct {
Path string
TypeFlag byte
Linkname string
hash uint64
Size int64
Mode os.FileMode
Uid int
Gid int
IsDir bool
Path string `json:"path"`
TypeFlag byte `json:"typeFlag"`
Linkname string `json:"linkName"`
hash uint64 //`json:"hash"`
Size int64 `json:"size"`
Mode os.FileMode `json:"fileMode"`
Uid int `json:"uid"`
Gid int `json:"gid"`
IsDir bool `json:"isDir"`
}

// NewFileInfoFromTarHeader extracts the metadata from a tar header and file contents and generates a new FileInfo object.
Expand Down
2 changes: 1 addition & 1 deletion dive/filetree/node_data.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ var GlobalFileTreeCollapse bool
// NodeData is the payload for a FileNode
type NodeData struct {
ViewInfo ViewInfo
FileInfo FileInfo
FileInfo FileInfo `json:"fileInfo"`
DiffType DiffType
}

Expand Down
14 changes: 14 additions & 0 deletions runtime/export/export.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ package export
import (
"encoding/json"

"github.com/sirupsen/logrus"

"github.com/wagoodman/dive/dive/filetree"
diveImage "github.com/wagoodman/dive/dive/image"
)

Expand All @@ -11,6 +14,7 @@ type export struct {
Image image `json:"image"`
}

// NewExport exports the analysis to a JSON
func NewExport(analysis *diveImage.AnalysisResult) *export {
data := export{
Layer: make([]layer, len(analysis.Layers)),
Expand All @@ -24,12 +28,22 @@ func NewExport(analysis *diveImage.AnalysisResult) *export {

// export layers in order
for idx, curLayer := range analysis.Layers {
layerFileList := make([]filetree.FileInfo, 0)
visitor := func(node *filetree.FileNode) error {
layerFileList = append(layerFileList, node.Data.FileInfo)
return nil
}
err := curLayer.Tree.VisitDepthChildFirst(visitor, nil)
if err != nil {
logrus.Errorf("Unable to propagate layer tree: %+v", err)
}
data.Layer[idx] = layer{
Index: curLayer.Index,
ID: curLayer.Id,
DigestID: curLayer.Digest,
SizeBytes: curLayer.Size,
Command: curLayer.Command,
FileList: layerFileList,
}
}

Expand Down
Loading

0 comments on commit 0f48a70

Please sign in to comment.