diff --git a/docker.go b/docker.go index 37166038b1..d55d892a5b 100644 --- a/docker.go +++ b/docker.go @@ -7,7 +7,7 @@ import ( "log" "os" "path" - "github.com/dotcloud/docker/image" // For sorting utility + "sort" ) type Docker struct { @@ -17,15 +17,11 @@ type Docker struct { } func (docker *Docker) List() []*Container { - history := new(image.History) + containers := new(History) for e := docker.containers.Front(); e != nil; e = e.Next() { - history.Add(e.Value.(*Container)) + containers.Add(e.Value.(*Container)) } - containers := make([]*Container, len(*history)) - for i := range *history { - containers[i] = (*history)[i].(*Container) - } - return containers + return *containers } func (docker *Docker) getContainerElement(id string) *list.Element { @@ -120,3 +116,28 @@ func NewFromDirectory(root string) (*Docker, error) { } return docker, nil } + + +type History []*Container + +func (history *History) Len() int { + return len(*history) +} + +func (history *History) Less(i, j int) bool { + containers := *history + return containers[j].When().Before(containers[i].When()) +} + +func (history *History) Swap(i, j int) { + containers := *history + tmp := containers[i] + containers[i] = containers[j] + containers[j] = tmp +} + +func (history *History) Add(container *Container) { + *history = append(*history, container) + sort.Sort(history) +} + diff --git a/dockerd/dockerd.go b/dockerd/dockerd.go index f3977e05b6..79b373ad43 100644 --- a/dockerd/dockerd.go +++ b/dockerd/dockerd.go @@ -288,8 +288,7 @@ func (srv *Server) CmdImages(stdin io.ReadCloser, stdout io.Writer, args ...stri if nameFilter != "" && nameFilter != name { continue } - for idx, evt := range *srv.images.ByName[name] { - img := evt.(*image.Image) + for idx, img := range *srv.images.ByName[name] { if *limit > 0 && idx >= *limit { break } diff --git a/image/image.go b/image/image.go index 91b0c6119e..64c19fbce5 100644 --- a/image/image.go +++ b/image/image.go @@ -102,7 +102,7 @@ func (index *Index) Find(idOrName string) *Image { } // Lookup by name if history, exists := index.ByName[idOrName]; exists && history.Len() > 0 { - return (*history)[0].(*Image) + return (*history)[0] } return nil } @@ -116,7 +116,7 @@ func (index *Index) Add(name string, image *Image) error { index.ByName[name] = new(History) } else { // If this image is already the latest version, don't add it. - if (*index.ByName[name])[0].(*Image).Id == image.Id { + if (*index.ByName[name])[0].Id == image.Id { return nil } } @@ -169,8 +169,7 @@ func (index *Index) Rename(oldName, newName string) error { index.ByName[newName] = index.ByName[oldName] delete(index.ByName, oldName) // Change the ID of all images, since they include the name - for _, event := range *index.ByName[newName] { - image := event.(*Image) + for _, image := range *index.ByName[newName] { if id, err := generateImageId(newName, image.Layers); err != nil { return err } else { @@ -228,33 +227,37 @@ func (index *Index) save() error { // History wraps an array of images so they can be sorted by date (most recent first) -type Event interface { - When() time.Time -} - -type History []Event +type History []*Image func (history *History) Len() int { return len(*history) } func (history *History) Less(i, j int) bool { - events := *history - return events[j].When().Before(events[i].When()) + images := *history + return images[j].Created.Before(images[i].Created) } func (history *History) Swap(i, j int) { - events := *history - tmp := events[i] - events[i] = events[j] - events[j] = tmp + images := *history + tmp := images[i] + images[i] = images[j] + images[j] = tmp } -func (history *History) Add(event Event) { - *history = append(*history, event) +func (history *History) Add(image *Image) { + *history = append(*history, image) sort.Sort(history) } +func (history *History) Del(id string) { + for idx, image := range *history { + if image.Id == id { + *history = append((*history)[:idx], (*history)[idx + 1:]...) + } + } +} + type Image struct { Id string // Globally unique identifier Layers []string // Absolute paths @@ -262,10 +265,6 @@ type Image struct { Parent string } -func (image *Image) When() time.Time { - return image.Created -} - func (image *Image) IdParts() (string, string) { if len(image.Id) < 8 { return "", image.Id