...
1
2
3 package main
4
5 import (
6 gcontext "context"
7 "encoding/json"
8 "fmt"
9 "os"
10
11 "github.com/Microsoft/hcsshim/internal/appargs"
12 "github.com/Microsoft/hcsshim/internal/hcs/schema1"
13 "github.com/urfave/cli"
14 )
15
16 var psCommand = cli.Command{
17 Name: "ps",
18 Usage: "ps displays the processes running inside a container",
19 ArgsUsage: `<container-id> [ps options]`,
20 Flags: []cli.Flag{
21 cli.StringFlag{
22 Name: "format, f",
23 Value: "json",
24 Usage: `select one of: ` + formatOptions,
25 },
26 },
27 Before: appargs.Validate(argID),
28 Action: func(context *cli.Context) error {
29 id := context.Args().First()
30 container, err := getContainer(id, true)
31 if err != nil {
32 return err
33 }
34 defer container.Close()
35
36 props, err := container.hc.Properties(gcontext.Background(), schema1.PropertyTypeProcessList)
37 if err != nil {
38 return err
39 }
40
41 var pids []int
42 for _, p := range props.ProcessList {
43 pids = append(pids, int(p.ProcessId))
44 }
45
46 switch context.String("format") {
47 case "json":
48 return json.NewEncoder(os.Stdout).Encode(pids)
49 default:
50 return fmt.Errorf("invalid format option")
51 }
52 },
53 SkipArgReorder: true,
54 }
55
View as plain text