mirror of
https://github.com/httprunner/httprunner.git
synced 2026-07-10 23:12:41 +08:00
feat: print ios apps
This commit is contained in:
53
hrp/cmd/ios/apps.go
Normal file
53
hrp/cmd/ios/apps.go
Normal 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)
|
||||||
|
}
|
||||||
@@ -4,22 +4,14 @@ import (
|
|||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
"strings"
|
|
||||||
|
|
||||||
giDevice "github.com/electricbubble/gidevice"
|
giDevice "github.com/electricbubble/gidevice"
|
||||||
"github.com/pkg/errors"
|
"github.com/pkg/errors"
|
||||||
"github.com/spf13/cobra"
|
"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 {
|
type Device struct {
|
||||||
d giDevice.Device
|
d giDevice.Device
|
||||||
UDID string `json:"UDID"`
|
UDID string `json:"UDID"`
|
||||||
@@ -77,30 +69,21 @@ var listIOSDevicesCmd = &cobra.Command{
|
|||||||
Use: "devices",
|
Use: "devices",
|
||||||
Short: "List all iOS devices",
|
Short: "List all iOS devices",
|
||||||
RunE: func(cmd *cobra.Command, args []string) error {
|
RunE: func(cmd *cobra.Command, args []string) error {
|
||||||
usbmux, err := giDevice.NewUsbmux()
|
devices, err := uixt.IOSDevices(udid)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return wrapError(err, "create usbmux failed")
|
return err
|
||||||
}
|
}
|
||||||
|
if len(devices) == 0 {
|
||||||
devices, err := usbmux.Devices()
|
if udid != "" {
|
||||||
if err != nil {
|
fmt.Printf("no ios device found for udid: %s\n", udid)
|
||||||
return wrapError(err, "list ios devices failed")
|
os.Exit(1)
|
||||||
}
|
} else {
|
||||||
|
fmt.Println("no ios device found")
|
||||||
var deviceList []giDevice.Device
|
os.Exit(0)
|
||||||
// filter by udid
|
|
||||||
for _, d := range devices {
|
|
||||||
if udid != "" && udid != d.Properties().SerialNumber {
|
|
||||||
continue
|
|
||||||
}
|
}
|
||||||
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())
|
deviceByte, _ := json.Marshal(d.Properties())
|
||||||
device := &Device{
|
device := &Device{
|
||||||
d: d,
|
d: d,
|
||||||
|
|||||||
@@ -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
|
var usbmux giDevice.Usbmux
|
||||||
if usbmux, err = giDevice.NewUsbmux(); err != nil {
|
if usbmux, err = giDevice.NewUsbmux(); err != nil {
|
||||||
return nil, fmt.Errorf("init usbmux failed: %v", err)
|
return nil, fmt.Errorf("init usbmux failed: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
var deviceList []giDevice.Device
|
if devices, err = usbmux.Devices(); err != nil {
|
||||||
if deviceList, err = usbmux.Devices(); err != nil {
|
return nil, fmt.Errorf("list ios devices failed: %v", err)
|
||||||
return nil, fmt.Errorf("get attached 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{
|
device = &IOSDevice{
|
||||||
Port: defaultWDAPort,
|
Port: defaultWDAPort,
|
||||||
MjpegPort: defaultMjpegPort,
|
MjpegPort: defaultMjpegPort,
|
||||||
@@ -141,15 +155,15 @@ func NewIOSDevice(options ...IOSDeviceOption) (device *IOSDevice, err error) {
|
|||||||
option(device)
|
option(device)
|
||||||
}
|
}
|
||||||
|
|
||||||
serialNumber := device.UDID
|
deviceList, err := IOSDevices(device.UDID)
|
||||||
for _, dev := range deviceList {
|
if err != nil {
|
||||||
// find device by serial number if specified
|
return nil, err
|
||||||
if serialNumber != "" && dev.Properties().SerialNumber != serialNumber {
|
}
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
device.UDID = dev.Properties().SerialNumber
|
if len(deviceList) > 0 {
|
||||||
device.d = dev
|
device.UDID = deviceList[0].Properties().SerialNumber
|
||||||
|
log.Info().Str("udid", device.UDID).Msg("select device")
|
||||||
|
device.d = deviceList[0]
|
||||||
return device, nil
|
return device, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user