@@ -3,6 +3,9 @@ package command
33import (
44 "context"
55 "fmt"
6+ "os"
7+ "os/signal"
8+ "syscall"
69
710 "github.com/pkg/errors"
811 "github.com/sirupsen/logrus"
@@ -45,7 +48,11 @@ func newExecCommand() *cobra.Command {
4548 RunE : func (cmd * cobra.Command , args []string ) error {
4649 options .container = args [0 ]
4750 options .command = args [1 :]
48- return runExec (options )
51+ err := runExec (options )
52+ if errors .Is (err , context .Canceled ) {
53+ return nil
54+ }
55+ return err
4956 },
5057 }
5158
@@ -69,7 +76,7 @@ func newExecCommand() *cobra.Command {
6976 return cmd
7077}
7178
72- func buildCli (options execOptions ) (* DebugCli , error ) {
79+ func buildCli (ctx context. Context , options execOptions ) (* DebugCli , error ) {
7380 conf , err := config .LoadConfig ()
7481 if err != nil {
7582 return nil , err
@@ -105,18 +112,36 @@ func buildCli(options execOptions) (*DebugCli, error) {
105112 opts = append (opts , WithClientConfig (opt ))
106113 }
107114
108- return NewDebugCli (opts ... )
115+ cli , err := NewDebugCli (opts ... )
116+ if err != nil {
117+ return nil , err
118+ }
119+ cli .ctx = ctx
120+ return cli , err
109121}
110122
111123func runExec (options execOptions ) error {
124+ var ctx , cancel = context .WithCancel (context .Background ())
112125 logrus .SetLevel (logrus .ErrorLevel )
113126 var containerID string
114- cli , err := buildCli (options )
127+ cli , err := buildCli (ctx , options )
115128 if err != nil {
129+ cancel ()
116130 return err
117131 }
132+ go func () {
133+ c := make (chan os.Signal , 1 )
134+ signal .Notify (c , syscall .SIGINT , syscall .SIGTERM )
135+ <- c
136+ cancel ()
137+ }()
118138 conf := cli .Config ()
119139 defer func () {
140+ if err != nil {
141+ if errors .Is (err , context .Canceled ) {
142+ cli .ctx = context .Background ()
143+ }
144+ }
120145 if containerID != "" {
121146 err = cli .ContainerClean (containerID )
122147 if err != nil {
@@ -130,31 +155,37 @@ func runExec(options execOptions) error {
130155 }()
131156 _ , err = cli .Ping ()
132157 if err != nil {
158+ cancel ()
133159 return err
134160 }
135161 // find image
136162 images , err := cli .FindImage (conf .Image )
137163 if err != nil {
164+ cancel ()
138165 return err
139166 }
140167
141168 if len (images ) == 0 {
142169 // pull image
143170 err = cli .PullImage (conf .Image )
144171 if err != nil {
172+ cancel ()
145173 return err
146174 }
147175 }
148176 containerID , err = cli .FindContainer (options .container )
149177 if err != nil {
178+ cancel ()
150179 return err
151180 }
152181 containerID , err = cli .CreateContainer (containerID , options )
153182 if err != nil {
183+ cancel ()
154184 return err
155185 }
156186 resp , err := cli .ExecCreate (options , containerID )
157187 if err != nil {
188+ cancel ()
158189 return err
159190 }
160191
@@ -167,15 +198,18 @@ func runExec(options execOptions) error {
167198 }()
168199 }()
169200 if cli .In ().IsTerminal () {
170- if err := tty .MonitorTtySize (context . Background () , cli .Client (), cli .Out (), resp .ID , true ); err != nil {
201+ if err := tty .MonitorTtySize (ctx , cli .Client (), cli .Out (), resp .ID , true ); err != nil {
171202 _ , _ = fmt .Fprintln (cli .Err (), "Error monitoring TTY size:" , err )
172203 }
173204 }
174205
175- if err := <- errCh ; err != nil {
206+ err = <- errCh
207+ if err != nil {
176208 logrus .Debugf ("Error hijack: %s" , err )
209+ cancel ()
177210 return err
178211 }
212+ cancel ()
179213 return nil
180214}
181215
0 commit comments