mirror of
https://github.com/httprunner/httprunner.git
synced 2026-07-12 16:01:27 +08:00
feat: print android devices
This commit is contained in:
62
hrp/cmd/adb/devices.go
Normal file
62
hrp/cmd/adb/devices.go
Normal 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
13
hrp/cmd/adb/init.go
Normal 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)
|
||||||
|
}
|
||||||
@@ -96,7 +96,7 @@ var listIOSDevicesCmd = &cobra.Command{
|
|||||||
deviceList = append(deviceList, d)
|
deviceList = append(deviceList, d)
|
||||||
}
|
}
|
||||||
if udid != "" && len(deviceList) == 0 {
|
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)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -9,6 +9,7 @@ import (
|
|||||||
"github.com/rs/zerolog/log"
|
"github.com/rs/zerolog/log"
|
||||||
"github.com/spf13/cobra"
|
"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/cmd/ios"
|
||||||
"github.com/httprunner/httprunner/v4/hrp/internal/version"
|
"github.com/httprunner/httprunner/v4/hrp/internal/version"
|
||||||
)
|
)
|
||||||
@@ -61,6 +62,8 @@ func Execute() {
|
|||||||
rootCmd.PersistentFlags().StringVar(&venv, "venv", "", "specify python3 venv path")
|
rootCmd.PersistentFlags().StringVar(&venv, "venv", "", "specify python3 venv path")
|
||||||
|
|
||||||
ios.Init(rootCmd)
|
ios.Init(rootCmd)
|
||||||
|
adb.Init(rootCmd)
|
||||||
|
|
||||||
if err := rootCmd.Execute(); err != nil {
|
if err := rootCmd.Execute(); err != nil {
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user