github.com/containers/podman/v2@v2.2.2-0.20210501105131-c1e07d070c4c/pkg/api/handlers/compat/containers_top.go (about) 1 package compat 2 3 import ( 4 "net/http" 5 "strings" 6 7 "github.com/containers/podman/v2/libpod" 8 "github.com/containers/podman/v2/pkg/api/handlers" 9 "github.com/containers/podman/v2/pkg/api/handlers/utils" 10 "github.com/gorilla/schema" 11 "github.com/pkg/errors" 12 ) 13 14 func TopContainer(w http.ResponseWriter, r *http.Request) { 15 runtime := r.Context().Value("runtime").(*libpod.Runtime) 16 decoder := r.Context().Value("decoder").(*schema.Decoder) 17 18 defaultValue := "-ef" 19 if utils.IsLibpodRequest(r) { 20 defaultValue = "" 21 } 22 query := struct { 23 PsArgs string `schema:"ps_args"` 24 }{ 25 PsArgs: defaultValue, 26 } 27 if err := decoder.Decode(&query, r.URL.Query()); err != nil { 28 utils.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest, 29 errors.Wrapf(err, "failed to parse parameters for %s", r.URL.String())) 30 return 31 } 32 33 name := utils.GetName(r) 34 c, err := runtime.LookupContainer(name) 35 if err != nil { 36 utils.ContainerNotFound(w, name, err) 37 return 38 } 39 40 output, err := c.Top([]string{query.PsArgs}) 41 if err != nil { 42 utils.InternalServerError(w, err) 43 return 44 } 45 46 var body = handlers.ContainerTopOKBody{} 47 if len(output) > 0 { 48 body.Titles = strings.Split(output[0], "\t") 49 for _, line := range output[1:] { 50 body.Processes = append(body.Processes, strings.Split(line, "\t")) 51 } 52 } 53 utils.WriteJSON(w, http.StatusOK, body) 54 }