From 51ed654ee202b4a6fec997aec3d3cf09e3b77d10 Mon Sep 17 00:00:00 2001 From: debugtalk Date: Sat, 1 Oct 2022 15:20:41 +0800 Subject: [PATCH] feat: print android devices --- hrp/cmd/adb/devices.go | 62 ++++++++++++++++++++++++++++++++++++++++++ hrp/cmd/adb/init.go | 13 +++++++++ hrp/cmd/ios/devices.go | 2 +- hrp/cmd/root.go | 3 ++ 4 files changed, 79 insertions(+), 1 deletion(-) create mode 100644 hrp/cmd/adb/devices.go create mode 100644 hrp/cmd/adb/init.go diff --git a/hrp/cmd/adb/devices.go b/hrp/cmd/adb/devices.go new file mode 100644 index 00000000..789bc1e4 --- /dev/null +++ b/hrp/cmd/adb/devices.go @@ -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) +} diff --git a/hrp/cmd/adb/init.go b/hrp/cmd/adb/init.go new file mode 100644 index 00000000..9025ef70 --- /dev/null +++ b/hrp/cmd/adb/init.go @@ -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) +} diff --git a/hrp/cmd/ios/devices.go b/hrp/cmd/ios/devices.go index 8286d6ef..87ae3b84 100644 --- a/hrp/cmd/ios/devices.go +++ b/hrp/cmd/ios/devices.go @@ -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) } diff --git a/hrp/cmd/root.go b/hrp/cmd/root.go index 17ca8ba2..f7572d10 100644 --- a/hrp/cmd/root.go +++ b/hrp/cmd/root.go @@ -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) }