github.com/dougm/docker@v1.5.0/graph/history.go (about)

     1  package graph
     2  
     3  import (
     4  	"strings"
     5  
     6  	"github.com/docker/docker/engine"
     7  	"github.com/docker/docker/image"
     8  )
     9  
    10  func (s *TagStore) CmdHistory(job *engine.Job) engine.Status {
    11  	if n := len(job.Args); n != 1 {
    12  		return job.Errorf("Usage: %s IMAGE", job.Name)
    13  	}
    14  	name := job.Args[0]
    15  	foundImage, err := s.LookupImage(name)
    16  	if err != nil {
    17  		return job.Error(err)
    18  	}
    19  
    20  	lookupMap := make(map[string][]string)
    21  	for name, repository := range s.Repositories {
    22  		for tag, id := range repository {
    23  			// If the ID already has a reverse lookup, do not update it unless for "latest"
    24  			if _, exists := lookupMap[id]; !exists {
    25  				lookupMap[id] = []string{}
    26  			}
    27  			lookupMap[id] = append(lookupMap[id], name+":"+tag)
    28  		}
    29  	}
    30  
    31  	outs := engine.NewTable("Created", 0)
    32  	err = foundImage.WalkHistory(func(img *image.Image) error {
    33  		out := &engine.Env{}
    34  		out.SetJson("Id", img.ID)
    35  		out.SetInt64("Created", img.Created.Unix())
    36  		out.Set("CreatedBy", strings.Join(img.ContainerConfig.Cmd, " "))
    37  		out.SetList("Tags", lookupMap[img.ID])
    38  		out.SetInt64("Size", img.Size)
    39  		outs.Add(out)
    40  		return nil
    41  	})
    42  	if _, err := outs.WriteListTo(job.Stdout); err != nil {
    43  		return job.Error(err)
    44  	}
    45  	return engine.StatusOK
    46  }