mirror of https://github.com/docker/docs.git
Merge pull request #12333 from Mashimiao/clean-up-viz
Clean up viz code
This commit is contained in:
commit
572ba9cb40
|
@ -4,7 +4,6 @@ import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"net/url"
|
"net/url"
|
||||||
"strings"
|
|
||||||
"text/tabwriter"
|
"text/tabwriter"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
@ -18,74 +17,6 @@ import (
|
||||||
"github.com/docker/docker/utils"
|
"github.com/docker/docker/utils"
|
||||||
)
|
)
|
||||||
|
|
||||||
// FIXME: --viz and --tree are deprecated. Remove them in a future version.
|
|
||||||
func (cli *DockerCli) walkTree(noTrunc bool, images []*types.Image, byParent map[string][]*types.Image, prefix string, printNode func(cli *DockerCli, noTrunc bool, image *types.Image, prefix string)) {
|
|
||||||
length := len(images)
|
|
||||||
if length > 1 {
|
|
||||||
for index, image := range images {
|
|
||||||
if index+1 == length {
|
|
||||||
printNode(cli, noTrunc, image, prefix+"└─")
|
|
||||||
if subimages, exists := byParent[image.ID]; exists {
|
|
||||||
cli.walkTree(noTrunc, subimages, byParent, prefix+" ", printNode)
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
printNode(cli, noTrunc, image, prefix+"\u251C─")
|
|
||||||
if subimages, exists := byParent[image.ID]; exists {
|
|
||||||
cli.walkTree(noTrunc, subimages, byParent, prefix+"\u2502 ", printNode)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
for _, image := range images {
|
|
||||||
printNode(cli, noTrunc, image, prefix+"└─")
|
|
||||||
if subimages, exists := byParent[image.ID]; exists {
|
|
||||||
cli.walkTree(noTrunc, subimages, byParent, prefix+" ", printNode)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// FIXME: --viz and --tree are deprecated. Remove them in a future version.
|
|
||||||
func (cli *DockerCli) printVizNode(noTrunc bool, image *types.Image, prefix string) {
|
|
||||||
var (
|
|
||||||
imageID string
|
|
||||||
parentID string
|
|
||||||
)
|
|
||||||
if noTrunc {
|
|
||||||
imageID = image.ID
|
|
||||||
parentID = image.ParentId
|
|
||||||
} else {
|
|
||||||
imageID = stringid.TruncateID(image.ID)
|
|
||||||
parentID = stringid.TruncateID(image.ParentId)
|
|
||||||
}
|
|
||||||
if parentID == "" {
|
|
||||||
fmt.Fprintf(cli.out, " base -> \"%s\" [style=invis]\n", imageID)
|
|
||||||
} else {
|
|
||||||
fmt.Fprintf(cli.out, " \"%s\" -> \"%s\"\n", parentID, imageID)
|
|
||||||
}
|
|
||||||
if image.RepoTags[0] != "<none>:<none>" {
|
|
||||||
fmt.Fprintf(cli.out, " \"%s\" [label=\"%s\\n%s\",shape=box,fillcolor=\"paleturquoise\",style=\"filled,rounded\"];\n",
|
|
||||||
imageID, imageID, strings.Join(image.RepoTags, "\\n"))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// FIXME: --viz and --tree are deprecated. Remove them in a future version.
|
|
||||||
func (cli *DockerCli) printTreeNode(noTrunc bool, image *types.Image, prefix string) {
|
|
||||||
var imageID string
|
|
||||||
if noTrunc {
|
|
||||||
imageID = image.ID
|
|
||||||
} else {
|
|
||||||
imageID = stringid.TruncateID(image.ID)
|
|
||||||
}
|
|
||||||
|
|
||||||
fmt.Fprintf(cli.out, "%s%s Virtual Size: %s", prefix, imageID, units.HumanSize(float64(image.VirtualSize)))
|
|
||||||
if image.RepoTags[0] != "<none>:<none>" {
|
|
||||||
fmt.Fprintf(cli.out, " Tags: %s\n", strings.Join(image.RepoTags, ", "))
|
|
||||||
} else {
|
|
||||||
fmt.Fprint(cli.out, "\n")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// CmdImages lists the images in a specified repository, or all top-level images if no repository is specified.
|
// CmdImages lists the images in a specified repository, or all top-level images if no repository is specified.
|
||||||
//
|
//
|
||||||
// Usage: docker images [OPTIONS] [REPOSITORY]
|
// Usage: docker images [OPTIONS] [REPOSITORY]
|
||||||
|
@ -95,9 +26,6 @@ func (cli *DockerCli) CmdImages(args ...string) error {
|
||||||
all := cmd.Bool([]string{"a", "-all"}, false, "Show all images (default hides intermediate images)")
|
all := cmd.Bool([]string{"a", "-all"}, false, "Show all images (default hides intermediate images)")
|
||||||
noTrunc := cmd.Bool([]string{"#notrunc", "-no-trunc"}, false, "Don't truncate output")
|
noTrunc := cmd.Bool([]string{"#notrunc", "-no-trunc"}, false, "Don't truncate output")
|
||||||
showDigests := cmd.Bool([]string{"-digests"}, false, "Show digests")
|
showDigests := cmd.Bool([]string{"-digests"}, false, "Show digests")
|
||||||
// FIXME: --viz and --tree are deprecated. Remove them in a future version.
|
|
||||||
flViz := cmd.Bool([]string{"#v", "#viz", "#-viz"}, false, "Output graph in graphviz format")
|
|
||||||
flTree := cmd.Bool([]string{"#t", "#tree", "#-tree"}, false, "Output graph in tree format")
|
|
||||||
|
|
||||||
flFilter := opts.NewListOpts(nil)
|
flFilter := opts.NewListOpts(nil)
|
||||||
cmd.Var(&flFilter, []string{"f", "-filter"}, "Filter output based on conditions provided")
|
cmd.Var(&flFilter, []string{"f", "-filter"}, "Filter output based on conditions provided")
|
||||||
|
@ -116,158 +44,83 @@ func (cli *DockerCli) CmdImages(args ...string) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
matchName := cmd.Arg(0)
|
matchName := cmd.Arg(0)
|
||||||
// FIXME: --viz and --tree are deprecated. Remove them in a future version.
|
v := url.Values{}
|
||||||
if *flViz || *flTree {
|
if len(imageFilterArgs) > 0 {
|
||||||
v := url.Values{
|
filterJSON, err := filters.ToParam(imageFilterArgs)
|
||||||
"all": []string{"1"},
|
|
||||||
}
|
|
||||||
if len(imageFilterArgs) > 0 {
|
|
||||||
filterJSON, err := filters.ToParam(imageFilterArgs)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
v.Set("filters", filterJSON)
|
|
||||||
}
|
|
||||||
|
|
||||||
rdr, _, err := cli.call("GET", "/images/json?"+v.Encode(), nil, nil)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
v.Set("filters", filterJSON)
|
||||||
|
}
|
||||||
|
|
||||||
images := []types.Image{}
|
if cmd.NArg() == 1 {
|
||||||
err = json.NewDecoder(rdr).Decode(&images)
|
// FIXME rename this parameter, to not be confused with the filters flag
|
||||||
if err != nil {
|
v.Set("filter", matchName)
|
||||||
return err
|
}
|
||||||
}
|
if *all {
|
||||||
|
v.Set("all", "1")
|
||||||
|
}
|
||||||
|
|
||||||
var (
|
rdr, _, err := cli.call("GET", "/images/json?"+v.Encode(), nil, nil)
|
||||||
printNode func(cli *DockerCli, noTrunc bool, image *types.Image, prefix string)
|
if err != nil {
|
||||||
startImage *types.Image
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
roots = []*types.Image{}
|
images := []types.Image{}
|
||||||
byParent = make(map[string][]*types.Image)
|
if err := json.NewDecoder(rdr).Decode(&images); err != nil {
|
||||||
)
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
for _, image := range images {
|
w := tabwriter.NewWriter(cli.out, 20, 1, 3, ' ', 0)
|
||||||
if image.ParentId == "" {
|
if !*quiet {
|
||||||
roots = append(roots, &image)
|
if *showDigests {
|
||||||
} else {
|
fmt.Fprintln(w, "REPOSITORY\tTAG\tDIGEST\tIMAGE ID\tCREATED\tVIRTUAL SIZE")
|
||||||
if children, exists := byParent[image.ParentId]; exists {
|
|
||||||
children = append(children, &image)
|
|
||||||
} else {
|
|
||||||
byParent[image.ParentId] = []*types.Image{&image}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if matchName != "" {
|
|
||||||
if matchName == image.ID || matchName == stringid.TruncateID(image.ID) {
|
|
||||||
startImage = &image
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, repotag := range image.RepoTags {
|
|
||||||
if repotag == matchName {
|
|
||||||
startImage = &image
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if *flViz {
|
|
||||||
fmt.Fprintf(cli.out, "digraph docker {\n")
|
|
||||||
printNode = (*DockerCli).printVizNode
|
|
||||||
} else {
|
} else {
|
||||||
printNode = (*DockerCli).printTreeNode
|
fmt.Fprintln(w, "REPOSITORY\tTAG\tIMAGE ID\tCREATED\tVIRTUAL SIZE")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, image := range images {
|
||||||
|
ID := image.ID
|
||||||
|
if !*noTrunc {
|
||||||
|
ID = stringid.TruncateID(ID)
|
||||||
}
|
}
|
||||||
|
|
||||||
if startImage != nil {
|
repoTags := image.RepoTags
|
||||||
root := []*types.Image{startImage}
|
repoDigests := image.RepoDigests
|
||||||
cli.walkTree(*noTrunc, root, byParent, "", printNode)
|
|
||||||
} else if matchName == "" {
|
if len(repoTags) == 1 && repoTags[0] == "<none>:<none>" && len(repoDigests) == 1 && repoDigests[0] == "<none>@<none>" {
|
||||||
cli.walkTree(*noTrunc, roots, byParent, "", printNode)
|
// dangling image - clear out either repoTags or repoDigsts so we only show it once below
|
||||||
}
|
repoDigests = []string{}
|
||||||
if *flViz {
|
|
||||||
fmt.Fprintf(cli.out, " base [style=invisible]\n}\n")
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
v := url.Values{}
|
|
||||||
if len(imageFilterArgs) > 0 {
|
|
||||||
filterJSON, err := filters.ToParam(imageFilterArgs)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
v.Set("filters", filterJSON)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if cmd.NArg() == 1 {
|
// combine the tags and digests lists
|
||||||
// FIXME rename this parameter, to not be confused with the filters flag
|
tagsAndDigests := append(repoTags, repoDigests...)
|
||||||
v.Set("filter", matchName)
|
for _, repoAndRef := range tagsAndDigests {
|
||||||
}
|
repo, ref := parsers.ParseRepositoryTag(repoAndRef)
|
||||||
if *all {
|
// default tag and digest to none - if there's a value, it'll be set below
|
||||||
v.Set("all", "1")
|
tag := "<none>"
|
||||||
}
|
digest := "<none>"
|
||||||
|
if utils.DigestReference(ref) {
|
||||||
rdr, _, err := cli.call("GET", "/images/json?"+v.Encode(), nil, nil)
|
digest = ref
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
images := []types.Image{}
|
|
||||||
err = json.NewDecoder(rdr).Decode(&images)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
w := tabwriter.NewWriter(cli.out, 20, 1, 3, ' ', 0)
|
|
||||||
if !*quiet {
|
|
||||||
if *showDigests {
|
|
||||||
fmt.Fprintln(w, "REPOSITORY\tTAG\tDIGEST\tIMAGE ID\tCREATED\tVIRTUAL SIZE")
|
|
||||||
} else {
|
} else {
|
||||||
fmt.Fprintln(w, "REPOSITORY\tTAG\tIMAGE ID\tCREATED\tVIRTUAL SIZE")
|
tag = ref
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, image := range images {
|
|
||||||
ID := image.ID
|
|
||||||
if !*noTrunc {
|
|
||||||
ID = stringid.TruncateID(ID)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
repoTags := image.RepoTags
|
if !*quiet {
|
||||||
repoDigests := image.RepoDigests
|
if *showDigests {
|
||||||
|
fmt.Fprintf(w, "%s\t%s\t%s\t%s\t%s ago\t%s\n", repo, tag, digest, ID, units.HumanDuration(time.Now().UTC().Sub(time.Unix(int64(image.Created), 0))), units.HumanSize(float64(image.VirtualSize)))
|
||||||
if len(repoTags) == 1 && repoTags[0] == "<none>:<none>" && len(repoDigests) == 1 && repoDigests[0] == "<none>@<none>" {
|
|
||||||
// dangling image - clear out either repoTags or repoDigsts so we only show it once below
|
|
||||||
repoDigests = []string{}
|
|
||||||
}
|
|
||||||
|
|
||||||
// combine the tags and digests lists
|
|
||||||
tagsAndDigests := append(repoTags, repoDigests...)
|
|
||||||
for _, repoAndRef := range tagsAndDigests {
|
|
||||||
repo, ref := parsers.ParseRepositoryTag(repoAndRef)
|
|
||||||
// default tag and digest to none - if there's a value, it'll be set below
|
|
||||||
tag := "<none>"
|
|
||||||
digest := "<none>"
|
|
||||||
if utils.DigestReference(ref) {
|
|
||||||
digest = ref
|
|
||||||
} else {
|
} else {
|
||||||
tag = ref
|
fmt.Fprintf(w, "%s\t%s\t%s\t%s ago\t%s\n", repo, tag, ID, units.HumanDuration(time.Now().UTC().Sub(time.Unix(int64(image.Created), 0))), units.HumanSize(float64(image.VirtualSize)))
|
||||||
}
|
|
||||||
|
|
||||||
if !*quiet {
|
|
||||||
if *showDigests {
|
|
||||||
fmt.Fprintf(w, "%s\t%s\t%s\t%s\t%s ago\t%s\n", repo, tag, digest, ID, units.HumanDuration(time.Now().UTC().Sub(time.Unix(int64(image.Created), 0))), units.HumanSize(float64(image.VirtualSize)))
|
|
||||||
} else {
|
|
||||||
fmt.Fprintf(w, "%s\t%s\t%s\t%s ago\t%s\n", repo, tag, ID, units.HumanDuration(time.Now().UTC().Sub(time.Unix(int64(image.Created), 0))), units.HumanSize(float64(image.VirtualSize)))
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
fmt.Fprintln(w, ID)
|
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
fmt.Fprintln(w, ID)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if !*quiet {
|
if !*quiet {
|
||||||
w.Flush()
|
w.Flush()
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
|
@ -406,15 +406,6 @@ func (s *Server) getImagesJSON(eng *engine.Engine, version version.Version, w ht
|
||||||
return writeJSON(w, http.StatusOK, legacyImages)
|
return writeJSON(w, http.StatusOK, legacyImages)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Server) getImagesViz(eng *engine.Engine, version version.Version, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
|
|
||||||
if version.GreaterThan("1.6") {
|
|
||||||
w.WriteHeader(http.StatusNotFound)
|
|
||||||
return fmt.Errorf("This is now implemented in the client.")
|
|
||||||
}
|
|
||||||
eng.ServeHTTP(w, r)
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s *Server) getInfo(eng *engine.Engine, version version.Version, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
|
func (s *Server) getInfo(eng *engine.Engine, version version.Version, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
|
||||||
w.Header().Set("Content-Type", "application/json")
|
w.Header().Set("Content-Type", "application/json")
|
||||||
|
|
||||||
|
@ -1589,7 +1580,6 @@ func createRouter(s *Server, eng *engine.Engine) *mux.Router {
|
||||||
"/info": s.getInfo,
|
"/info": s.getInfo,
|
||||||
"/version": s.getVersion,
|
"/version": s.getVersion,
|
||||||
"/images/json": s.getImagesJSON,
|
"/images/json": s.getImagesJSON,
|
||||||
"/images/viz": s.getImagesViz,
|
|
||||||
"/images/search": s.getImagesSearch,
|
"/images/search": s.getImagesSearch,
|
||||||
"/images/get": s.getImagesGet,
|
"/images/get": s.getImagesGet,
|
||||||
"/images/{name:.*}/get": s.getImagesGet,
|
"/images/{name:.*}/get": s.getImagesGet,
|
||||||
|
|
Loading…
Reference in New Issue