-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathmain.go
More file actions
53 lines (46 loc) · 1.06 KB
/
main.go
File metadata and controls
53 lines (46 loc) · 1.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package main
import (
"fmt"
"os"
"os/signal"
"syscall"
"github.com/mobile-next/mobilecli/cli"
"github.com/mobile-next/mobilecli/commands"
"github.com/mobile-next/mobilecli/daemon"
"github.com/mobile-next/mobilecli/devices"
)
func main() {
// daemon child sets up its own signal handling in server.StartServer
if daemon.IsChild() {
if err := cli.Execute(); err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
return
}
// create shutdown hook for cleanup tracking
hook := devices.NewShutdownHook()
commands.SetShutdownHook(hook)
// setup signal handling for graceful shutdown
sigChan := make(chan os.Signal, 1)
signal.Notify(sigChan, syscall.SIGINT, syscall.SIGTERM)
// run command in goroutine
done := make(chan error, 1)
go func() {
done <- cli.Execute()
}()
// wait for command completion or signal
select {
case <-sigChan:
// cleanup resources on signal
hook.Shutdown()
os.Exit(0)
case err := <-done:
// cleanup resources on normal exit
hook.Shutdown()
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
}
}