-
Notifications
You must be signed in to change notification settings - Fork 32
/
ps.go
61 lines (52 loc) · 1.38 KB
/
ps.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package ios
import (
"fmt"
"time"
"github.com/pkg/errors"
"github.com/spf13/cobra"
)
var psCmd = &cobra.Command{
Use: "ps",
Short: "show running processes",
PersistentPreRun: func(cmd *cobra.Command, args []string) {},
RunE: func(cmd *cobra.Command, args []string) error {
device, err := getDevice(udid)
if err != nil {
return err
}
apps, err := device.AppList()
if err != nil {
return errors.Wrap(err, "get ios apps failed")
}
maxNameLen := 0
mapper := make(map[string]interface{})
for _, app := range apps {
mapper[app.ExecutableName] = app.CFBundleIdentifier
if len(app.ExecutableName) > maxNameLen {
maxNameLen = len(app.ExecutableName)
}
}
runningProcesses, err := device.AppRunningProcesses()
if err != nil {
return errors.Wrap(err, "get running processes failed")
}
for _, p := range runningProcesses {
if !isAll && !p.IsApplication {
continue
}
bundleID, ok := mapper[p.Name]
if !ok {
bundleID = ""
}
fmt.Printf("%4d %-"+fmt.Sprintf("%d", maxNameLen)+"s %20s %s\n",
p.Pid, p.Name, time.Since(p.StartDate).String(), bundleID)
}
return nil
},
}
var isAll bool
func init() {
psCmd.Flags().StringVarP(&udid, "udid", "u", "", "specify device by udid")
psCmd.Flags().BoolVarP(&isAll, "all", "a", false, "print all processes including system processes")
iosRootCmd.AddCommand(psCmd)
}