Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion commands/info.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,15 @@ package commands

import (
"fmt"

"github.com/mobile-next/mobilecli/devices"
)

// DeviceInfoResponse represents the response for a device info command
type DeviceInfoResponse struct {
Device *devices.FullDeviceInfo `json:"device"`
}

func InfoCommand(deviceID string) *CommandResponse {
targetDevice, err := FindDeviceOrAutoSelect(deviceID)
if err != nil {
Expand All @@ -15,5 +22,9 @@ func InfoCommand(deviceID string) *CommandResponse {
return NewErrorResponse(fmt.Errorf("error getting device info: %v", err))
}

return NewSuccessResponse(info)
response := DeviceInfoResponse{
Device: info,
}

return NewSuccessResponse(response)
}
2 changes: 1 addition & 1 deletion devices/wda/requests.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ func CreateSession() (string, error) {
return "", fmt.Errorf("failed to create session: %v", err)
}

log.Printf("createSession response: %v", response)
// log.Printf("createSession response: %v", response)

sessionId := response["sessionId"].(string)
return sessionId, nil
Expand Down
4 changes: 2 additions & 2 deletions devices/wda/windowsize.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package wda

import (
"fmt"
"log"
)

type Size struct {
Expand Down Expand Up @@ -31,7 +30,8 @@ func GetWindowSize() (*WindowSize, error) {
if err != nil {
return nil, err
}
log.Printf("response: %v", response["value"])

// log.Printf("response: %v", response["value"])

windowSize := response["value"].(map[string]interface{})
screenSize := windowSize["screenSize"].(map[string]interface{})
Expand Down
113 changes: 79 additions & 34 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"strings"

"github.com/mobile-next/mobilecli/commands"
"github.com/mobile-next/mobilecli/devices"
"github.com/mobile-next/mobilecli/server"
"github.com/mobile-next/mobilecli/utils"
"github.com/spf13/cobra"
Expand All @@ -35,6 +34,9 @@ var (
screenshotFormat string
screenshotJpegQuality int

// for screencapture command
screencaptureFormat string

// for devices command
platform string
deviceType string
Expand All @@ -48,7 +50,9 @@ var rootCmd = &cobra.Command{
CompletionOptions: cobra.CompletionOptions{
HiddenDefaultCmd: true,
},
Version: version,
Version: version,
SilenceUsage: true,
SilenceErrors: true,
}

var devicesCmd = &cobra.Command{
Expand Down Expand Up @@ -104,7 +108,61 @@ var screenshotCmd = &cobra.Command{
},
}

var rebootCmd = &cobra.Command{
var screencaptureCmd = &cobra.Command{
Use: "screencapture",
Short: "Stream screen capture from a connected device",
Long: `Streams MJPEG screen capture from a specified device to stdout. Only supports MJPEG format.`,
RunE: func(cmd *cobra.Command, args []string) error {
// Validate format
if screencaptureFormat != "mjpeg" {
response := commands.NewErrorResponse(fmt.Errorf("format must be 'mjpeg' for screen capture"))
printJson(response)
return fmt.Errorf(response.Error)
}

// Find the target device
targetDevice, err := commands.FindDeviceOrAutoSelect(deviceId)
if err != nil {
response := commands.NewErrorResponse(fmt.Errorf("error finding device: %v", err))
printJson(response)
return fmt.Errorf(response.Error)
}

// Start agent
err = targetDevice.StartAgent()
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

meh name

if err != nil {
response := commands.NewErrorResponse(fmt.Errorf("error starting agent: %v", err))
printJson(response)
return fmt.Errorf(response.Error)
}

// Start screen capture and stream to stdout
err = targetDevice.StartScreenCapture(screencaptureFormat, func(data []byte) bool {
_, writeErr := os.Stdout.Write(data)
if writeErr != nil {
fmt.Fprintf(os.Stderr, "Error writing data: %v\n", writeErr)
return false
}
return true
})

if err != nil {
response := commands.NewErrorResponse(fmt.Errorf("error starting screen capture: %v", err))
printJson(response)
return fmt.Errorf(response.Error)
}

return nil
},
}

var deviceCmd = &cobra.Command{
Use: "device",
Short: "Device management commands",
Long: `Commands for managing individual devices including rebooting and getting device information.`,
}

var deviceRebootCmd = &cobra.Command{
Use: "reboot",
Short: "Reboot a connected device or simulator",
Long: `Reboots a specified device (using its ID). Supports iOS (real/simulator) and Android (real/emulator).`,
Expand All @@ -123,7 +181,7 @@ var rebootCmd = &cobra.Command{
},
}

var infoCmd = &cobra.Command{
var deviceInfoCmd = &cobra.Command{
Use: "info",
Short: "Get device info",
Long: `Get detailed information about a connected device, such as OS, version, and screen size.`,
Expand Down Expand Up @@ -347,14 +405,18 @@ func init() {

// add main commands
rootCmd.AddCommand(devicesCmd)
rootCmd.AddCommand(deviceCmd)
rootCmd.AddCommand(screenshotCmd)
rootCmd.AddCommand(rebootCmd)
rootCmd.AddCommand(infoCmd)
rootCmd.AddCommand(screencaptureCmd)
rootCmd.AddCommand(ioCmd)
rootCmd.AddCommand(appsCmd)
rootCmd.AddCommand(serverCmd)
rootCmd.AddCommand(urlCmd)

// add device subcommands
deviceCmd.AddCommand(deviceRebootCmd)
deviceCmd.AddCommand(deviceInfoCmd)

// add io subcommands
ioCmd.AddCommand(ioTapCmd)
ioCmd.AddCommand(ioButtonCmd)
Expand All @@ -370,31 +432,33 @@ func init() {
serverStartCmd.Flags().String("listen", "", "Address to listen on (e.g., 'localhost:12000' or '0.0.0.0:13000')")
serverStartCmd.Flags().Bool("cors", false, "Enable CORS support")

// devices command flags
devicesCmd.Flags().StringVar(&platform, "platform", "", "target platform (ios or android)")
devicesCmd.Flags().StringVar(&deviceType, "type", "", "filter by device type (real or simulator/emulator)")

screenshotCmd.Flags().StringVar(&deviceId, "device", "", "ID of the device to take screenshot from (optional if only one device connected)")
// screenshot command flags
screenshotCmd.Flags().StringVar(&deviceId, "device", "", "ID of the device to take screenshot from")
screenshotCmd.Flags().StringVarP(&screenshotOutputPath, "output", "o", "", "Output file path for screenshot (e.g., screen.png, or '-' for stdout)")
screenshotCmd.Flags().StringVarP(&screenshotFormat, "format", "f", "png", "Output format for screenshot (png or jpeg)")
screenshotCmd.Flags().IntVarP(&screenshotJpegQuality, "quality", "q", 90, "JPEG quality (1-100, only applies if format is jpeg)")

rebootCmd.Flags().StringVar(&deviceId, "device", "", "ID of the device to reboot")
// screencapture command flags
screencaptureCmd.Flags().StringVar(&deviceId, "device", "", "ID of the device to capture from")
screencaptureCmd.Flags().StringVarP(&screencaptureFormat, "format", "f", "mjpeg", "Output format for screen capture")

infoCmd.Flags().StringVar(&deviceId, "device", "", "ID of the device to get info from (optional if only one device connected)")
// device command flags
deviceRebootCmd.Flags().StringVar(&deviceId, "device", "", "ID of the device to reboot")
deviceInfoCmd.Flags().StringVar(&deviceId, "device", "", "ID of the device to get info from")

// io command flags
ioTapCmd.Flags().StringVar(&deviceId, "device", "", "ID of the device to tap on")

ioButtonCmd.Flags().StringVar(&deviceId, "device", "", "ID of the device to press button on")

ioTextCmd.Flags().StringVar(&deviceId, "device", "", "ID of the device to send keys to")

// apps command flags
appsLaunchCmd.Flags().StringVar(&deviceId, "device", "", "ID of the device to launch app on")

appsTerminateCmd.Flags().StringVar(&deviceId, "device", "", "ID of the device to terminate app on (optional if only one device connected)")

appsListCmd.Flags().StringVar(&deviceId, "device", "", "ID of the device to list apps from (optional if only one device connected)")
appsTerminateCmd.Flags().StringVar(&deviceId, "device", "", "ID of the device to terminate app on")
appsListCmd.Flags().StringVar(&deviceId, "device", "", "ID of the device to list apps from")

// url command flags
urlCmd.Flags().StringVar(&deviceId, "device", "", "ID of the device to open URL on")
Expand All @@ -410,22 +474,3 @@ func main() {
os.Exit(1)
}
}

func findTargetDevice(deviceID string) (devices.ControllableDevice, error) {
if deviceID == "" {
return nil, fmt.Errorf("--device flag is required")
}

allDevices, err := devices.GetAllControllableDevices()
if err != nil {
return nil, fmt.Errorf("failed to list devices: %w", err)
}

for _, d := range allDevices {
if d.ID() == deviceID {
return d, nil
}
}

return nil, fmt.Errorf("device with ID '%s' not found", deviceID)
}
Loading