diff --git a/list.go b/list.go index 5fe414b8..627a5732 100644 --- a/list.go +++ b/list.go @@ -81,12 +81,9 @@ in json format: fatal(err) } case "json": - data, err := json.Marshal(s) - if err != nil { + if err := json.NewEncoder(os.Stdout).Encode(s); err != nil { fatal(err) } - os.Stdout.Write(data) - default: fatalf("invalid format option") } diff --git a/ps.go b/ps.go index 5a0dae61..fad16ef8 100644 --- a/ps.go +++ b/ps.go @@ -3,7 +3,9 @@ package main import ( + "encoding/json" "fmt" + "os" "os/exec" "strconv" "strings" @@ -15,12 +17,35 @@ var psCommand = cli.Command{ Name: "ps", Usage: "ps displays the processes running inside a container", ArgsUsage: ` `, + Flags: []cli.Flag{ + cli.StringFlag{ + Name: "format, f", + Value: "", + Usage: `select one of: ` + formatOptions + `. + +The default format is table. The following will output the processes of a container +in json format: + + # runc ps -f json`, + }, + }, Action: func(context *cli.Context) { container, err := getContainer(context) if err != nil { fatal(err) } + if context.String("format") == "json" { + pids, err := container.Processes() + if err != nil { + fatal(err) + } + if err := json.NewEncoder(os.Stdout).Encode(pids); err != nil { + fatal(err) + } + return + } + psArgs := context.Args().Get(1) if psArgs == "" { psArgs = "-ef"