Add json format to ps command

For programatic parsing add a json format option to the new `runc ps`
command.

Signed-off-by: Michael Crosby <crosbymichael@gmail.com>
This commit is contained in:
Michael Crosby 2016-04-25 15:21:07 -07:00
parent e559f7aebb
commit bb8591138b
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"