feat: print android devices

This commit is contained in:
debugtalk
2022-10-01 15:20:41 +08:00
parent bfc7a49669
commit 8c0d495c52
4 changed files with 79 additions and 1 deletions

62
hrp/cmd/adb/devices.go Normal file
View File

@@ -0,0 +1,62 @@
package adb
import (
"encoding/json"
"fmt"
"os"
"github.com/electricbubble/gadb"
"github.com/pkg/errors"
"github.com/spf13/cobra"
"github.com/httprunner/httprunner/v4/hrp/internal/uixt"
)
func format(data map[string]string) string {
result, _ := json.MarshalIndent(data, "", "\t")
return string(result)
}
var listAndroidDevicesCmd = &cobra.Command{
Use: "devices",
Short: "List all Android devices",
RunE: func(cmd *cobra.Command, args []string) error {
devices, err := uixt.DeviceList()
if err != nil {
return errors.Wrap(err, "list android devices failed")
}
var deviceList []gadb.Device
// filter by serial
for _, d := range devices {
if serial != "" && serial != d.Serial() {
continue
}
deviceList = append(deviceList, d)
}
if serial != "" && len(deviceList) == 0 {
fmt.Printf("no android device found for serial: %s\n", serial)
os.Exit(1)
}
for _, d := range deviceList {
if isDetail {
fmt.Println(format(d.DeviceInfo()))
} else {
fmt.Println(d.Serial(), d.Usb())
}
}
return nil
},
}
var (
serial string
isDetail bool
)
func init() {
listAndroidDevicesCmd.Flags().StringVarP(&serial, "serial", "s", "", "filter by device's serial")
listAndroidDevicesCmd.Flags().BoolVarP(&isDetail, "detail", "d", false, "print device's detail")
androidRootCmd.AddCommand(listAndroidDevicesCmd)
}

13
hrp/cmd/adb/init.go Normal file
View File

@@ -0,0 +1,13 @@
package adb
import "github.com/spf13/cobra"
var androidRootCmd = &cobra.Command{
Use: "adb",
Short: "simple utils for android device management",
PersistentPreRun: func(cmd *cobra.Command, args []string) {},
}
func Init(rootCmd *cobra.Command) {
rootCmd.AddCommand(androidRootCmd)
}

View File

@@ -96,7 +96,7 @@ var listIOSDevicesCmd = &cobra.Command{
deviceList = append(deviceList, d)
}
if udid != "" && len(deviceList) == 0 {
fmt.Printf("no device found for udid: %s\n", udid)
fmt.Printf("no ios device found for udid: %s\n", udid)
os.Exit(1)
}

View File

@@ -9,6 +9,7 @@ import (
"github.com/rs/zerolog/log"
"github.com/spf13/cobra"
"github.com/httprunner/httprunner/v4/hrp/cmd/adb"
"github.com/httprunner/httprunner/v4/hrp/cmd/ios"
"github.com/httprunner/httprunner/v4/hrp/internal/version"
)
@@ -61,6 +62,8 @@ func Execute() {
rootCmd.PersistentFlags().StringVar(&venv, "venv", "", "specify python3 venv path")
ios.Init(rootCmd)
adb.Init(rootCmd)
if err := rootCmd.Execute(); err != nil {
os.Exit(1)
}