File tree 1 file changed +30
-3
lines changed
1 file changed +30
-3
lines changed Original file line number Diff line number Diff line change @@ -28,16 +28,43 @@ import (
28
28
"go.opentelemetry.io/otel"
29
29
)
30
30
31
+ var errCtxUserTerminated = errors .New ("user terminated the process" )
32
+
31
33
func main () {
32
- err := dockerMain (context .Background ())
33
- if err != nil && ! errdefs .IsCancelled (err ) {
34
+ ctx := context .Background ()
35
+ err := dockerMain (ctx )
36
+ if err != nil && ! errdefs .IsCancelled (err ) && ! errors .Is (err , errCtxUserTerminated ) {
34
37
_ , _ = fmt .Fprintln (os .Stderr , err )
35
38
os .Exit (getExitCode (err ))
36
39
}
37
40
}
38
41
42
+ func notifyContext (ctx context.Context , signals ... os.Signal ) (context.Context , context.CancelFunc ) {
43
+ ch := make (chan os.Signal , 1 )
44
+ signal .Notify (ch , signals ... )
45
+
46
+ ctx , cancel := context .WithCancelCause (ctx )
47
+
48
+ go func () {
49
+ select {
50
+ case <- ctx .Done ():
51
+ signal .Stop (ch )
52
+ return
53
+ case <- ch :
54
+ cancel (errCtxUserTerminated )
55
+ signal .Stop (ch )
56
+ return
57
+ }
58
+ }()
59
+
60
+ return ctx , func () {
61
+ signal .Stop (ch )
62
+ cancel (nil )
63
+ }
64
+ }
65
+
39
66
func dockerMain (ctx context.Context ) error {
40
- ctx , cancelNotify := signal . NotifyContext (ctx , platformsignals .TerminationSignals ... )
67
+ ctx , cancelNotify := notifyContext (ctx , platformsignals .TerminationSignals ... )
41
68
defer cancelNotify ()
42
69
43
70
dockerCli , err := command .NewDockerCli (command .WithBaseContext (ctx ))
You can’t perform that action at this time.
0 commit comments