@@ -19,7 +19,6 @@ Use 'lts' to start the last started device.`,
1919 Run : func (cmd * cobra.Command , args []string ) {
2020 deviceID := args [0 ]
2121
22- // Handle "lts" (last started) case
2322 if deviceID == "lts" {
2423 lastDevice , err := getLastStartedDevice ()
2524 if err != nil || lastDevice == nil {
@@ -83,7 +82,7 @@ var shutdownCmd = &cobra.Command{
8382 }
8483 }
8584
86- if stopAndroidEmulator (deviceID ) { // Android doesn't distinguish between stop and shutdown
85+ if stopAndroidEmulator (deviceID ) {
8786 return
8887 }
8988
@@ -210,7 +209,6 @@ func startIOSSimulator(deviceID string) bool {
210209 fmt .Printf ("Warning: Could not open Simulator app: %v\n " , err )
211210 }
212211
213- // Save as last started device with complete information
214212 device .State = StateBooted
215213 if err := saveLastStartedDevice (device ); err != nil {
216214 fmt .Printf ("Warning: Could not save last started device: %v\n " , err )
@@ -265,7 +263,6 @@ func restartIOSSimulator(deviceID string) bool {
265263 fmt .Printf ("Warning: Could not open Simulator app: %v\n " , err )
266264 }
267265
268- // Save as last started device with complete information
269266 device .State = StateBooted
270267 if err := saveLastStartedDevice (device ); err != nil {
271268 fmt .Printf ("Warning: Could not save last started device: %v\n " , err )
@@ -280,7 +277,6 @@ func startAndroidEmulator(deviceID string) bool {
280277 if isAndroidEmulatorRunning (deviceID ) {
281278 fmt .Printf ("Android emulator '%s' is already running\n " , deviceID )
282279
283- // Save as last started device even if already running
284280 udid , name := findRunningAndroidEmulator (deviceID )
285281 device := & Device {
286282 Name : name ,
@@ -306,10 +302,9 @@ func startAndroidEmulator(deviceID string) bool {
306302 return false
307303 }
308304
309- // Save as last started device
310305 device := & Device {
311306 Name : deviceID ,
312- UDID : "starting" , // Will be updated when emulator is fully running
307+ UDID : "starting" ,
313308 Type : TypeAndroidEmulator ,
314309 State : StateBooted ,
315310 }
@@ -346,7 +341,6 @@ func restartAndroidEmulator(deviceID string) bool {
346341 stopAndroidEmulator (deviceID )
347342
348343 if startAndroidEmulator (deviceID ) {
349- // Save as last started device
350344 device := & Device {
351345 Name : deviceID ,
352346 UDID : "restarting" ,
@@ -371,11 +365,9 @@ func deleteIOSSimulator(deviceID string) bool {
371365
372366 fmt .Printf ("Deleting iOS simulator '%s'...\n " , deviceID )
373367
374- // Shutdown the simulator if it's running
375368 shutdownCmd := exec .Command (CmdXCrun , CmdSimctl , "shutdown" , udid )
376- _ = shutdownCmd .Run () // Ignore error if already shutdown
369+ _ = shutdownCmd .Run ()
377370
378- // Delete the simulator
379371 cmd := exec .Command (CmdXCrun , CmdSimctl , "delete" , udid )
380372 if err := cmd .Run (); err != nil {
381373 fmt .Printf ("Error deleting iOS simulator: %v\n " , err )
@@ -394,10 +386,8 @@ func deleteAndroidEmulator(deviceID string) bool {
394386
395387 fmt .Printf ("Deleting Android emulator '%s'...\n " , deviceID )
396388
397- // Stop the emulator if it's running
398389 stopAndroidEmulator (deviceID )
399390
400- // Delete the AVD
401391 cmd := exec .Command (CmdAvdManager , "delete" , "avd" , "-n" , deviceID )
402392 if err := cmd .Run (); err != nil {
403393 fmt .Printf ("Error deleting Android emulator: %v\n " , err )
@@ -409,10 +399,8 @@ func deleteAndroidEmulator(deviceID string) bool {
409399 return true
410400}
411401
412- // findIOSSimulator returns the UDID and name of a simulator by its name or UDID.
413402func findIOSSimulator (deviceID string ) (string , string ) {
414403 if len (deviceID ) == 36 && strings .Count (deviceID , "-" ) == 4 {
415- // It's likely a UDID, find its name
416404 sims := getIOSSimulators ()
417405 for _ , sim := range sims {
418406 if sim .UDID == deviceID {
@@ -421,7 +409,6 @@ func findIOSSimulator(deviceID string) (string, string) {
421409 }
422410 }
423411
424- // It's a name, find its UDID
425412 sims := getIOSSimulators ()
426413 for _ , sim := range sims {
427414 if strings .EqualFold (sim .Name , deviceID ) {
@@ -450,7 +437,8 @@ func getRunningIOSSimulator() (*iOSSimulator, error) {
450437 return & iOSSimulator {udid : sim .UDID , name : sim .Name }, nil
451438 }
452439 }
453- return nil , fmt .Errorf ("no running iOS simulator found" )
440+
441+ return nil , ErrNoRunningIOSSimulator
454442}
455443
456444func doesAndroidAVDExist (avdName string ) bool {
@@ -475,17 +463,15 @@ func isAndroidEmulatorRunning(avdName string) bool {
475463 return udid != ""
476464}
477465
478- // findRunningAndroidEmulator returns the UDID and name of a running emulator.
479- // If avdName is empty, it returns the first running emulator it finds.
480466func findRunningAndroidEmulator (avdName string ) (string , string ) {
481467 cmd := exec .Command (CmdAdb , "devices" )
482468 output , err := cmd .Output ()
483469 if err != nil {
484470 return "" , ""
485471 }
486472
487- lines := strings .Split (string (output ), "\n " )
488- for _ , line := range lines {
473+ lines := strings .SplitSeq (string (output ), "\n " )
474+ for line := range lines {
489475 if strings .Contains (line , "emulator-" ) && strings .Contains (line , "device" ) {
490476 parts := strings .Fields (line )
491477 if len (parts ) > 0 {
@@ -494,7 +480,6 @@ func findRunningAndroidEmulator(avdName string) (string, string) {
494480 nameOutput , err := nameCmd .Output ()
495481 if err == nil {
496482 actualName := strings .TrimSpace (string (nameOutput ))
497- // If a name is specified, match it. Otherwise, return the first one.
498483 if avdName == "" || actualName == avdName {
499484 return emulatorID , actualName
500485 }
@@ -511,15 +496,6 @@ func getRunningAndroidEmulator() (*androidEmulator, error) {
511496 if udid != "" {
512497 return & androidEmulator {udid : udid , name : name }, nil
513498 }
514- return nil , fmt .Errorf ("no running Android emulator found" )
515- }
516499
517- func init () {
518- rootCmd .AddCommand (startCmd )
519- rootCmd .AddCommand (stopCmd )
520- rootCmd .AddCommand (shutdownCmd )
521- rootCmd .AddCommand (restartCmd )
522- rootCmd .AddCommand (deleteCmd )
523- rootCmd .AddCommand (lastCmd )
524- rootCmd .AddCommand (ltsCmd )
500+ return nil , ErrNoRunningAndroidEmulator
525501}
0 commit comments