Skip to content

Commit

Permalink
rework images JSON
Browse files Browse the repository at this point in the history
  • Loading branch information
justone committed Nov 6, 2013
1 parent 6113e1d commit 15867ff
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 31 deletions.
6 changes: 3 additions & 3 deletions api_params.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ type APIHistory struct {
}

type APIImages struct {
Repository string `json:",omitempty"`
Tag string `json:",omitempty"`
ID string `json:"Id"`
ID string `json:"Id"`
RepoTags []string `json:",omitempty"`
Created int64
Size int64
VirtualSize int64
ParentId string `json:",omitempty"`
}

type APIInfo struct {
Expand Down
34 changes: 18 additions & 16 deletions commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -1097,27 +1097,29 @@ func (cli *DockerCli) CmdImages(args ...string) error {
fmt.Fprintln(w, "REPOSITORY\tTAG\tID\tCREATED\tSIZE")
}

var repo string
var tag string
for _, out := range outs {
if out.Repository == "" {
out.Repository = "<none>"
}
if out.Tag == "" {
out.Tag = "<none>"
}
for _, repotag := range out.RepoTags {

if !*noTrunc {
out.ID = utils.TruncateID(out.ID)
}
components := strings.SplitN(repotag, ":", 2)
repo = components[0]
tag = components[1]

if !*quiet {
fmt.Fprintf(w, "%s\t%s\t%s\t%s ago\t", out.Repository, out.Tag, out.ID, utils.HumanDuration(time.Now().Sub(time.Unix(out.Created, 0))))
if out.VirtualSize > 0 {
fmt.Fprintf(w, "%s (virtual %s)\n", utils.HumanSize(out.Size), utils.HumanSize(out.VirtualSize))
if !*noTrunc {
out.ID = utils.TruncateID(out.ID)
}

if !*quiet {
fmt.Fprintf(w, "%s\t%s\t%s\t%s ago\t", repo, tag, out.ID, utils.HumanDuration(time.Now().Sub(time.Unix(out.Created, 0))))
if out.VirtualSize > 0 {
fmt.Fprintf(w, "%s (virtual %s)\n", utils.HumanSize(out.Size), utils.HumanSize(out.VirtualSize))
} else {
fmt.Fprintf(w, "%s\n", utils.HumanSize(out.Size))
}
} else {
fmt.Fprintf(w, "%s\n", utils.HumanSize(out.Size))
fmt.Fprintln(w, out.ID)
}
} else {
fmt.Fprintln(w, out.ID)
}
}

Expand Down
39 changes: 28 additions & 11 deletions server.go
Original file line number Diff line number Diff line change
Expand Up @@ -284,31 +284,48 @@ func (srv *Server) Images(all bool, filter string) ([]APIImages, error) {
if err != nil {
return nil, err
}
outs := []APIImages{} //produce [] when empty instead of 'null'
lookup := make(map[string]APIImages)
for name, repository := range srv.runtime.repositories.Repositories {
if filter != "" {
if match, _ := path.Match(filter, name); !match {
continue
}
}
for tag, id := range repository {
var out APIImages
image, err := srv.runtime.graph.Get(id)
if err != nil {
log.Printf("Warning: couldn't load %s from %s/%s: %s", id, name, tag, err)
continue
}
delete(allImages, id)
out.Repository = name
out.Tag = tag
out.ID = image.ID
out.Created = image.Created.Unix()
out.Size = image.Size
out.VirtualSize = image.getParentsSize(0) + image.Size
outs = append(outs, out)

if out, exists := lookup[id]; exists {
out.RepoTags = append(out.RepoTags, fmt.Sprintf("%s:%s", name, tag))

lookup[id] = out
} else {
var out APIImages

delete(allImages, id)

out.ParentId = image.Parent
out.RepoTags = []string{fmt.Sprintf("%s:%s", name, tag)}
out.ID = image.ID
out.Created = image.Created.Unix()
out.Size = image.Size
out.VirtualSize = image.getParentsSize(0) + image.Size

lookup[id] = out
}

}
}
// Display images which aren't part of a

outs := make([]APIImages, 0, len(lookup))
for _, value := range lookup {
outs = append(outs, value)
}

// Display images which aren't part of a repository/tag
if filter == "" {
for _, image := range allImages {
var out APIImages
Expand Down
2 changes: 1 addition & 1 deletion sorter.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func (s *imageSorter) Less(i, j int) bool {
// Sort []ApiImages by most recent creation date and tag name.
func sortImagesByCreationAndTag(images []APIImages) {
creationAndTag := func(i1, i2 *APIImages) bool {
return i1.Created > i2.Created || (i1.Created == i2.Created && i2.Tag > i1.Tag)
return i1.Created > i2.Created
}

sorter := &imageSorter{
Expand Down

0 comments on commit 15867ff

Please sign in to comment.