small refactoring, tests work again

This commit is contained in:
Nate Jones 2015-11-07 20:02:21 -08:00
parent f8106ccec6
commit f8eb6a8bc7
2 changed files with 78 additions and 44 deletions

View file

@ -85,11 +85,54 @@ func (x *ImagesCommand) Execute(args []string) error {
if imagesCommand.Tree || imagesCommand.Dot { if imagesCommand.Tree || imagesCommand.Dot {
var startImage *Image var startImage *Image
if len(args) > 0 { if len(args) > 0 {
startImage, err = findStartImage(args[0], images)
if err != nil {
return err
}
}
// select the start image of the tree
var roots []Image
if startImage == nil {
roots = collectRoots(images)
} else {
startImage.ParentId = ""
roots = []Image{*startImage}
}
// build helper map (image -> children)
imagesByParent := collectChildren(images)
// filter images
if imagesCommand.OnlyLabelled {
*images, imagesByParent = filterImages(images, &imagesByParent)
}
if imagesCommand.Tree {
fmt.Print(jsonToTree(imagesCommand.NoTruncate, roots, imagesByParent))
}
if imagesCommand.Dot {
fmt.Print(jsonToDot(roots, imagesByParent))
}
} else if imagesCommand.Short {
fmt.Printf(jsonToShort(images))
} else {
return fmt.Errorf("Please specify either --dot, --tree, or --short")
}
return nil
}
func findStartImage(name string, images *[]Image) (*Image, error) {
var startImage *Image
// attempt to find the start image, which can be specified as an // attempt to find the start image, which can be specified as an
// image ID or a repository name // image ID or a repository name
startImageArg := args[0] startImageArg := name
startImageRepo := args[0] startImageRepo := name
// if tag is not defined, find by :latest tag // if tag is not defined, find by :latest tag
if strings.Index(startImageRepo, ":") == -1 { if strings.Index(startImageRepo, ":") == -1 {
@ -114,51 +157,28 @@ func (x *ImagesCommand) Execute(args []string) error {
} }
if startImage == nil { if startImage == nil {
return fmt.Errorf("Unable to find image %s = %s.", startImageArg, startImageRepo) return nil, fmt.Errorf("Unable to find image %s = %s.", startImageArg, startImageRepo)
}
} }
// select the start image of the tree return startImage, nil
var roots []Image
if startImage == nil {
roots = collectRoots(images)
} else {
startImage.ParentId = ""
roots = []Image{*startImage}
}
// build helper map (image -> children)
var imagesByParent = make(map[string][]Image)
imagesByParent = collectChildren(images)
// image ids truncate
// initialize image informations
// filter images
if imagesCommand.OnlyLabelled {
*images, imagesByParent = filterImages(images, &imagesByParent)
} }
func jsonToTree(noTrunc bool, images []Image, byParent map[string][]Image) string {
var buffer bytes.Buffer var buffer bytes.Buffer
if imagesCommand.Tree { jsonToText(&buffer, noTrunc, images, byParent, "")
jsonToText(&buffer, imagesCommand.NoTruncate, roots, imagesByParent, "")
return buffer.String()
} }
if imagesCommand.Dot {
func jsonToDot(roots []Image, byParent map[string][]Image) string {
var buffer bytes.Buffer
buffer.WriteString("digraph docker {\n") buffer.WriteString("digraph docker {\n")
imagesToDot(&buffer, roots, imagesByParent) imagesToDot(&buffer, roots, byParent)
buffer.WriteString(" base [style=invisible]\n}\n") buffer.WriteString(" base [style=invisible]\n}\n")
buffer.String()
}
fmt.Print(buffer.String()) return buffer.String()
} else if imagesCommand.Short {
fmt.Printf(jsonToShort(images))
} else {
return fmt.Errorf("Please specify either --dot, --tree, or --short")
}
return nil
} }
func collectChildren(images *[]Image) map[string][]Image { func collectChildren(images *[]Image) map[string][]Image {

View file

@ -56,7 +56,12 @@ func Test_Dot(t *testing.T) {
for _, dotTest := range dotTests { for _, dotTest := range dotTests {
im, _ := parseImagesJSON([]byte(dotTest.json)) im, _ := parseImagesJSON([]byte(dotTest.json))
result := jsonToDot(im) byParent := collectChildren(im)
roots := collectRoots(im)
// TODO: test start image limiting
result := jsonToDot(roots, byParent)
for _, regexp := range allRegex { for _, regexp := range allRegex {
if !regexp.MatchString(result) { if !regexp.MatchString(result) {
@ -109,7 +114,16 @@ func Test_Tree(t *testing.T) {
for _, treeTest := range treeTests { for _, treeTest := range treeTests {
im, _ := parseImagesJSON([]byte(treeTest.json)) im, _ := parseImagesJSON([]byte(treeTest.json))
result := jsonToTree(im, treeTest.startImage, treeTest.noTrunc) byParent := collectChildren(im)
var roots []Image
if len(treeTest.startImage) > 0 {
startImage, _ := findStartImage(treeTest.startImage, im)
startImage.ParentId = ""
roots = []Image{*startImage}
} else {
roots = collectRoots(im)
}
result := jsonToTree(treeTest.noTrunc, roots, byParent)
for _, regexp := range compileRegexps(t, treeTest.regexps) { for _, regexp := range compileRegexps(t, treeTest.regexps) {
if !regexp.MatchString(result) { if !regexp.MatchString(result) {