feat: print ios apps

This commit is contained in:
debugtalk
2022-10-01 17:21:36 +08:00
parent 51ed654ee2
commit ac1afdc87a
3 changed files with 91 additions and 41 deletions

53
hrp/cmd/ios/apps.go Normal file
View File

@@ -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)
}

View File

@@ -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,

View File

@@ -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
}