From 6af6a3a5383642d2d7c3784f0f4e562dc324819a Mon Sep 17 00:00:00 2001 From: Daniel Nephin Date: Mon, 29 Aug 2016 13:36:29 -0400 Subject: [PATCH] Move holdHijackConnection to the container package. Signed-off-by: Daniel Nephin --- api/client/cli.go | 23 ++------------- api/client/container/attach.go | 2 +- api/client/container/exec.go | 5 ++-- api/client/{ => container}/hijack.go | 44 ++++++++++++++++++++++------ api/client/container/run.go | 2 +- api/client/container/start.go | 2 +- api/client/container/tty.go | 6 ++-- api/client/in.go | 6 ++-- api/client/out.go | 6 ++-- 9 files changed, 53 insertions(+), 43 deletions(-) rename api/client/{ => container}/hijack.go (65%) diff --git a/api/client/cli.go b/api/client/cli.go index 357504384f..e25ea95d6f 100644 --- a/api/client/cli.go +++ b/api/client/cli.go @@ -57,31 +57,12 @@ func (cli *DockerCli) ConfigFile() *configfile.ConfigFile { return cli.configFile } -func (cli *DockerCli) setRawTerminal() error { - if err := cli.in.setRawTerminal(); err != nil { - return err - } - return cli.out.setRawTerminal() -} - -func (cli *DockerCli) restoreTerminal(in io.Closer) error { - cli.in.restoreTerminal() - cli.out.restoreTerminal() - // WARNING: DO NOT REMOVE THE OS CHECK !!! - // For some reason this Close call blocks on darwin.. - // As the client exists right after, simply discard the close - // until we find a better solution. - if in != nil && runtime.GOOS != "darwin" { - return in.Close() - } - return nil -} - // Initialize the dockerCli runs initialization that must happen after command // line flags are parsed. -func (cli *DockerCli) Initialize(opts *cliflags.ClientOptions) (err error) { +func (cli *DockerCli) Initialize(opts *cliflags.ClientOptions) error { cli.configFile = LoadDefaultConfigFile(cli.err) + var err error cli.client, err = NewAPIClientFromFlags(opts.Common, cli.configFile) if err != nil { return err diff --git a/api/client/container/attach.go b/api/client/container/attach.go index aea3eec334..b0be3df90a 100644 --- a/api/client/container/attach.go +++ b/api/client/container/attach.go @@ -110,7 +110,7 @@ func runAttach(dockerCli *client.DockerCli, opts *attachOptions) error { logrus.Debugf("Error monitoring TTY size: %s", err) } } - if err := dockerCli.HoldHijackedConnection(ctx, c.Config.Tty, in, dockerCli.Out(), dockerCli.Err(), resp); err != nil { + if err := holdHijackedConnection(ctx, dockerCli, c.Config.Tty, in, dockerCli.Out(), dockerCli.Err(), resp); err != nil { return err } diff --git a/api/client/container/exec.go b/api/client/container/exec.go index 89338ae061..2913e63723 100644 --- a/api/client/container/exec.go +++ b/api/client/container/exec.go @@ -10,9 +10,8 @@ import ( "github.com/docker/docker/api/client" "github.com/docker/docker/api/types" "github.com/docker/docker/cli" - "github.com/docker/docker/pkg/promise" apiclient "github.com/docker/docker/client" - "github.com/docker/engine-api/types" + "github.com/docker/docker/pkg/promise" "github.com/spf13/cobra" ) @@ -127,7 +126,7 @@ func runExec(dockerCli *client.DockerCli, opts *execOptions, container string, e } defer resp.Close() errCh = promise.Go(func() error { - return dockerCli.HoldHijackedConnection(ctx, execConfig.Tty, in, out, stderr, resp) + return holdHijackedConnection(ctx, dockerCli, execConfig.Tty, in, out, stderr, resp) }) if execConfig.Tty && dockerCli.In().IsTerminal() { diff --git a/api/client/hijack.go b/api/client/container/hijack.go similarity index 65% rename from api/client/hijack.go rename to api/client/container/hijack.go index c7f7c3a0c0..5a1b4119d5 100644 --- a/api/client/hijack.go +++ b/api/client/container/hijack.go @@ -1,30 +1,36 @@ -package client +package container import ( "io" + "runtime" "sync" - "golang.org/x/net/context" - "github.com/Sirupsen/logrus" + "github.com/docker/docker/api/client" "github.com/docker/docker/api/types" "github.com/docker/docker/pkg/stdcopy" + "golang.org/x/net/context" ) -// HoldHijackedConnection handles copying input to and output from streams to the +type streams interface { + In() *client.InStream + Out() *client.OutStream +} + +// holdHijackedConnection handles copying input to and output from streams to the // connection -func (cli *DockerCli) HoldHijackedConnection(ctx context.Context, tty bool, inputStream io.ReadCloser, outputStream, errorStream io.Writer, resp types.HijackedResponse) error { +func holdHijackedConnection(ctx context.Context, streams streams, tty bool, inputStream io.ReadCloser, outputStream, errorStream io.Writer, resp types.HijackedResponse) error { var ( err error restoreOnce sync.Once ) if inputStream != nil && tty { - if err := cli.setRawTerminal(); err != nil { + if err := setRawTerminal(streams); err != nil { return err } defer func() { restoreOnce.Do(func() { - cli.restoreTerminal(inputStream) + restoreTerminal(streams, inputStream) }) }() } @@ -39,7 +45,7 @@ func (cli *DockerCli) HoldHijackedConnection(ctx context.Context, tty bool, inpu // so any following print messages will be in normal type. if inputStream != nil { restoreOnce.Do(func() { - cli.restoreTerminal(inputStream) + restoreTerminal(streams, inputStream) }) } } else { @@ -59,7 +65,7 @@ func (cli *DockerCli) HoldHijackedConnection(ctx context.Context, tty bool, inpu // so any following print messages will be in normal type. if tty { restoreOnce.Do(func() { - cli.restoreTerminal(inputStream) + restoreTerminal(streams, inputStream) }) } logrus.Debug("[hijack] End of stdin") @@ -93,3 +99,23 @@ func (cli *DockerCli) HoldHijackedConnection(ctx context.Context, tty bool, inpu return nil } + +func setRawTerminal(streams streams) error { + if err := streams.In().SetRawTerminal(); err != nil { + return err + } + return streams.Out().SetRawTerminal() +} + +func restoreTerminal(streams streams, in io.Closer) error { + streams.In().RestoreTerminal() + streams.Out().RestoreTerminal() + // WARNING: DO NOT REMOVE THE OS CHECK !!! + // For some reason this Close call blocks on darwin.. + // As the client exists right after, simply discard the close + // until we find a better solution. + if in != nil && runtime.GOOS != "darwin" { + return in.Close() + } + return nil +} diff --git a/api/client/container/run.go b/api/client/container/run.go index 5d44ff5ad5..af1a898234 100644 --- a/api/client/container/run.go +++ b/api/client/container/run.go @@ -203,7 +203,7 @@ func runRun(dockerCli *client.DockerCli, flags *pflag.FlagSet, opts *runOptions, defer resp.Close() errCh = promise.Go(func() error { - errHijack := dockerCli.HoldHijackedConnection(ctx, config.Tty, in, out, cerr, resp) + errHijack := holdHijackedConnection(ctx, dockerCli, config.Tty, in, out, cerr, resp) if errHijack == nil { return errAttach } diff --git a/api/client/container/start.go b/api/client/container/start.go index dbd4bca056..c3b4a3167b 100644 --- a/api/client/container/start.go +++ b/api/client/container/start.go @@ -95,7 +95,7 @@ func runStart(dockerCli *client.DockerCli, opts *startOptions) error { } defer resp.Close() cErr := promise.Go(func() error { - errHijack := dockerCli.HoldHijackedConnection(ctx, c.Config.Tty, in, dockerCli.Out(), dockerCli.Err(), resp) + errHijack := holdHijackedConnection(ctx, dockerCli, c.Config.Tty, in, dockerCli.Out(), dockerCli.Err(), resp) if errHijack == nil { return errAttach } diff --git a/api/client/container/tty.go b/api/client/container/tty.go index d87be0dcda..7af732750b 100644 --- a/api/client/container/tty.go +++ b/api/client/container/tty.go @@ -9,13 +9,13 @@ import ( "github.com/Sirupsen/logrus" "github.com/docker/docker/api/client" + "github.com/docker/docker/api/types" + apiclient "github.com/docker/docker/client" "github.com/docker/docker/pkg/signal" - apiclient "github.com/docker/engine-api/client" - "github.com/docker/engine-api/types" "golang.org/x/net/context" ) -// ResizeTtyTo resizes tty to specific height and width +// resizeTtyTo resizes tty to specific height and width func resizeTtyTo(ctx context.Context, client apiclient.ContainerAPIClient, id string, height, width int, isExec bool) { if height == 0 && width == 0 { return diff --git a/api/client/in.go b/api/client/in.go index acbac50c34..f0ce628dec 100644 --- a/api/client/in.go +++ b/api/client/in.go @@ -36,7 +36,8 @@ func (i *InStream) IsTerminal() bool { return i.isTerminal } -func (i *InStream) setRawTerminal() (err error) { +// SetRawTerminal sets raw mode on the input terminal +func (i *InStream) SetRawTerminal() (err error) { if os.Getenv("NORAW") != "" || !i.isTerminal { return nil } @@ -44,7 +45,8 @@ func (i *InStream) setRawTerminal() (err error) { return err } -func (i *InStream) restoreTerminal() { +// RestoreTerminal restores normal mode to the terminal +func (i *InStream) RestoreTerminal() { if i.state != nil { term.RestoreTerminal(i.fd, i.state) } diff --git a/api/client/out.go b/api/client/out.go index ac33d005f3..e8b38e4b3f 100644 --- a/api/client/out.go +++ b/api/client/out.go @@ -31,7 +31,8 @@ func (o *OutStream) IsTerminal() bool { return o.isTerminal } -func (o *OutStream) setRawTerminal() (err error) { +// SetRawTerminal sets raw mode on the output terminal +func (o *OutStream) SetRawTerminal() (err error) { if os.Getenv("NORAW") != "" || !o.isTerminal { return nil } @@ -39,7 +40,8 @@ func (o *OutStream) setRawTerminal() (err error) { return err } -func (o *OutStream) restoreTerminal() { +// RestoreTerminal restores normal mode to the terminal +func (o *OutStream) RestoreTerminal() { if o.state != nil { term.RestoreTerminal(o.fd, o.state) }