File: layers.go

package info (click to toggle)
golang-github-containers-storage 1.59.1%2Bds1-2
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 4,184 kB
  • sloc: sh: 630; ansic: 389; makefile: 143; awk: 12
file content (153 lines) | stat: -rw-r--r-- 4,104 bytes parent folder | download | duplicates (2)
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
package main

import (
	"encoding/json"
	"fmt"
	"os"

	"github.com/containers/storage"
	"github.com/containers/storage/pkg/mflag"
)

var (
	listLayersTree  = false
	listLayersQuick = false
)

func storageLayers(flags *mflag.FlagSet, action string, m storage.Store, args []string) (int, error) {
	driver, err := m.GraphDriver()
	if err != nil {
		return 1, err
	}
	layers, err := driver.ListLayers()
	if err != nil {
		return 1, err
	}
	if jsonOutput {
		if err := json.NewEncoder(os.Stdout).Encode(layers); err != nil {
			return 1, err
		}
		return 0, nil
	}
	for _, layer := range layers {
		fmt.Printf("%s\n", layer)
	}
	return 0, nil
}

func layers(flags *mflag.FlagSet, action string, m storage.Store, args []string) (int, error) {
	layers, err := m.Layers()
	if err != nil {
		return 1, err
	}
	if jsonOutput {
		return outputJSON(layers)
	}
	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:  layer.Parent,
				right: 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)
			if listLayersQuick {
				continue
			}
			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, nil
}

func init() {
	commands = append(commands, command{
		names:       []string{"layers"},
		optionsHelp: "[options [...]]",
		usage:       "List layers",
		action:      layers,
		minArgs:     0,
		maxArgs:     0,
		addFlags: func(flags *mflag.FlagSet, cmd *command) {
			flags.BoolVar(&listLayersTree, []string{"-tree", "t"}, listLayersTree, "Use a tree")
			flags.BoolVar(&listLayersQuick, []string{"-quick", "q"}, listLayersTree, "Just the IDs")
			flags.BoolVar(&jsonOutput, []string{"-json", "j"}, jsonOutput, "Prefer JSON output")
		},
	})
	commands = append(commands, command{
		names:       []string{"storage-layers"},
		optionsHelp: "[options [...]]",
		usage:       "List storage layers",
		action:      storageLayers,
		minArgs:     0,
		maxArgs:     0,
		addFlags: func(flags *mflag.FlagSet, cmd *command) {
			flags.BoolVar(&jsonOutput, []string{"-json", "j"}, jsonOutput, "Prefer JSON output")
		},
	})
}