From ac1afdc87a16f2d58d2897633cb5157c2c86715d Mon Sep 17 00:00:00 2001 From: debugtalk Date: Sat, 1 Oct 2022 17:21:36 +0800 Subject: [PATCH] feat: print ios apps --- hrp/cmd/ios/apps.go | 53 +++++++++++++++++++++++++++++++++ hrp/cmd/ios/devices.go | 41 ++++++++----------------- hrp/internal/uixt/ios_device.go | 38 +++++++++++++++-------- 3 files changed, 91 insertions(+), 41 deletions(-) create mode 100644 hrp/cmd/ios/apps.go diff --git a/hrp/cmd/ios/apps.go b/hrp/cmd/ios/apps.go new file mode 100644 index 00000000..ed8d6063 --- /dev/null +++ b/hrp/cmd/ios/apps.go @@ -0,0 +1,53 @@ +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{ + Use: "apps", + Short: "List all iOS installed apps", + RunE: func(cmd *cobra.Command, args []string) error { + devices, err := uixt.IOSDevices(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 { + return errors.Wrap(err, "get ios apps failed") + } + for _, app := range apps { + if appType != "all" && strings.ToLower(app.Type) != appType { + continue + } + + fmt.Printf("%-10.10s %-30.30s %-50.50s %-s\n", + app.Type, app.DisplayName, app.CFBundleIdentifier, app.Version) + } + return nil + }, +} + +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) +} diff --git a/hrp/cmd/ios/devices.go b/hrp/cmd/ios/devices.go index 87ae3b84..f10736e4 100644 --- a/hrp/cmd/ios/devices.go +++ b/hrp/cmd/ios/devices.go @@ -4,22 +4,14 @@ import ( "encoding/json" "fmt" "os" - "strings" giDevice "github.com/electricbubble/gidevice" "github.com/pkg/errors" "github.com/spf13/cobra" + + "github.com/httprunner/httprunner/v4/hrp/internal/uixt" ) -func wrapError(err error, msg string) error { - if strings.HasSuffix(err.Error(), "InvalidService") { - msg += ", check if Developer Disk Image mounted" - } - return errors.Wrap(err, msg) -} - -type DeviceList []Device - type Device struct { d giDevice.Device UDID string `json:"UDID"` @@ -77,30 +69,21 @@ var listIOSDevicesCmd = &cobra.Command{ Use: "devices", Short: "List all iOS devices", RunE: func(cmd *cobra.Command, args []string) error { - usbmux, err := giDevice.NewUsbmux() + devices, err := uixt.IOSDevices(udid) if err != nil { - return wrapError(err, "create usbmux failed") + return err } - - devices, err := usbmux.Devices() - if err != nil { - return wrapError(err, "list ios devices failed") - } - - var deviceList []giDevice.Device - // filter by udid - for _, d := range devices { - if udid != "" && udid != d.Properties().SerialNumber { - continue + if len(devices) == 0 { + if udid != "" { + fmt.Printf("no ios device found for udid: %s\n", udid) + os.Exit(1) + } else { + fmt.Println("no ios device found") + os.Exit(0) } - deviceList = append(deviceList, d) - } - if udid != "" && len(deviceList) == 0 { - fmt.Printf("no ios device found for udid: %s\n", udid) - os.Exit(1) } - for _, d := range deviceList { + for _, d := range devices { deviceByte, _ := json.Marshal(d.Properties()) device := &Device{ d: d, diff --git a/hrp/internal/uixt/ios_device.go b/hrp/internal/uixt/ios_device.go index 80b19a07..95228062 100644 --- a/hrp/internal/uixt/ios_device.go +++ b/hrp/internal/uixt/ios_device.go @@ -122,17 +122,31 @@ func WithLogOn(logOn bool) IOSDeviceOption { } } -func NewIOSDevice(options ...IOSDeviceOption) (device *IOSDevice, err error) { +func IOSDevices(udid ...string) (devices []giDevice.Device, err error) { var usbmux giDevice.Usbmux if usbmux, err = giDevice.NewUsbmux(); err != nil { return nil, fmt.Errorf("init usbmux failed: %v", err) } - var deviceList []giDevice.Device - if deviceList, err = usbmux.Devices(); err != nil { - return nil, fmt.Errorf("get attached devices failed: %v", err) + if devices, err = usbmux.Devices(); err != nil { + return nil, fmt.Errorf("list ios devices failed: %v", err) } + // filter by udid + var deviceList []giDevice.Device + for _, d := range devices { + for _, u := range udid { + if u != "" && u != d.Properties().SerialNumber { + continue + } + deviceList = append(deviceList, d) + } + } + + return deviceList, nil +} + +func NewIOSDevice(options ...IOSDeviceOption) (device *IOSDevice, err error) { device = &IOSDevice{ Port: defaultWDAPort, MjpegPort: defaultMjpegPort, @@ -141,15 +155,15 @@ func NewIOSDevice(options ...IOSDeviceOption) (device *IOSDevice, err error) { option(device) } - serialNumber := device.UDID - for _, dev := range deviceList { - // find device by serial number if specified - if serialNumber != "" && dev.Properties().SerialNumber != serialNumber { - continue - } + deviceList, err := IOSDevices(device.UDID) + if err != nil { + return nil, err + } - device.UDID = dev.Properties().SerialNumber - device.d = dev + if len(deviceList) > 0 { + device.UDID = deviceList[0].Properties().SerialNumber + log.Info().Str("udid", device.UDID).Msg("select device") + device.d = deviceList[0] return device, nil }