Skip to content

Commit 31a9080

Browse files
committed
fix: fix linting and add errors constant
1 parent 85db372 commit 31a9080

7 files changed

Lines changed: 97 additions & 87 deletions

File tree

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,9 @@ go.work
3636
# Screenshots and recordings
3737
screenshot_*.png
3838
recording_*.mp4
39+
*.mp4
40+
*.gif
41+
*.png
3942

4043
# Temporary files
4144
/tmp/

cmd/device.go

Lines changed: 8 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -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.
413402
func 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

456444
func 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.
480466
func 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
}

cmd/errors.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package cmd
2+
3+
import "errors"
4+
5+
var (
6+
ErrNoRunningIOSSimulator = errors.New("no running iOS simulator found")
7+
ErrNoRunningAndroidEmulator = errors.New("no running Android emulator found")
8+
ErrIOSSimulatorNotRunning = errors.New("iOS simulator not found or not running")
9+
ErrAndroidEmulatorNotRunning = errors.New("android emulator not found or not running")
10+
ErrDeviceNotRunning = errors.New("device not found or not a running iOS simulator or Android emulator")
11+
ErrNoActiveDevice = errors.New("no active iOS simulator or Android emulator found")
12+
ErrFFmpegNotInstalled = errors.New("ffmpeg is not installed. Please install ffmpeg to use the GIF conversion feature")
13+
)

cmd/list.go

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -52,18 +52,17 @@ var listCmd = &cobra.Command{
5252

5353
runtimeVal := device.Runtime
5454
if strings.Contains(runtimeVal, "com.apple.CoreSimulator.SimRuntime.iOS-") {
55-
// Extract iOS version from runtime string
5655
parts := strings.Split(runtimeVal, "-")
5756
if len(parts) >= 2 {
5857
version := strings.Join(parts[len(parts)-2:], ".")
5958
runtimeVal = "iOS " + version
6059
}
6160
}
6261

63-
table.Append([]string{device.Type, device.Name, device.State, udid, runtimeVal})
62+
_ = table.Append([]string{device.Type, device.Name, device.State, udid, runtimeVal})
6463
}
6564

66-
table.Render()
65+
_ = table.Render()
6766
},
6867
}
6968

@@ -112,6 +111,7 @@ func getAndroidEmulators() []Device {
112111
if err != nil {
113112
// Emulator command might not be in path, but adb might work.
114113
// We can proceed and just list running devices.
114+
fmt.Printf("Could not run 'emulator -list-avds': %v. Only running emulators will be listed.\n", err)
115115
}
116116
avdLines := strings.Split(strings.TrimSpace(string(avdOutput)), "\n")
117117
avdMap := make(map[string]bool)
@@ -125,7 +125,7 @@ func getAndroidEmulators() []Device {
125125
adbCmd := exec.Command(CmdAdb, "devices")
126126
adbOutput, err := adbCmd.Output()
127127
if err != nil {
128-
var devices []Device
128+
devices := make([]Device, 0, len(avdMap))
129129
for avd := range avdMap {
130130
devices = append(devices, Device{
131131
Name: avd,
@@ -135,12 +135,13 @@ func getAndroidEmulators() []Device {
135135
Runtime: "Android",
136136
})
137137
}
138+
138139
return devices
139140
}
140141

141142
runningDevices := make(map[string]string) // map[name]udid
142-
lines := strings.Split(string(adbOutput), "\n")
143-
for _, line := range lines {
143+
lines := strings.SplitSeq(string(adbOutput), "\n")
144+
for line := range lines {
144145
if strings.Contains(line, "emulator-") && strings.Contains(line, "device") {
145146
parts := strings.Fields(line)
146147
if len(parts) > 0 {
@@ -155,7 +156,7 @@ func getAndroidEmulators() []Device {
155156
}
156157
}
157158

158-
var devices []Device
159+
devices := make([]Device, 0, len(avdMap)+len(runningDevices))
159160
for name, udid := range runningDevices {
160161
devices = append(devices, Device{
161162
Name: name,
@@ -164,14 +165,13 @@ func getAndroidEmulators() []Device {
164165
Type: TypeAndroidEmulator,
165166
Runtime: "Android",
166167
})
167-
// Remove from avdMap so we don't list it twice
168168
delete(avdMap, name)
169169
}
170170

171171
for avd := range avdMap {
172172
devices = append(devices, Device{
173173
Name: avd,
174-
UDID: "offline",
174+
UDID: "N/A",
175175
State: StateShutdown,
176176
Type: TypeAndroidEmulator,
177177
Runtime: "Android",
@@ -180,7 +180,3 @@ func getAndroidEmulators() []Device {
180180

181181
return devices
182182
}
183-
184-
func init() {
185-
rootCmd.AddCommand(listCmd)
186-
}

0 commit comments

Comments
 (0)