Skip to content

Commit bc69baf

Browse files
committed
fix: update docker term dep
1 parent 71f9038 commit bc69baf

File tree

9 files changed

+127
-145
lines changed

9 files changed

+127
-145
lines changed

cmd/docker-debug/main.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package main
22

3-
import "github.com/zeromake/docker-debug/internal/command"
3+
import (
4+
"github.com/zeromake/docker-debug/internal/command"
5+
)
46

57
func main() {
68
command.Execute()

go.mod

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,27 +3,33 @@ module github.com/zeromake/docker-debug
33
go 1.12
44

55
require (
6-
github.com/Azure/go-ansiterm v0.0.0-20170929234023-d6e3b3328b78 // indirect
76
github.com/BurntSushi/toml v0.3.1
8-
github.com/Microsoft/go-winio v0.4.14 // indirect
7+
github.com/Microsoft/go-winio v0.4.15-0.20200908182639-5b44b70ab3ab // indirect
98
github.com/blang/semver v3.5.1+incompatible
10-
github.com/containerd/containerd v1.3.4 // indirect
9+
github.com/containerd/containerd v1.4.1 // indirect
1110
github.com/docker/distribution v2.7.1+incompatible // indirect
12-
github.com/docker/docker v0.0.0-20200309214505-aa6a9891b09c
11+
github.com/docker/docker v0.0.0-20201009000351-654cad4d9dc4
1312
github.com/docker/go-connections v0.4.0 // indirect
1413
github.com/docker/go-units v0.4.0 // indirect
1514
github.com/gogo/protobuf v1.3.1 // indirect
16-
github.com/google/go-cmp v0.4.0 // indirect
1715
github.com/gorilla/mux v1.7.4 // indirect
16+
github.com/inconshreveable/mousetrap v1.0.0 // indirect
17+
github.com/kr/pretty v0.1.0 // indirect
1818
github.com/mitchellh/go-homedir v1.1.0
19+
github.com/moby/term v0.0.0-20200915141129-7f0af18e79f2
1920
github.com/morikuni/aec v1.0.0 // indirect
20-
github.com/opencontainers/go-digest v1.0.0-rc1 // indirect
21+
github.com/opencontainers/go-digest v1.0.0 // indirect
2122
github.com/opencontainers/image-spec v1.0.1 // indirect
2223
github.com/pkg/errors v0.9.1
23-
github.com/sirupsen/logrus v1.5.0
24-
github.com/spf13/cobra v1.0.0
25-
golang.org/x/sys v0.0.0-20200420163511-1957bb5e6d1f // indirect
24+
github.com/sirupsen/logrus v1.6.0
25+
github.com/spf13/cobra v0.0.2-0.20171109065643-2da4a54c5cee
26+
github.com/stretchr/testify v1.4.0 // indirect
27+
golang.org/x/net v0.0.0-20191004110552-13f9640d40b9 // indirect
28+
golang.org/x/sys v0.0.0-20201020230747-6e5568b54d1a // indirect
29+
golang.org/x/text v0.3.2 // indirect
2630
golang.org/x/time v0.0.0-20200416051211-89c76fbcd5d1 // indirect
27-
google.golang.org/grpc v1.28.1 // indirect
28-
gotest.tools v2.2.0+incompatible // indirect
31+
google.golang.org/genproto v0.0.0-20200117163144-32f20d992d24 // indirect
32+
google.golang.org/grpc v1.33.1 // indirect
33+
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 // indirect
34+
gopkg.in/yaml.v2 v2.2.8 // indirect
2935
)

go.sum

Lines changed: 46 additions & 108 deletions
Large diffs are not rendered by default.

internal/command/cli.go

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,25 +3,26 @@ package command
33
import (
44
"context"
55
"fmt"
6+
"io"
7+
"regexp"
8+
"runtime"
9+
"strings"
10+
"time"
11+
612
"github.com/docker/docker/api/types"
713
"github.com/docker/docker/api/types/container"
814
"github.com/docker/docker/api/types/filters"
915
"github.com/docker/docker/api/types/mount"
1016
"github.com/docker/docker/api/types/strslice"
1117
"github.com/docker/docker/pkg/jsonmessage"
18+
"github.com/moby/term"
1219
"github.com/pkg/errors"
1320
"github.com/sirupsen/logrus"
1421
"github.com/zeromake/docker-debug/internal/config"
1522
"github.com/zeromake/docker-debug/pkg/opts"
1623
"github.com/zeromake/docker-debug/pkg/tty"
17-
"io"
18-
"regexp"
19-
"runtime"
20-
"strings"
21-
"time"
2224

2325
"github.com/docker/docker/client"
24-
"github.com/docker/docker/pkg/term"
2526
"github.com/zeromake/docker-debug/pkg/stream"
2627
"github.com/zeromake/docker-debug/version"
2728
)
@@ -35,6 +36,7 @@ const (
3536
defaultDomain = "docker.io"
3637
officialRepoName = "library"
3738
)
39+
3840
var (
3941
containerIDReg = regexp.MustCompile("^([0-9a-fA-F]{12})|([0-9a-fA-F]{64})$")
4042
)
@@ -61,6 +63,7 @@ type DebugCli struct {
6163
err io.Writer
6264
client client.APIClient
6365
config *config.Config
66+
ctx context.Context
6467
}
6568

6669
// NewDebugCli new DebugCli
@@ -241,7 +244,7 @@ func (cli *DebugCli) Ping() (types.Ping, error) {
241244
}
242245

243246
func (cli *DebugCli) withContent(timeout time.Duration) (context.Context, context.CancelFunc) {
244-
return context.WithTimeout(context.Background(), timeout)
247+
return context.WithTimeout(cli.ctx, timeout)
245248
}
246249

247250
func containerMode(name string) string {
@@ -294,7 +297,7 @@ func (cli *DebugCli) CreateContainer(attachContainer string, options execOptions
294297
mountLen := len(mountArgs)
295298
if mountLen > 0 && mountLen <= 3 {
296299
mountDefault := mount.Mount{
297-
Type: "bind",
300+
Type: "bind",
298301
ReadOnly: false,
299302
}
300303
switch mountLen {
@@ -335,6 +338,7 @@ func (cli *DebugCli) CreateContainer(attachContainer string, options execOptions
335338
Mounts: mounts,
336339
SecurityOpt: options.securityOpts,
337340
CapAdd: options.capAdds,
341+
AutoRemove: true,
338342
//VolumesFrom: []string{attachContainer},
339343
}
340344

@@ -348,6 +352,7 @@ func (cli *DebugCli) CreateContainer(attachContainer string, options execOptions
348352
conf,
349353
hostConfig,
350354
nil,
355+
nil,
351356
"",
352357
)
353358
cancel()
@@ -368,12 +373,11 @@ func (cli *DebugCli) CreateContainer(attachContainer string, options execOptions
368373
func (cli *DebugCli) ContainerClean(id string) error {
369374
ctx, cancel := cli.withContent(cli.config.Timeout)
370375
defer cancel()
371-
return errors.WithStack(cli.client.ContainerRemove(
376+
timeout := time.Second
377+
return errors.WithStack(cli.client.ContainerStop(
372378
ctx,
373379
id,
374-
types.ContainerRemoveOptions{
375-
Force: true,
376-
},
380+
&timeout,
377381
))
378382
}
379383

@@ -424,7 +428,7 @@ func (cli *DebugCli) ExecStart(options execOptions, execID string) error {
424428
Resp: response,
425429
TTY: true,
426430
}
427-
return streamer.Stream(context.Background())
431+
return streamer.Stream(cli.ctx)
428432
}
429433

430434
// FindContainer find container

internal/command/root.go

Lines changed: 40 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@ package command
33
import (
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

111123
func 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

pkg/stream/in.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import (
55
"os"
66
"runtime"
77

8-
"github.com/docker/docker/pkg/term"
8+
"github.com/moby/term"
99
"github.com/pkg/errors"
1010
)
1111

pkg/stream/out.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import (
55
"io"
66
"os"
77

8-
"github.com/docker/docker/pkg/term"
8+
"github.com/moby/term"
99
"github.com/pkg/errors"
1010
)
1111

pkg/stream/stream.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
package stream
22

3-
import (
4-
"github.com/docker/docker/pkg/term"
5-
)
3+
import "github.com/moby/term"
64

75
// Streams interface
86
type Streams interface {

pkg/tty/hijack.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import (
99
"github.com/docker/docker/api/types"
1010
"github.com/docker/docker/pkg/ioutils"
1111
"github.com/docker/docker/pkg/stdcopy"
12-
"github.com/docker/docker/pkg/term"
12+
"github.com/moby/term"
1313
"github.com/pkg/errors"
1414
"github.com/sirupsen/logrus"
1515
"github.com/zeromake/docker-debug/pkg/stream"

0 commit comments

Comments
 (0)