@@ -2,16 +2,12 @@ package command
22
33import (
44 "context"
5- "fmt"
6- "os"
7- "os/signal"
8- "syscall"
95
106 "github.com/pkg/errors"
117 "github.com/sirupsen/logrus"
128 "github.com/spf13/cobra"
9+
1310 "github.com/zeromake/docker-debug/internal/config"
14- "github.com/zeromake/docker-debug/pkg/tty"
1511)
1612
1713var rootCmd = newExecCommand ()
@@ -114,63 +110,33 @@ func runExec(options execOptions) error {
114110 var ctx , cancel = context .WithCancel (context .Background ())
115111 defer cancel ()
116112 logrus .SetLevel (logrus .ErrorLevel )
117- var containerID string
113+
118114 cli , err := buildCli (ctx , options )
119115 if err != nil {
120116 return err
121117 }
122- go func () {
123- c := make (chan os.Signal , 1 )
124- signal .Notify (c , syscall .SIGINT , syscall .SIGTERM )
125- <- c
126- cancel ()
127- }()
118+ defer cli .Close ()
119+
128120 conf := cli .Config ()
129- defer func () {
130- cc := cli .ctx
131- if err != nil {
132- logrus .Errorf ("Error: %s" , err .Error ())
133- if errors .Is (err , context .Canceled ) {
134- cc = context .Background ()
135- }
136- }
137- if containerID != "" {
138- err = cli .ContainerClean (cc , containerID )
139- if err != nil {
140- logrus .Debugf ("%+v" , err )
141- }
142- }
143- err = cli .Close ()
144- if err != nil {
145- logrus .Debugf ("%+v" , err )
146- }
147- }()
148- _ , err = cli .Ping ()
149- if err != nil {
150- return err
151- }
152121 // find image
153122 images , err := cli .FindImage (conf .Image )
154123 if err != nil {
155124 return err
156125 }
157-
158126 if len (images ) == 0 {
159127 // pull image
160128 err = cli .PullImage (conf .Image )
161129 if err != nil {
162130 return err
163131 }
164132 }
165- var originContainerID string
166- originContainerID , err = cli .FindContainer (options .container )
167- if err != nil {
168- return err
169- }
170- containerID , err = cli .CreateContainer (originContainerID , options )
133+
134+ containerID , err := cli .CreateContainer (options .container , options )
171135 if err != nil {
172136 return err
173137 }
138+ defer cli .ContainerClean (ctx , containerID )
139+
174140 resp , err := cli .ExecCreate (options , containerID )
175141 if err != nil {
176142 return err
@@ -180,21 +146,13 @@ func runExec(options execOptions) error {
180146 defer close (errCh )
181147
182148 go func () {
183- errCh <- func () error {
184- return cli .ExecStart (options , resp .ID )
185- }()
149+ errCh <- cli .ExecStart (options , resp .ID )
150+ }()
151+ go func () {
152+ errCh <- cli .WatchContainer (ctx , options .container )
186153 }()
187- if cli .In ().IsTerminal () {
188- if err = tty .MonitorTtySize (ctx , cli .Client (), cli .Out (), resp .ID , true ); err != nil {
189- _ , _ = fmt .Fprintln (cli .Err (), "Error monitoring TTY size:" , err )
190- }
191- }
192154
193- if err = <- errCh ; err != nil {
194- logrus .Debugf ("Error hijack: %s" , err )
195- return err
196- }
197- return nil
155+ return <- errCh
198156}
199157
200158// Execute main func
0 commit comments