|
7 | 7 | "fmt" |
8 | 8 | "os" |
9 | 9 | "os/exec" |
10 | | - "strconv" |
11 | 10 | "strings" |
12 | 11 |
|
13 | 12 | "github.com/urfave/cli" |
@@ -42,58 +41,28 @@ var psCommand = cli.Command{ |
42 | 41 | return nil |
43 | 42 | } |
44 | 43 |
|
| 44 | + pidlist := []string{} |
| 45 | + for _, pid := range pids { |
| 46 | + pidlist = append(pidlist, fmt.Sprintf("%d", pid)) |
| 47 | + } |
| 48 | + |
45 | 49 | // [1:] is to remove command name, ex: |
46 | 50 | // context.Args(): [containet_id ps_arg1 ps_arg2 ...] |
47 | 51 | // psArgs: [ps_arg1 ps_arg2 ...] |
48 | 52 | // |
49 | 53 | psArgs := context.Args()[1:] |
50 | 54 | if len(psArgs) == 0 { |
51 | | - psArgs = []string{"-ef"} |
| 55 | + psArgs = []string{"-f"} |
52 | 56 | } |
53 | 57 |
|
| 58 | + psArgs = append(psArgs, "-p", strings.Join(pidlist, ",")) |
54 | 59 | output, err := exec.Command("ps", psArgs...).Output() |
55 | 60 | if err != nil { |
56 | 61 | return err |
57 | 62 | } |
58 | 63 |
|
59 | | - lines := strings.Split(string(output), "\n") |
60 | | - pidIndex, err := getPidIndex(lines[0]) |
61 | | - if err != nil { |
62 | | - return err |
63 | | - } |
64 | | - |
65 | | - fmt.Println(lines[0]) |
66 | | - for _, line := range lines[1:] { |
67 | | - if len(line) == 0 { |
68 | | - continue |
69 | | - } |
70 | | - fields := strings.Fields(line) |
71 | | - p, err := strconv.Atoi(fields[pidIndex]) |
72 | | - if err != nil { |
73 | | - return fmt.Errorf("unexpected pid '%s': %s", fields[pidIndex], err) |
74 | | - } |
75 | | - |
76 | | - for _, pid := range pids { |
77 | | - if pid == p { |
78 | | - fmt.Println(line) |
79 | | - break |
80 | | - } |
81 | | - } |
82 | | - } |
| 64 | + fmt.Printf(string(output)) |
83 | 65 | return nil |
84 | 66 | }, |
85 | 67 | SkipArgReorder: true, |
86 | 68 | } |
87 | | - |
88 | | -func getPidIndex(title string) (int, error) { |
89 | | - titles := strings.Fields(title) |
90 | | - |
91 | | - pidIndex := -1 |
92 | | - for i, name := range titles { |
93 | | - if name == "PID" { |
94 | | - return i, nil |
95 | | - } |
96 | | - } |
97 | | - |
98 | | - return pidIndex, fmt.Errorf("couldn't find PID field in ps output") |
99 | | -} |
0 commit comments