Merge pull request #593 from dotcloud/579-move_display_options_to_client-feature

* Api: Move display options to client
This commit is contained in:
Guillaume J. Charmes 2013-05-13 11:40:46 -07:00
Родитель 02d255457a e82ff22fae
Коммит 37b80325d0
6 изменённых файлов: 62 добавлений и 77 удалений

7
api.go
Просмотреть файл

@ -129,9 +129,8 @@ func getImagesJson(srv *Server, w http.ResponseWriter, r *http.Request, vars map
all := r.Form.Get("all") == "1" all := r.Form.Get("all") == "1"
filter := r.Form.Get("filter") filter := r.Form.Get("filter")
only_ids := r.Form.Get("only_ids") == "1"
outs, err := srv.Images(all, only_ids, filter) outs, err := srv.Images(all, filter)
if err != nil { if err != nil {
return err return err
} }
@ -199,8 +198,6 @@ func getContainersPs(srv *Server, w http.ResponseWriter, r *http.Request, vars m
return err return err
} }
all := r.Form.Get("all") == "1" all := r.Form.Get("all") == "1"
trunc_cmd := r.Form.Get("trunc_cmd") != "0"
only_ids := r.Form.Get("only_ids") == "1"
since := r.Form.Get("since") since := r.Form.Get("since")
before := r.Form.Get("before") before := r.Form.Get("before")
n, err := strconv.Atoi(r.Form.Get("limit")) n, err := strconv.Atoi(r.Form.Get("limit"))
@ -208,7 +205,7 @@ func getContainersPs(srv *Server, w http.ResponseWriter, r *http.Request, vars m
n = -1 n = -1
} }
outs := srv.Containers(all, trunc_cmd, only_ids, n, since, before) outs := srv.Containers(all, n, since, before)
b, err := json.Marshal(outs) b, err := json.Marshal(outs)
if err != nil { if err != nil {
return err return err

Просмотреть файл

@ -10,7 +10,7 @@ type ApiImages struct {
Repository string `json:",omitempty"` Repository string `json:",omitempty"`
Tag string `json:",omitempty"` Tag string `json:",omitempty"`
Id string Id string
Created int64 `json:",omitempty"` Created int64
} }
type ApiInfo struct { type ApiInfo struct {
@ -25,11 +25,11 @@ type ApiInfo struct {
type ApiContainers struct { type ApiContainers struct {
Id string Id string
Image string `json:",omitempty"` Image string
Command string `json:",omitempty"` Command string
Created int64 `json:",omitempty"` Created int64
Status string `json:",omitempty"` Status string
Ports string `json:",omitempty"` Ports string
} }
type ApiSearch struct { type ApiSearch struct {

Просмотреть файл

@ -115,8 +115,8 @@ func TestGetImagesJson(t *testing.T) {
srv := &Server{runtime: runtime} srv := &Server{runtime: runtime}
// only_ids=0&all=0 // all=0
req, err := http.NewRequest("GET", "/images/json?only_ids=0&all=0", nil) req, err := http.NewRequest("GET", "/images/json?all=0", nil)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
@ -142,8 +142,8 @@ func TestGetImagesJson(t *testing.T) {
r2 := httptest.NewRecorder() r2 := httptest.NewRecorder()
// only_ids=1&all=1 // all=1
req2, err := http.NewRequest("GET", "/images/json?only_ids=1&all=1", nil) req2, err := http.NewRequest("GET", "/images/json?all=1", nil)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
@ -161,12 +161,8 @@ func TestGetImagesJson(t *testing.T) {
t.Errorf("Excepted 1 image, %d found", len(images2)) t.Errorf("Excepted 1 image, %d found", len(images2))
} }
if images2[0].Repository != "" { if images2[0].Id != GetTestImage(runtime).Id {
t.Errorf("Excepted no image Repository, %s found", images2[0].Repository) t.Errorf("Retrieved image Id differs, expected %s, received %s", GetTestImage(runtime).Id, images2[0].Id)
}
if images2[0].Id != GetTestImage(runtime).ShortId() {
t.Errorf("Retrieved image Id differs, expected %s, received %s", GetTestImage(runtime).ShortId(), images2[0].Id)
} }
r3 := httptest.NewRecorder() r3 := httptest.NewRecorder()
@ -329,8 +325,8 @@ func TestGetContainersPs(t *testing.T) {
if len(containers) != 1 { if len(containers) != 1 {
t.Fatalf("Excepted %d container, %d found", 1, len(containers)) t.Fatalf("Excepted %d container, %d found", 1, len(containers))
} }
if containers[0].Id != container.ShortId() { if containers[0].Id != container.Id {
t.Fatalf("Container ID mismatch. Expected: %s, received: %s\n", container.ShortId(), containers[0].Id) t.Fatalf("Container ID mismatch. Expected: %s, received: %s\n", container.Id, containers[0].Id)
} }
} }

Просмотреть файл

@ -687,6 +687,7 @@ func CmdImages(args ...string) error {
cmd := Subcmd("images", "[OPTIONS] [NAME]", "List images") cmd := Subcmd("images", "[OPTIONS] [NAME]", "List images")
quiet := cmd.Bool("q", false, "only show numeric IDs") quiet := cmd.Bool("q", false, "only show numeric IDs")
all := cmd.Bool("a", false, "show all images") all := cmd.Bool("a", false, "show all images")
noTrunc := cmd.Bool("notrunc", false, "Don't truncate output")
flViz := cmd.Bool("viz", false, "output graph in graphviz format") flViz := cmd.Bool("viz", false, "output graph in graphviz format")
if err := cmd.Parse(args); err != nil { if err := cmd.Parse(args); err != nil {
@ -708,9 +709,6 @@ func CmdImages(args ...string) error {
if cmd.NArg() == 1 { if cmd.NArg() == 1 {
v.Set("filter", cmd.Arg(0)) v.Set("filter", cmd.Arg(0))
} }
if *quiet {
v.Set("only_ids", "1")
}
if *all { if *all {
v.Set("all", "1") v.Set("all", "1")
} }
@ -732,10 +730,27 @@ func CmdImages(args ...string) error {
} }
for _, out := range outs { for _, out := range outs {
if out.Repository == "" {
out.Repository = "<none>"
}
if out.Tag == "" {
out.Tag = "<none>"
}
if !*quiet { if !*quiet {
fmt.Fprintf(w, "%s\t%s\t%s\t%s ago\n", out.Repository, out.Tag, out.Id, HumanDuration(time.Now().Sub(time.Unix(out.Created, 0)))) fmt.Fprintf(w, "%s\t%s\t", out.Repository, out.Tag)
if *noTrunc {
fmt.Fprintf(w, "%s\t", out.Id)
} else {
fmt.Fprintf(w, "%s\t", TruncateId(out.Id))
}
fmt.Fprintf(w, "%s ago\n", HumanDuration(time.Now().Sub(time.Unix(out.Created, 0))))
} else { } else {
fmt.Fprintln(w, out.Id) if *noTrunc {
fmt.Fprintln(w, out.Id)
} else {
fmt.Fprintln(w, TruncateId(out.Id))
}
} }
} }
@ -763,15 +778,9 @@ func CmdPs(args ...string) error {
if *last == -1 && *nLatest { if *last == -1 && *nLatest {
*last = 1 *last = 1
} }
if *quiet {
v.Set("only_ids", "1")
}
if *all { if *all {
v.Set("all", "1") v.Set("all", "1")
} }
if *noTrunc {
v.Set("trunc_cmd", "0")
}
if *last != -1 { if *last != -1 {
v.Set("limit", strconv.Itoa(*last)) v.Set("limit", strconv.Itoa(*last))
} }
@ -799,9 +808,17 @@ func CmdPs(args ...string) error {
for _, out := range outs { for _, out := range outs {
if !*quiet { if !*quiet {
fmt.Fprintf(w, "%s\t%s\t%s\t%s\t%s ago\t%s\n", out.Id, out.Image, out.Command, out.Status, HumanDuration(time.Now().Sub(time.Unix(out.Created, 0))), out.Ports) if *noTrunc {
fmt.Fprintf(w, "%s\t%s\t%s\t%s\t%s ago\t%s\n", out.Id, out.Image, out.Command, out.Status, HumanDuration(time.Now().Sub(time.Unix(out.Created, 0))), out.Ports)
} else {
fmt.Fprintf(w, "%s\t%s\t%s\t%s\t%s ago\t%s\n", TruncateId(out.Id), out.Image, Trunc(out.Command, 20), out.Status, HumanDuration(time.Now().Sub(time.Unix(out.Created, 0))), out.Ports)
}
} else { } else {
fmt.Fprintln(w, out.Id) if *noTrunc {
fmt.Fprintln(w, out.Id)
} else {
fmt.Fprintln(w, TruncateId(out.Id))
}
} }
} }

Просмотреть файл

@ -28,7 +28,7 @@ List containers
.. sourcecode:: http .. sourcecode:: http
GET /containers/ps?trunc_cmd=0&all=1&only_ids=0&before=8dfafdbc3a40 HTTP/1.1 GET /containers/ps?trunc_cmd=0&all=1&before=8dfafdbc3a40 HTTP/1.1
**Example response**: **Example response**:
@ -68,9 +68,7 @@ List containers
} }
] ]
:query only_ids: 1 or 0, Only display numeric IDs. Default 0
:query all: 1 or 0, Show all containers. Only running containers are shown by default :query all: 1 or 0, Show all containers. Only running containers are shown by default
:query trunc_cmd: 1 or 0, Truncate output. Output is truncated by default
:query limit: Show ``limit`` last created containers, include non-running ones. :query limit: Show ``limit`` last created containers, include non-running ones.
:query since: Show only containers created since Id, include non-running ones. :query since: Show only containers created since Id, include non-running ones.
:query before: Show only containers created before Id, include non-running ones. :query before: Show only containers created before Id, include non-running ones.
@ -467,7 +465,7 @@ List Images
.. sourcecode:: http .. sourcecode:: http
GET /images/json?all=0&only_ids=0 HTTP/1.1 GET /images/json?all=0 HTTP/1.1
**Example response**: **Example response**:
@ -523,7 +521,6 @@ List Images
base [style=invisible] base [style=invisible]
} }
:query only_ids: 1 or 0, Only display numeric IDs. Default 0
:query all: 1 or 0, Show all containers. Only running containers are shown by default :query all: 1 or 0, Show all containers. Only running containers are shown by default
:statuscode 200: no error :statuscode 200: no error
:statuscode 500: server error :statuscode 500: server error

Просмотреть файл

@ -135,7 +135,7 @@ func (srv *Server) ImagesViz(out io.Writer) error {
return nil return nil
} }
func (srv *Server) Images(all, only_ids bool, filter string) ([]ApiImages, error) { func (srv *Server) Images(all bool, filter string) ([]ApiImages, error) {
var allImages map[string]*Image var allImages map[string]*Image
var err error var err error
if all { if all {
@ -159,29 +159,19 @@ func (srv *Server) Images(all, only_ids bool, filter string) ([]ApiImages, error
continue continue
} }
delete(allImages, id) delete(allImages, id)
if !only_ids { out.Repository = name
out.Repository = name out.Tag = tag
out.Tag = tag out.Id = image.Id
out.Id = TruncateId(id) out.Created = image.Created.Unix()
out.Created = image.Created.Unix()
} else {
out.Id = image.ShortId()
}
outs = append(outs, out) outs = append(outs, out)
} }
} }
// Display images which aren't part of a // Display images which aren't part of a
if filter == "" { if filter == "" {
for id, image := range allImages { for _, image := range allImages {
var out ApiImages var out ApiImages
if !only_ids { out.Id = image.Id
out.Repository = "<none>" out.Created = image.Created.Unix()
out.Tag = "<none>"
out.Id = TruncateId(id)
out.Created = image.Created.Unix()
} else {
out.Id = image.ShortId()
}
outs = append(outs, out) outs = append(outs, out)
} }
} }
@ -235,7 +225,7 @@ func (srv *Server) ContainerChanges(name string) ([]Change, error) {
return nil, fmt.Errorf("No such container: %s", name) return nil, fmt.Errorf("No such container: %s", name)
} }
func (srv *Server) Containers(all, trunc_cmd, only_ids bool, n int, since, before string) []ApiContainers { func (srv *Server) Containers(all bool, n int, since, before string) []ApiContainers {
var foundBefore bool var foundBefore bool
var displayed int var displayed int
retContainers := []ApiContainers{} retContainers := []ApiContainers{}
@ -264,23 +254,11 @@ func (srv *Server) Containers(all, trunc_cmd, only_ids bool, n int, since, befor
c := ApiContainers{ c := ApiContainers{
Id: container.Id, Id: container.Id,
} }
if trunc_cmd { c.Image = srv.runtime.repositories.ImageName(container.Image)
c = ApiContainers{ c.Command = fmt.Sprintf("%s %s", container.Path, strings.Join(container.Args, " "))
Id: container.ShortId(), c.Created = container.Created.Unix()
} c.Status = container.State.String()
} c.Ports = container.NetworkSettings.PortMappingHuman()
if !only_ids {
command := fmt.Sprintf("%s %s", container.Path, strings.Join(container.Args, " "))
if trunc_cmd {
command = Trunc(command, 20)
}
c.Image = srv.runtime.repositories.ImageName(container.Image)
c.Command = command
c.Created = container.Created.Unix()
c.Status = container.State.String()
c.Ports = container.NetworkSettings.PortMappingHuman()
}
retContainers = append(retContainers, c) retContainers = append(retContainers, c)
} }
return retContainers return retContainers