1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
|
package main
import (
"encoding/json"
"fmt"
"os"
"github.com/containers/storage"
"github.com/containers/storage/pkg/mflag"
)
var listLayersTree = false
func layers(flags *mflag.FlagSet, action string, m storage.Store, args []string) int {
layers, err := m.Layers()
if err != nil {
fmt.Fprintf(os.Stderr, "%+v\n", err)
return 1
}
if jsonOutput {
json.NewEncoder(os.Stdout).Encode(layers)
return 0
}
imageMap := make(map[string]*[]storage.Image)
if images, err := m.Images(); err == nil {
for _, image := range images {
for _, layerID := range append([]string{image.TopLayer}, image.MappedTopLayers...) {
if ilist, ok := imageMap[layerID]; ok && ilist != nil {
list := append(*ilist, image)
imageMap[layerID] = &list
} else {
list := []storage.Image{image}
imageMap[layerID] = &list
}
}
}
}
containerMap := make(map[string]storage.Container)
if containers, err := m.Containers(); err == nil {
for _, container := range containers {
containerMap[container.LayerID] = container
}
}
nodes := []treeNode{}
for _, layer := range layers {
if listLayersTree {
node := treeNode{
left: string(layer.Parent),
right: string(layer.ID),
notes: []string{},
}
if node.left == "" {
node.left = "(base)"
}
for _, name := range layer.Names {
node.notes = append(node.notes, "name: "+name)
}
if layer.MountPoint != "" {
node.notes = append(node.notes, "mount: "+layer.MountPoint)
}
if imageList, ok := imageMap[layer.ID]; ok && imageList != nil {
for _, image := range *imageList {
node.notes = append(node.notes, fmt.Sprintf("image: %s", image.ID))
for _, name := range image.Names {
node.notes = append(node.notes, fmt.Sprintf("image name: %s", name))
}
}
}
if container, ok := containerMap[layer.ID]; ok {
node.notes = append(node.notes, fmt.Sprintf("container: %s", container.ID))
for _, name := range container.Names {
node.notes = append(node.notes, fmt.Sprintf("container name: %s", name))
}
}
nodes = append(nodes, node)
} else {
fmt.Printf("%s\n", layer.ID)
for _, name := range layer.Names {
fmt.Printf("\tname: %s\n", name)
}
if imageList, ok := imageMap[layer.ID]; ok && imageList != nil {
for _, image := range *imageList {
fmt.Printf("\timage: %s\n", image.ID)
for _, name := range image.Names {
fmt.Printf("\t\tname: %s\n", name)
}
}
}
if container, ok := containerMap[layer.ID]; ok {
fmt.Printf("\tcontainer: %s\n", container.ID)
for _, name := range container.Names {
fmt.Printf("\t\tname: %s\n", name)
}
}
}
}
if listLayersTree {
printTree(nodes)
}
return 0
}
func init() {
commands = append(commands, command{
names: []string{"layers"},
optionsHelp: "[options [...]]",
usage: "List layers",
action: layers,
maxArgs: 0,
addFlags: func(flags *mflag.FlagSet, cmd *command) {
flags.BoolVar(&listLayersTree, []string{"-tree", "t"}, listLayersTree, "Use a tree")
flags.BoolVar(&jsonOutput, []string{"-json", "j"}, jsonOutput, "Prefer JSON output")
},
})
}
|