Skip to content

Commit

Permalink
Merge pull request #149 from rst0git/2024-10-23-metadata
Browse files Browse the repository at this point in the history
Recursively add metadata JSON fields to the tree
  • Loading branch information
adrianreber authored Oct 23, 2024
2 parents c770752 + 2e305b9 commit 9a448d5
Showing 1 changed file with 36 additions and 3 deletions.
39 changes: 36 additions & 3 deletions internal/tree.go
Original file line number Diff line number Diff line change
Expand Up @@ -335,9 +335,8 @@ func addPodInfoToTree(tree treeprint.Tree, info *checkpointInfo) {
continue
}
localTree := annotationTree.AddBranch(key)
for labelKey := range local {
localTree.AddBranch(fmt.Sprintf("%s: %s", labelKey, local[labelKey]))
}
// Recursively add array/map JSON fields to the tree
addMapToTree(localTree, local)
case "io.kubernetes.cri-o.Volumes":
// We know that some annotations contain a JSON string we can pretty print
var local []mountAnnotations
Expand All @@ -359,3 +358,37 @@ func addPodInfoToTree(tree treeprint.Tree, info *checkpointInfo) {
}
}
}

func addMapToTree(tree treeprint.Tree, data map[string]interface{}) {
for key, value := range data {
switch v := value.(type) {
case map[string]interface{}:
// Recursively add nested maps
subTree := tree.AddBranch(fmt.Sprintf("%s:", key))
addMapToTree(subTree, v)
case []interface{}:
// Handle arrays recursively
arrayTree := tree.AddBranch(fmt.Sprintf("%s: ", key))
addArrayToTree(arrayTree, v)
default:
tree.AddBranch(fmt.Sprintf("%s: %v", key, v))
}
}
}

func addArrayToTree(tree treeprint.Tree, data []interface{}) {
for i, value := range data {
switch v := value.(type) {
case map[string]interface{}:
// Recursively add maps inside arrays
subTree := tree.AddBranch(fmt.Sprintf("[%d]:", i))
addMapToTree(subTree, v)
case []interface{}:
// Recursively add arrays inside arrays
subArrayTree := tree.AddBranch(fmt.Sprintf("[%d]: ", i))
addArrayToTree(subArrayTree, v)
default:
tree.AddBranch(fmt.Sprintf("[%d]: %v", i, v))
}
}
}

0 comments on commit 9a448d5

Please sign in to comment.