Merge pull request #779 from crosbymichael/ps-json

Add json format to ps command
This commit is contained in:
Qiang Huang 2016-04-26 09:34:27 +08:00
commit 6d1c115b10
2 changed files with 26 additions and 4 deletions

View File

@ -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")
}

25
ps.go
View File

@ -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: `<container-id> <ps options>`,
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"