forked from superfly/flyctl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
82 lines (61 loc) · 1.54 KB
/
main.go
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package main
//go:generate go run github.com/Khan/genqlient
import (
"context"
"fmt"
"os"
"os/signal"
"runtime"
"syscall"
dockerclient "github.com/docker/docker/client"
dockeropts "github.com/docker/docker/opts"
"github.com/superfly/flyctl/iostreams"
"github.com/superfly/flyctl/internal/buildinfo"
"github.com/superfly/flyctl/internal/cli"
"github.com/superfly/flyctl/internal/sentry"
)
func main() {
guessDockerHost()
os.Exit(run())
}
func run() (exitCode int) {
defer sentry.Flush()
ctx, cancel := newContext()
defer cancel()
go handleDebugSignal(ctx)
if !buildinfo.IsDev() {
defer func() {
if r := recover(); r != nil {
sentry.Recover(r)
exitCode = 3
}
}()
}
exitCode = cli.Run(ctx, iostreams.System(), os.Args[1:]...)
return
}
func newContext() (context.Context, context.CancelFunc) {
// NOTE: when signal.Notify is called for os.Interrupt it traps both
// ^C (Control-C) and ^BREAK (Control-Break) on Windows.
signals := []os.Signal{os.Interrupt}
if runtime.GOOS != "windows" {
signals = append(signals, syscall.SIGTERM)
}
return signal.NotifyContext(context.Background(), signals...)
}
func guessDockerHost() {
host := os.Getenv("DOCKER_HOST")
if host == "" {
return // no docker host specified
}
if _, err := dockerclient.ParseHostURL(host); err == nil {
return // host is well defined
}
host, err := dockeropts.ParseHost(false, false, host)
if err != nil {
return
}
if err := os.Setenv("DOCKER_HOST", host); err != nil {
panic(fmt.Errorf("failed fixing DOCKER_HOST: %w", err))
}
}