Bläddra i källkod

Merge pull request #918 from dotcloud/hisotry_lookup

Add image lookup to history command
Guillaume J. Charmes 12 år sedan
förälder
incheckning
32e8f9beca
4 ändrade filer med 19 tillägg och 2 borttagningar
  1. 2 1
      api_params.go
  2. 4 1
      commands.go
  3. 1 0
      docs/sources/api/docker_remote_api_v1.2.rst
  4. 12 0
      server.go

+ 2 - 1
api_params.go

@@ -1,7 +1,8 @@
 package docker
 
 type APIHistory struct {
-	ID        string `json:"Id"`
+	ID        string   `json:"Id"`
+	Tags      []string `json:",omitempty"`
 	Created   int64
 	CreatedBy string `json:",omitempty"`
 }

+ 4 - 1
commands.go

@@ -627,7 +627,10 @@ func (cli *DockerCli) CmdHistory(args ...string) error {
 	fmt.Fprintln(w, "ID\tCREATED\tCREATED BY")
 
 	for _, out := range outs {
-		fmt.Fprintf(w, "%s\t%s ago\t%s\n", out.ID, utils.HumanDuration(time.Now().Sub(time.Unix(out.Created, 0))), out.CreatedBy)
+		if out.Tags != nil {
+			out.ID = out.Tags[0]
+		}
+		fmt.Fprintf(w, "%s \t%s ago\t%s\n", out.ID, utils.HumanDuration(time.Now().Sub(time.Unix(out.Created, 0))), out.CreatedBy)
 	}
 	w.Flush()
 	return nil

+ 1 - 0
docs/sources/api/docker_remote_api_v1.2.rst

@@ -691,6 +691,7 @@ Get the history of an image
 	   [
 		{
 			"Id":"b750fe79269d",
+			"Tag":["base:latest"],
 			"Created":1364102658,
 			"CreatedBy":"/bin/bash"
 		},

+ 12 - 0
server.go

@@ -218,12 +218,24 @@ func (srv *Server) ImageHistory(name string) ([]APIHistory, error) {
 		return nil, err
 	}
 
+	lookupMap := make(map[string][]string)
+	for name, repository := range srv.runtime.repositories.Repositories {
+		for tag, id := range repository {
+			// If the ID already has a reverse lookup, do not update it unless for "latest"
+			if _, exists := lookupMap[id]; !exists {
+				lookupMap[id] = []string{}
+			}
+			lookupMap[id] = append(lookupMap[id], name+":"+tag)
+		}
+	}
+
 	outs := []APIHistory{} //produce [] when empty instead of 'null'
 	err = image.WalkHistory(func(img *Image) error {
 		var out APIHistory
 		out.ID = srv.runtime.repositories.ImageName(img.ShortID())
 		out.Created = img.Created.Unix()
 		out.CreatedBy = strings.Join(img.ContainerConfig.Cmd, " ")
+		out.Tags = lookupMap[img.ID]
 		outs = append(outs, out)
 		return nil
 	})