feat: get ios running processes by ps

This commit is contained in:
debugtalk
2022-10-01 23:46:24 +08:00
parent fdc9f382d4
commit 0a051a1f34
5 changed files with 94 additions and 33 deletions

View File

@@ -2,31 +2,20 @@ package ios
import (
"fmt"
"os"
"strings"
"github.com/pkg/errors"
"github.com/spf13/cobra"
"github.com/httprunner/httprunner/v4/hrp/internal/uixt"
)
var listIOSAppsCmd = &cobra.Command{
var listAppsCmd = &cobra.Command{
Use: "apps",
Short: "List all iOS installed apps",
RunE: func(cmd *cobra.Command, args []string) error {
devices, err := uixt.IOSDevices(udid)
device, err := getDevice(udid)
if err != nil {
return err
}
if len(devices) == 0 {
fmt.Println("no ios device found")
os.Exit(1)
}
if len(devices) > 1 {
return fmt.Errorf("multiple devices found, please specify udid")
}
device := devices[0]
apps, err := device.AppList()
if err != nil {
@@ -47,7 +36,7 @@ var listIOSAppsCmd = &cobra.Command{
var appType string
func init() {
listIOSAppsCmd.Flags().StringVarP(&udid, "udid", "u", "", "filter by device's udid")
listIOSAppsCmd.Flags().StringVarP(&appType, "type", "t", "user", "filter application type [user|system|pluginkit|all]")
iosRootCmd.AddCommand(listIOSAppsCmd)
listAppsCmd.Flags().StringVarP(&udid, "udid", "u", "", "filter by device's udid")
listAppsCmd.Flags().StringVarP(&appType, "type", "t", "user", "filter application type [user|system|pluginkit|all]")
iosRootCmd.AddCommand(listAppsCmd)
}

View File

@@ -65,7 +65,7 @@ func (device *Device) ToFormat() string {
return string(result)
}
var listIOSDevicesCmd = &cobra.Command{
var listDevicesCmd = &cobra.Command{
Use: "devices",
Short: "List all iOS devices",
RunE: func(cmd *cobra.Command, args []string) error {
@@ -111,7 +111,7 @@ var (
)
func init() {
listIOSDevicesCmd.Flags().StringVarP(&udid, "udid", "u", "", "filter by device's udid")
listIOSDevicesCmd.Flags().BoolVarP(&isDetail, "detail", "d", false, "print device's detail")
iosRootCmd.AddCommand(listIOSDevicesCmd)
listDevicesCmd.Flags().StringVarP(&udid, "udid", "u", "", "filter by device's udid")
listDevicesCmd.Flags().BoolVarP(&isDetail, "detail", "d", false, "print device's detail")
iosRootCmd.AddCommand(listDevicesCmd)
}

View File

@@ -1,12 +1,35 @@
package ios
import "github.com/spf13/cobra"
import (
"fmt"
"os"
giDevice "github.com/electricbubble/gidevice"
"github.com/spf13/cobra"
"github.com/httprunner/httprunner/v4/hrp/internal/uixt"
)
var iosRootCmd = &cobra.Command{
Use: "ios",
Short: "simple utils for ios device management",
}
func getDevice(udid string) (giDevice.Device, error) {
devices, err := uixt.IOSDevices(udid)
if err != nil {
return nil, err
}
if len(devices) == 0 {
fmt.Println("no ios device found")
os.Exit(1)
}
if len(devices) > 1 {
return nil, fmt.Errorf("multiple devices found, please specify udid")
}
return devices[0], nil
}
func Init(rootCmd *cobra.Command) {
rootCmd.AddCommand(iosRootCmd)
}

60
hrp/cmd/ios/ps.go Normal file
View File

@@ -0,0 +1,60 @@
package ios
import (
"fmt"
"time"
"github.com/pkg/errors"
"github.com/spf13/cobra"
)
var psCmd = &cobra.Command{
Use: "ps",
Short: "show running processes",
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", "", "filter by device's udid")
psCmd.Flags().BoolVarP(&isAll, "all", "a", false, "print all processes including system processes")
iosRootCmd.AddCommand(psCmd)
}

View File

@@ -9,8 +9,6 @@ import (
"github.com/pkg/errors"
"github.com/rs/zerolog/log"
"github.com/spf13/cobra"
"github.com/httprunner/httprunner/v4/hrp/internal/uixt"
)
var xctestCmd = &cobra.Command{
@@ -20,19 +18,10 @@ var xctestCmd = &cobra.Command{
if bundleID == "" {
return fmt.Errorf("bundleID is required")
}
devices, err := uixt.IOSDevices(udid)
device, err := getDevice(udid)
if err != nil {
return err
}
if len(devices) == 0 {
fmt.Println("no ios device found")
os.Exit(1)
}
if len(devices) > 1 {
return fmt.Errorf("multiple devices found, please specify udid")
}
device := devices[0]
log.Info().Str("bundleID", bundleID).Msg("run xctest")
out, cancel, err := device.XCTest(bundleID)