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:
parent
e559f7aebb
commit
bb8591138b
5
list.go
5
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")
|
||||
}
|
||||
|
|
25
ps.go
25
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: `<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"
|
||||
|
|
Loading…
Reference in New Issue