Skip to content

Commit ffeb2ba

Browse files
committed
more cleanups
1 parent 5046b8c commit ffeb2ba

File tree

16 files changed

+208
-91
lines changed

16 files changed

+208
-91
lines changed

cli/device.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ var deviceInfoCmd = &cobra.Command{
4949

5050
// Start agent
5151
err = targetDevice.StartAgent(devices.StartAgentConfig{
52-
Registry: commands.GetRegistry(),
52+
Hook: commands.GetShutdownHook(),
5353
})
5454
if err != nil {
5555
response := commands.NewErrorResponse(fmt.Errorf("error starting agent: %v", err))

cli/screenshot.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ var screencaptureCmd = &cobra.Command{
8080
OnProgress: func(message string) {
8181
utils.Verbose(message)
8282
},
83-
Registry: commands.GetRegistry(),
83+
Hook: commands.GetShutdownHook(),
8484
})
8585
if err != nil {
8686
response := commands.NewErrorResponse(fmt.Errorf("error starting agent: %v", err))

commands/commands.go

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -33,24 +33,24 @@ func NewErrorResponse(err error) *CommandResponse {
3333
// DeviceCache provides a simple cache for devices to avoid repeated lookups
3434
var deviceCache = make(map[string]devices.ControllableDevice)
3535

36-
// deviceRegistry holds the registry for device cleanup tracking.
37-
// It is set once at application startup via SetRegistry and used by commands
38-
// to register devices for graceful shutdown cleanup.
39-
var deviceRegistry *devices.DeviceRegistry
36+
// shutdownHook holds the shutdown hook for resource cleanup tracking.
37+
// It is set once at application startup via SetShutdownHook and used by commands
38+
// to register cleanup functions for graceful shutdown.
39+
var shutdownHook *devices.ShutdownHook
4040

41-
// SetRegistry sets the global device registry for cleanup tracking.
41+
// SetShutdownHook sets the global shutdown hook for resource cleanup.
4242
// This should be called once at application startup (main.go or server.go).
43-
// The registry is used to track active devices and clean up their resources
43+
// The hook is used to register cleanup functions that will be called
4444
// during graceful shutdown (SIGINT/SIGTERM).
45-
func SetRegistry(registry *devices.DeviceRegistry) {
46-
deviceRegistry = registry
45+
func SetShutdownHook(hook *devices.ShutdownHook) {
46+
shutdownHook = hook
4747
}
4848

49-
// GetRegistry returns the current device registry.
50-
// Returns nil if SetRegistry has not been called yet.
51-
// Commands use this to register devices when StartAgent is called.
52-
func GetRegistry() *devices.DeviceRegistry {
53-
return deviceRegistry
49+
// GetShutdownHook returns the current shutdown hook.
50+
// Returns nil if SetShutdownHook has not been called yet.
51+
// Commands use this to register cleanup functions.
52+
func GetShutdownHook() *devices.ShutdownHook {
53+
return shutdownHook
5454
}
5555

5656
// FindDevice finds a device by ID, using cache when possible

commands/dump.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ func DumpUICommand(req DumpUIRequest) *CommandResponse {
2727

2828
// Start agent if needed
2929
err = targetDevice.StartAgent(devices.StartAgentConfig{
30-
Registry: GetRegistry(),
30+
Hook: GetShutdownHook(),
3131
})
3232
if err != nil {
3333
return NewErrorResponse(fmt.Errorf("failed to start agent on device %s: %w", targetDevice.ID(), err))

commands/info.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ func InfoCommand(deviceID string) *CommandResponse {
1818
}
1919

2020
err = targetDevice.StartAgent(devices.StartAgentConfig{
21-
Registry: GetRegistry(),
21+
Hook: GetShutdownHook(),
2222
})
2323
if err != nil {
2424
return NewErrorResponse(fmt.Errorf("error starting agent: %v", err))

commands/input.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ func TapCommand(req TapRequest) *CommandResponse {
6262
}
6363

6464
err = targetDevice.StartAgent(devices.StartAgentConfig{
65-
Registry: GetRegistry(),
65+
Hook: GetShutdownHook(),
6666
})
6767
if err != nil {
6868
return NewErrorResponse(fmt.Errorf("failed to start agent on device %s: %v", targetDevice.ID(), err))
@@ -90,7 +90,7 @@ func LongPressCommand(req LongPressRequest) *CommandResponse {
9090
}
9191

9292
err = targetDevice.StartAgent(devices.StartAgentConfig{
93-
Registry: GetRegistry(),
93+
Hook: GetShutdownHook(),
9494
})
9595
if err != nil {
9696
return NewErrorResponse(fmt.Errorf("failed to start agent on device %s: %v", targetDevice.ID(), err))
@@ -118,7 +118,7 @@ func TextCommand(req TextRequest) *CommandResponse {
118118
}
119119

120120
err = targetDevice.StartAgent(devices.StartAgentConfig{
121-
Registry: GetRegistry(),
121+
Hook: GetShutdownHook(),
122122
})
123123
if err != nil {
124124
return NewErrorResponse(fmt.Errorf("failed to start agent on device %s: %v", targetDevice.ID(), err))
@@ -146,7 +146,7 @@ func ButtonCommand(req ButtonRequest) *CommandResponse {
146146
}
147147

148148
err = targetDevice.StartAgent(devices.StartAgentConfig{
149-
Registry: GetRegistry(),
149+
Hook: GetShutdownHook(),
150150
})
151151
if err != nil {
152152
return NewErrorResponse(fmt.Errorf("failed to start agent on device %s: %v", targetDevice.ID(), err))
@@ -174,7 +174,7 @@ func GestureCommand(req GestureRequest) *CommandResponse {
174174
}
175175

176176
err = targetDevice.StartAgent(devices.StartAgentConfig{
177-
Registry: GetRegistry(),
177+
Hook: GetShutdownHook(),
178178
})
179179
if err != nil {
180180
return NewErrorResponse(fmt.Errorf("failed to start agent on device %s: %v", targetDevice.ID(), err))
@@ -213,7 +213,7 @@ func SwipeCommand(req SwipeRequest) *CommandResponse {
213213
}
214214

215215
err = targetDevice.StartAgent(devices.StartAgentConfig{
216-
Registry: GetRegistry(),
216+
Hook: GetShutdownHook(),
217217
})
218218
if err != nil {
219219
return NewErrorResponse(fmt.Errorf("failed to start agent on device %s: %v", targetDevice.ID(), err))

commands/orientation.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ func OrientationGetCommand(req OrientationGetRequest) *CommandResponse {
3131

3232
// start agent if needed
3333
err = device.StartAgent(devices.StartAgentConfig{
34-
Registry: GetRegistry(),
34+
Hook: GetShutdownHook(),
3535
})
3636
if err != nil {
3737
return NewErrorResponse(fmt.Errorf("failed to start agent on device %s: %v", device.ID(), err))
@@ -63,7 +63,7 @@ func OrientationSetCommand(req OrientationSetRequest) *CommandResponse {
6363

6464
// start agent if needed
6565
err = device.StartAgent(devices.StartAgentConfig{
66-
Registry: GetRegistry(),
66+
Hook: GetShutdownHook(),
6767
})
6868
if err != nil {
6969
return NewErrorResponse(fmt.Errorf("failed to start agent on device %s: %v", device.ID(), err))

commands/screenshot.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ func ScreenshotCommand(req ScreenshotRequest) *CommandResponse {
5555

5656
// Start agent if needed
5757
err = targetDevice.StartAgent(devices.StartAgentConfig{
58-
Registry: GetRegistry(),
58+
Hook: GetShutdownHook(),
5959
})
6060
if err != nil {
6161
return NewErrorResponse(fmt.Errorf("failed to start agent on device %s: %v", targetDevice.ID(), err))

commands/url.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ func URLCommand(req URLRequest) *CommandResponse {
2424
}
2525

2626
err = targetDevice.StartAgent(devices.StartAgentConfig{
27-
Registry: GetRegistry(),
27+
Hook: GetShutdownHook(),
2828
})
2929
if err != nil {
3030
return NewErrorResponse(fmt.Errorf("failed to start agent on device %s: %v", targetDevice.ID(), err))

devices/common.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ type ScreenCaptureConfig struct {
3131
// StartAgentConfig contains configuration for agent startup operations
3232
type StartAgentConfig struct {
3333
OnProgress func(message string) // optional progress callback
34-
Registry *DeviceRegistry // optional registry for cleanup tracking
34+
Hook *ShutdownHook // optional shutdown hook for cleanup tracking
3535
}
3636

3737
// ScreenElementRect represents the rectangle coordinates and dimensions

0 commit comments

Comments
 (0)