From 0a051a1f3435b397f06d31b8d259280e0df9f282 Mon Sep 17 00:00:00 2001 From: debugtalk Date: Sat, 1 Oct 2022 23:46:24 +0800 Subject: [PATCH] feat: get ios running processes by ps --- hrp/cmd/ios/apps.go | 21 ++++----------- hrp/cmd/ios/devices.go | 8 +++--- hrp/cmd/ios/init.go | 25 +++++++++++++++++- hrp/cmd/ios/ps.go | 60 ++++++++++++++++++++++++++++++++++++++++++ hrp/cmd/ios/xctest.go | 13 +-------- 5 files changed, 94 insertions(+), 33 deletions(-) create mode 100644 hrp/cmd/ios/ps.go diff --git a/hrp/cmd/ios/apps.go b/hrp/cmd/ios/apps.go index ed8d6063..5021a422 100644 --- a/hrp/cmd/ios/apps.go +++ b/hrp/cmd/ios/apps.go @@ -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) } diff --git a/hrp/cmd/ios/devices.go b/hrp/cmd/ios/devices.go index f10736e4..ec884e19 100644 --- a/hrp/cmd/ios/devices.go +++ b/hrp/cmd/ios/devices.go @@ -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) } diff --git a/hrp/cmd/ios/init.go b/hrp/cmd/ios/init.go index e66898a9..8cfea1f3 100644 --- a/hrp/cmd/ios/init.go +++ b/hrp/cmd/ios/init.go @@ -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) } diff --git a/hrp/cmd/ios/ps.go b/hrp/cmd/ios/ps.go new file mode 100644 index 00000000..22f5a94b --- /dev/null +++ b/hrp/cmd/ios/ps.go @@ -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) +} diff --git a/hrp/cmd/ios/xctest.go b/hrp/cmd/ios/xctest.go index 9300c6e1..e2b8d343 100644 --- a/hrp/cmd/ios/xctest.go +++ b/hrp/cmd/ios/xctest.go @@ -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)