Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: add a layers subtree for image layers #7

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions ociutils.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"fmt"
"log"
"os"
"os/exec"
"path/filepath"
"strings"
"text/tabwriter"
Expand Down Expand Up @@ -121,6 +122,46 @@ func (sr subIndexRef) summary() string {
return fmt.Sprintf("sub-index with %d manifests", len(info.manifestDescriptors))
}

type layerRef struct {
blobfilepath string
hash string
displayString string
mediaType string
}

var LayerSummaryCache = map[string]string{}

func (lr layerRef) summary(filter string) string {
layerfilterkey := lr.blobfilepath + "\\" + filter
cachedSummary, ok := LayerSummaryCache[layerfilterkey]
if ok {
return cachedSummary
}

filterArg := ""
if filter != "" {
filterArg = " | grep " + filter
}

cmdstr := "tar tzvf " + lr.blobfilepath + filterArg
if strings.Contains(lr.mediaType, "squashfs") {
cmdstr = "echo TODO: gunzip -c " + lr.blobfilepath + " |unsquashfs -llc "
//TODO: can't unsquash via pipe, need to gunzip to tempfile and delete
}
cmd := exec.Command("sh", "-c", cmdstr)
var out strings.Builder
var stderr strings.Builder
cmd.Stdout = &out
cmd.Stderr = &stderr
err := cmd.Run()
if err != nil {
log.Printf("error: %v", err)
}
summaryString := fmt.Sprintf("%q\n%q\n%s\n%s", lr.displayString, cmdstr, out.String(), stderr.String())
LayerSummaryCache[layerfilterkey] = summaryString
return summaryString
}

// ImageInfoMap - global map of manifest hashes to info
var ImageInfoMap = map[string]imageInfo{}

Expand Down
61 changes: 59 additions & 2 deletions ui.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,27 @@ func addContentsOfLayout(target *tview.TreeNode, path string) ([]imageInfo, []su

node.AddChild(refNode)
}

layerTreeNode := tview.NewTreeNode("layers").
SetReference(imageInfo.ref).
SetSelectable(true)
node.AddChild(layerTreeNode)

for idx, layerDigest := range imageInfo.layerDigests {
displayString := layerDigest
if len(LayerNameMap[layerDigest]) > 0 {
displayString = strings.Join(LayerNameMap[layerDigest], ",")
}
blobfilepath := filepath.Join(path, "blobs", "sha256", layerDigest)
mt := imageInfo.manifest.Layers[idx].MediaType
newLayerRef := layerRef{hash: layerDigest, mediaType: mt,
blobfilepath: blobfilepath, displayString: displayString}
layerNode := tview.NewTreeNode(displayString).
SetReference(newLayerRef).
SetSelectable(true)
layerTreeNode.AddChild(layerNode)
}

target.AddChild(node)
log.Printf(" done loading image %q", imageInfo.displayName)
}
Expand Down Expand Up @@ -382,6 +403,9 @@ func getMatchingTreeNodes(node *tview.TreeNode, needle string) []*tview.TreeNode
case imageref:
haystacks = reference.(imageref).searchString()

case layerRef:
haystacks = []string{ref.hash, ref.displayString}

case subIndexRef:
haystacks = []string{}
info := SubIndexInfoMap[ref.hash]
Expand Down Expand Up @@ -435,6 +459,8 @@ func clearTreeFormatting(node *tview.TreeNode, selectable bool) {
node.SetColor(tcell.ColorBlue)
case imageref:
node.SetColor(tcell.ColorRed)
case layerRef:
node.SetColor(tcell.ColorGreen)
case subIndexRef:
node.SetColor(tcell.ColorBlue)
default:
Expand Down Expand Up @@ -550,7 +576,32 @@ func doTViewStuff(ctxt *cli.Context) error {
SetText(strings.Join(summaries, "\n")).
SetDynamicColors(true).
SetRegions(true)

infoPane.Box.SetBorder(true)
currentFilter := ""
summaryFilterField := tview.NewInputField().
SetLabel("Filter Output: ").
SetChangedFunc(func(needle string) {

currentFilter = needle

reference := tree.GetCurrentNode().GetReference()
switch ref := reference.(type) {
case layerRef:
infoPane.SetText(ref.summary(currentFilter))
}

// update info pane with summaries
})
summaryFilterField.SetDoneFunc(func(key tcell.Key) {
app.SetFocus(infoPane)
})

summaryFilterField.Box.SetBorder(true)

infoPaneGrid := tview.NewGrid().SetRows(0, 3).SetColumns(0).
AddItem(infoPane, 0, 0, 1, 1, 0, 0, true).
AddItem(summaryFilterField, 1, 0, 1, 1, 0, 0, false)

statusLine := tview.NewTextView().
SetTextAlign(tview.AlignCenter).
Expand All @@ -571,6 +622,8 @@ func doTViewStuff(ctxt *cli.Context) error {
infoPane.ScrollToBeginning()
case treeInfo:
infoPane.SetText(tview.Escape(ref.summary()))
case layerRef:
infoPane.SetText(ref.summary(currentFilter))
case subIndexRef:
infoPane.SetText(ref.summary())
default:
Expand All @@ -584,6 +637,10 @@ func doTViewStuff(ctxt *cli.Context) error {
case treeInfo:
infoPane.SetText(ref.summary())
infoPane.ScrollToBeginning()
case layerRef:
// todo mmcc didn't think through this behavior:
infoPane.SetText(ref.summary(currentFilter))
infoPane.ScrollToBeginning()
case subIndexRef:
infoPane.SetText(ref.summary())
default:
Expand Down Expand Up @@ -634,10 +691,10 @@ func doTViewStuff(ctxt *cli.Context) error {
SetRows(0, 1).
SetColumns(-1, -3).
AddItem(treeGrid, 0, 0, 1, 1, 0, 0, true).
AddItem(infoPane, 0, 1, 1, 1, 0, 0, false).
AddItem(infoPaneGrid, 0, 1, 1, 1, 0, 0, false).
AddItem(statusLine, 1, 0, 1, 2, 0, 0, false)

tabbableViews := []tview.Primitive{tree, searchInputField, infoPane}
tabbableViews := []tview.Primitive{tree, searchInputField, infoPane, summaryFilterField}
tabbableViewIdx := 0

setNewFocusedViewIdx := func(prevIdx int, newIdx int) {
Expand Down
Loading