go fmt
This commit is contained in:
parent
b86486fdbc
commit
f8106ccec6
1 changed files with 20 additions and 30 deletions
50
images.go
50
images.go
|
@ -30,7 +30,6 @@ type ImagesCommand struct {
|
|||
|
||||
var imagesCommand ImagesCommand
|
||||
|
||||
|
||||
func (x *ImagesCommand) Execute(args []string) error {
|
||||
var images *[]Image
|
||||
|
||||
|
@ -83,7 +82,7 @@ func (x *ImagesCommand) Execute(args []string) error {
|
|||
images = &ims
|
||||
}
|
||||
|
||||
if imagesCommand.Tree || imagesCommand.Dot {
|
||||
if imagesCommand.Tree || imagesCommand.Dot {
|
||||
var startImage *Image
|
||||
if len(args) > 0 {
|
||||
|
||||
|
@ -118,7 +117,7 @@ func (x *ImagesCommand) Execute(args []string) error {
|
|||
return fmt.Errorf("Unable to find image %s = %s.", startImageArg, startImageRepo)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// select the start image of the tree
|
||||
var roots []Image
|
||||
if startImage == nil {
|
||||
|
@ -127,23 +126,23 @@ func (x *ImagesCommand) Execute(args []string) error {
|
|||
startImage.ParentId = ""
|
||||
roots = []Image{*startImage}
|
||||
}
|
||||
|
||||
|
||||
// build helper map (image -> children)
|
||||
var imagesByParent = make(map[string][]Image)
|
||||
imagesByParent = collectChildren(images);
|
||||
|
||||
imagesByParent = collectChildren(images)
|
||||
|
||||
// image ids truncate
|
||||
// initialize image informations
|
||||
|
||||
|
||||
// filter images
|
||||
if imagesCommand.OnlyLabelled{
|
||||
if imagesCommand.OnlyLabelled {
|
||||
*images, imagesByParent = filterImages(images, &imagesByParent)
|
||||
}
|
||||
|
||||
|
||||
var buffer bytes.Buffer
|
||||
|
||||
|
||||
if imagesCommand.Tree {
|
||||
jsonToText(&buffer, imagesCommand.NoTruncate, roots, imagesByParent, "")
|
||||
jsonToText(&buffer, imagesCommand.NoTruncate, roots, imagesByParent, "")
|
||||
}
|
||||
if imagesCommand.Dot {
|
||||
buffer.WriteString("digraph docker {\n")
|
||||
|
@ -151,7 +150,7 @@ func (x *ImagesCommand) Execute(args []string) error {
|
|||
buffer.WriteString(" base [style=invisible]\n}\n")
|
||||
buffer.String()
|
||||
}
|
||||
|
||||
|
||||
fmt.Print(buffer.String())
|
||||
} else if imagesCommand.Short {
|
||||
fmt.Printf(jsonToShort(images))
|
||||
|
@ -162,7 +161,6 @@ func (x *ImagesCommand) Execute(args []string) error {
|
|||
return nil
|
||||
}
|
||||
|
||||
|
||||
func collectChildren(images *[]Image) map[string][]Image {
|
||||
var imagesByParent = make(map[string][]Image)
|
||||
for _, image := range *images {
|
||||
|
@ -172,11 +170,10 @@ func collectChildren(images *[]Image) map[string][]Image {
|
|||
imagesByParent[image.ParentId] = []Image{image}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
return imagesByParent
|
||||
}
|
||||
|
||||
|
||||
func collectRoots(images *[]Image) []Image {
|
||||
var roots []Image
|
||||
for _, image := range *images {
|
||||
|
@ -184,29 +181,28 @@ func collectRoots(images *[]Image) []Image {
|
|||
roots = append(roots, image)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
return roots
|
||||
}
|
||||
|
||||
|
||||
func filterImages (images *[]Image, byParent *map[string][]Image) (filteredImages []Image, filteredChildren map[string][]Image) {
|
||||
for i := 0; i<len(*images); i++ {
|
||||
// image is visible
|
||||
func filterImages(images *[]Image, byParent *map[string][]Image) (filteredImages []Image, filteredChildren map[string][]Image) {
|
||||
for i := 0; i < len(*images); i++ {
|
||||
// image is visible
|
||||
// 1. it has a label
|
||||
// 2. it is root
|
||||
// 3. it is a node
|
||||
// 3. it is a node
|
||||
var visible bool = (*images)[i].RepoTags[0] != "<none>:<none>" || (*images)[i].ParentId == "" || len((*byParent)[(*images)[i].Id]) > 1
|
||||
if visible {
|
||||
filteredImages = append(filteredImages, (*images)[i])
|
||||
} else {
|
||||
// change childs parent id
|
||||
// if items are filtered with only one child
|
||||
for j := 0; j<len(filteredImages); j++ {
|
||||
for j := 0; j < len(filteredImages); j++ {
|
||||
if filteredImages[j].ParentId == (*images)[i].Id {
|
||||
filteredImages[j].ParentId = (*images)[i].ParentId
|
||||
}
|
||||
}
|
||||
for j := 0; j<len(*images); j++ {
|
||||
for j := 0; j < len(*images); j++ {
|
||||
if (*images)[j].ParentId == (*images)[i].Id {
|
||||
(*images)[j].ParentId = (*images)[i].ParentId
|
||||
}
|
||||
|
@ -215,7 +211,7 @@ func filterImages (images *[]Image, byParent *map[string][]Image) (filteredImage
|
|||
}
|
||||
|
||||
filteredChildren = collectChildren(&filteredImages)
|
||||
|
||||
|
||||
return filteredImages, filteredChildren
|
||||
}
|
||||
|
||||
|
@ -245,7 +241,6 @@ func jsonToText(buffer *bytes.Buffer, noTrunc bool, images []Image, byParent map
|
|||
}
|
||||
}
|
||||
|
||||
|
||||
func PrintTreeNode(buffer *bytes.Buffer, noTrunc bool, image Image, prefix string) {
|
||||
var imageID string
|
||||
if noTrunc {
|
||||
|
@ -280,12 +275,10 @@ func humanSize(raw int64) string {
|
|||
return fmt.Sprintf("%.01f %s", rawFloat, sizes[ind])
|
||||
}
|
||||
|
||||
|
||||
func truncate(id string) string {
|
||||
return id[0:12]
|
||||
}
|
||||
|
||||
|
||||
func parseImagesJSON(rawJSON []byte) (*[]Image, error) {
|
||||
|
||||
var images []Image
|
||||
|
@ -298,7 +291,6 @@ func parseImagesJSON(rawJSON []byte) (*[]Image, error) {
|
|||
return &images, nil
|
||||
}
|
||||
|
||||
|
||||
func imagesToDot(buffer *bytes.Buffer, images []Image, byParent map[string][]Image) {
|
||||
for _, image := range images {
|
||||
if image.ParentId == "" {
|
||||
|
@ -315,7 +307,6 @@ func imagesToDot(buffer *bytes.Buffer, images []Image, byParent map[string][]Ima
|
|||
}
|
||||
}
|
||||
|
||||
|
||||
func jsonToShort(images *[]Image) string {
|
||||
var buffer bytes.Buffer
|
||||
|
||||
|
@ -347,7 +338,6 @@ func jsonToShort(images *[]Image) string {
|
|||
return buffer.String()
|
||||
}
|
||||
|
||||
|
||||
func init() {
|
||||
parser.AddCommand("images",
|
||||
"Visualize docker images.",
|
||||
|
|
Loading…
Reference in a new issue