refactor for testing ease, add a single test
This commit is contained in:
parent
1db432d5fc
commit
4857bfb781
2 changed files with 60 additions and 20 deletions
56
images.go
56
images.go
|
@ -1,6 +1,7 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
|
@ -32,28 +33,10 @@ func (x *ImagesCommand) Execute(args []string) error {
|
||||||
return fmt.Errorf("error reading all input", err)
|
return fmt.Errorf("error reading all input", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
var images []Image
|
images, err := parseJSON(stdin)
|
||||||
err = json.Unmarshal(stdin, &images)
|
|
||||||
|
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("Error reading JSON: ", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
if imagesCommand.Dot {
|
if imagesCommand.Dot {
|
||||||
fmt.Printf("digraph docker {\n")
|
fmt.Printf(jsonToDot(images))
|
||||||
|
|
||||||
for _, image := range images {
|
|
||||||
if image.ParentId == "" {
|
|
||||||
fmt.Printf(" base -> \"%s\" [style=invis]\n", truncate(image.Id))
|
|
||||||
} else {
|
|
||||||
fmt.Printf(" \"%s\" -> \"%s\"\n", truncate(image.ParentId), truncate(image.Id))
|
|
||||||
}
|
|
||||||
if image.RepoTags[0] != "<none>:<none>" {
|
|
||||||
fmt.Printf(" \"%s\" [label=\"%s\\n%s\",shape=box,fillcolor=\"paleturquoise\",style=\"filled,rounded\"];\n", truncate(image.Id), truncate(image.Id), strings.Join(image.RepoTags, "\\n"))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fmt.Printf(" base [style=invisible]\n}\n")
|
|
||||||
} else if imagesCommand.Tree {
|
} else if imagesCommand.Tree {
|
||||||
fmt.Println("Tree output not implemented yet.")
|
fmt.Println("Tree output not implemented yet.")
|
||||||
}
|
}
|
||||||
|
@ -65,6 +48,39 @@ func truncate(id string) string {
|
||||||
return id[0:12]
|
return id[0:12]
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func parseJSON(rawJSON []byte) (*[]Image, error) {
|
||||||
|
|
||||||
|
var images []Image
|
||||||
|
err := json.Unmarshal(rawJSON, &images)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("Error reading JSON: ", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return &images, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func jsonToDot(images *[]Image) string {
|
||||||
|
|
||||||
|
var buffer bytes.Buffer
|
||||||
|
buffer.WriteString("digraph docker {\n")
|
||||||
|
|
||||||
|
for _, image := range *images {
|
||||||
|
if image.ParentId == "" {
|
||||||
|
buffer.WriteString(fmt.Sprintf(" base -> \"%s\" [style=invis]\n", truncate(image.Id)))
|
||||||
|
} else {
|
||||||
|
buffer.WriteString(fmt.Sprintf(" \"%s\" -> \"%s\"\n", truncate(image.ParentId), truncate(image.Id)))
|
||||||
|
}
|
||||||
|
if image.RepoTags[0] != "<none>:<none>" {
|
||||||
|
buffer.WriteString(fmt.Sprintf(" \"%s\" [label=\"%s\\n%s\",shape=box,fillcolor=\"paleturquoise\",style=\"filled,rounded\"];\n", truncate(image.Id), truncate(image.Id), strings.Join(image.RepoTags, "\\n")))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
buffer.WriteString(" base [style=invisible]\n}\n")
|
||||||
|
|
||||||
|
return buffer.String()
|
||||||
|
}
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
parser.AddCommand("images",
|
parser.AddCommand("images",
|
||||||
"Visualize docker images.",
|
"Visualize docker images.",
|
||||||
|
|
24
images_test.go
Normal file
24
images_test.go
Normal file
|
@ -0,0 +1,24 @@
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func Test_Dot(t *testing.T) {
|
||||||
|
json := `[{ "VirtualSize": 662553464, "Size": 662553464, "RepoTags": [ "<none>:<none>" ], "ParentId": "", "Id": "4c1208b690c68af3476b437e7bc2bcc460f062bda2094d2d8f21a7e70368d358", "Created": 1386114144 }]`
|
||||||
|
|
||||||
|
expectedResult := `digraph docker {
|
||||||
|
base -> "4c1208b690c6" [style=invis]
|
||||||
|
base [style=invisible]
|
||||||
|
}
|
||||||
|
`
|
||||||
|
|
||||||
|
im, _ := parseJSON([]byte(json))
|
||||||
|
result := jsonToDot(im)
|
||||||
|
|
||||||
|
if result == expectedResult {
|
||||||
|
t.Log("Pass")
|
||||||
|
} else {
|
||||||
|
t.Errorf("|%s| and |%s| are different.", result, expectedResult)
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue