Skip to content

Commit

Permalink
Extract streams helpers from command package to their own package to …
Browse files Browse the repository at this point in the history
…remove a cyclic dependency from command to internal/containerizedengine

Aliasing old types
* streams.InStream -> streams.In
* streams.NewInStream -> streams.NewIn
* streams.OutStream -> streams.Out
* streams.NewOutStream -> streams.NewOut

Signed-off-by: Silvin Lubecki <silvin.lubecki@docker.com>
  • Loading branch information
silvin-lubecki committed Jan 28, 2019
1 parent 81e7426 commit eb0ba4f
Show file tree
Hide file tree
Showing 17 changed files with 136 additions and 108 deletions.
4 changes: 2 additions & 2 deletions cli/command/context/export-import_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"path/filepath"
"testing"

"github.com/docker/cli/cli/command"
"github.com/docker/cli/cli/streams"
"gotest.tools/assert"
)

Expand Down Expand Up @@ -53,7 +53,7 @@ func TestExportImportPipe(t *testing.T) {
dest: "-",
}))
assert.Equal(t, cli.ErrBuffer().String(), "")
cli.SetIn(command.NewInStream(ioutil.NopCloser(bytes.NewBuffer(cli.OutBuffer().Bytes()))))
cli.SetIn(streams.NewIn(ioutil.NopCloser(bytes.NewBuffer(cli.OutBuffer().Bytes()))))
cli.OutBuffer().Reset()
cli.ErrBuffer().Reset()
assert.NilError(t, runImport(cli, "test2", "-"))
Expand Down
4 changes: 2 additions & 2 deletions cli/command/image/build_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
"sort"
"testing"

"github.com/docker/cli/cli/command"
"github.com/docker/cli/cli/streams"
"github.com/docker/cli/internal/test"
"github.com/docker/docker/api/types"
"github.com/docker/docker/pkg/archive"
Expand All @@ -39,7 +39,7 @@ func TestRunBuildDockerfileFromStdinWithCompress(t *testing.T) {
FROM alpine:3.6
COPY foo /
`)
cli.SetIn(command.NewInStream(ioutil.NopCloser(dockerfile)))
cli.SetIn(streams.NewIn(ioutil.NopCloser(dockerfile)))

dir := fs.NewDir(t, t.Name(),
fs.WithFile("foo", "some content"))
Expand Down
3 changes: 2 additions & 1 deletion cli/command/image/trust.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"sort"

"github.com/docker/cli/cli/command"
"github.com/docker/cli/cli/streams"
"github.com/docker/cli/cli/trust"
"github.com/docker/distribution/reference"
"github.com/docker/docker/api/types"
Expand Down Expand Up @@ -296,7 +297,7 @@ func imagePullPrivileged(ctx context.Context, cli command.Cli, imgRefAndAuth tru

out := cli.Out()
if opts.quiet {
out = command.NewOutStream(ioutil.Discard)
out = streams.NewOut(ioutil.Discard)
}
return jsonmessage.DisplayJSONMessagesToStream(responseBody, out, nil)
}
Expand Down
50 changes: 0 additions & 50 deletions cli/command/out.go

This file was deleted.

3 changes: 2 additions & 1 deletion cli/command/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"strings"

"github.com/docker/cli/cli/debug"
"github.com/docker/cli/cli/streams"
"github.com/docker/distribution/reference"
"github.com/docker/docker/api/types"
registrytypes "github.com/docker/docker/api/types/registry"
Expand Down Expand Up @@ -101,7 +102,7 @@ func GetDefaultAuthConfig(cli Cli, checkCredStore bool, serverAddress string, is
func ConfigureAuth(cli Cli, flUser, flPassword string, authconfig *types.AuthConfig, isDefaultRegistry bool) error {
// On Windows, force the use of the regular OS stdin stream. Fixes #14336/#14210
if runtime.GOOS == "windows" {
cli.SetIn(NewInStream(os.Stdin))
cli.SetIn(streams.NewIn(os.Stdin))
}

// Some links documenting this:
Expand Down
8 changes: 4 additions & 4 deletions cli/command/stack/kubernetes/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import (
"fmt"
"io"

"github.com/docker/cli/cli/command"
"github.com/docker/cli/cli/command/stack/options"
composetypes "github.com/docker/cli/cli/compose/types"
"github.com/docker/cli/cli/streams"
"github.com/morikuni/aec"
corev1 "k8s.io/client-go/kubernetes/typed/core/v1"
)
Expand Down Expand Up @@ -117,7 +117,7 @@ func metaStateFromStatus(status serviceStatus) metaServiceState {
}

type forwardOnlyStatusDisplay struct {
o *command.OutStream
o *streams.Out
states map[string]metaServiceState
}

Expand All @@ -130,7 +130,7 @@ func (d *forwardOnlyStatusDisplay) OnStatus(status serviceStatus) {
}

type interactiveStatusDisplay struct {
o *command.OutStream
o *streams.Out
statuses []serviceStatus
}

Expand Down Expand Up @@ -163,7 +163,7 @@ func displayInteractiveServiceStatus(status serviceStatus, o io.Writer) {
status.podsReady, status.podsPending, totalFailed, status.podsTotal)
}

func newStatusDisplay(o *command.OutStream) statusDisplay {
func newStatusDisplay(o *streams.Out) statusDisplay {
if !o.IsTerminal() {
return &forwardOnlyStatusDisplay{o: o, states: map[string]metaServiceState{}}
}
Expand Down
23 changes: 23 additions & 0 deletions cli/command/streams.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package command

import (
"github.com/docker/cli/cli/streams"
)

// InStream is an input stream used by the DockerCli to read user input
// Deprecated: Use github.com/docker/cli/cli/streams.In instead
type InStream = streams.In

// OutStream is an output stream used by the DockerCli to write normal program
// output.
// Deprecated: Use github.com/docker/cli/cli/streams.Out instead
type OutStream = streams.Out

var (
// NewInStream returns a new InStream object from a ReadCloser
// Deprecated: Use github.com/docker/cli/cli/streams.NewIn instead
NewInStream = streams.NewIn
// NewOutStream returns a new OutStream object from a Writer
// Deprecated: Use github.com/docker/cli/cli/streams.NewOut instead
NewOutStream = streams.NewOut
)
3 changes: 2 additions & 1 deletion cli/command/swarm/unlock.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (

"github.com/docker/cli/cli"
"github.com/docker/cli/cli/command"
"github.com/docker/cli/cli/streams"
"github.com/docker/docker/api/types/swarm"
"github.com/pkg/errors"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -59,7 +60,7 @@ func runUnlock(dockerCli command.Cli) error {
return client.SwarmUnlock(ctx, req)
}

func readKey(in *command.InStream, prompt string) (string, error) {
func readKey(in *streams.In, prompt string) (string, error) {
if in.IsTerminal() {
fmt.Print(prompt)
dt, err := terminal.ReadPassword(int(in.FD()))
Expand Down
4 changes: 2 additions & 2 deletions cli/command/swarm/unlock_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"strings"
"testing"

"github.com/docker/cli/cli/command"
"github.com/docker/cli/cli/streams"
"github.com/docker/cli/internal/test"
"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/swarm"
Expand Down Expand Up @@ -92,7 +92,7 @@ func TestSwarmUnlock(t *testing.T) {
return nil
},
})
dockerCli.SetIn(command.NewInStream(ioutil.NopCloser(strings.NewReader(input))))
dockerCli.SetIn(streams.NewIn(ioutil.NopCloser(strings.NewReader(input))))
cmd := newUnlockCommand(dockerCli)
assert.NilError(t, cmd.Execute())
}
3 changes: 2 additions & 1 deletion cli/command/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"runtime"
"strings"

"github.com/docker/cli/cli/streams"
"github.com/docker/docker/api/types/filters"
"github.com/docker/docker/pkg/system"
"github.com/spf13/pflag"
Expand Down Expand Up @@ -80,7 +81,7 @@ func PromptForConfirmation(ins io.Reader, outs io.Writer, message string) bool {

// On Windows, force the use of the regular OS stdin stream.
if runtime.GOOS == "windows" {
ins = NewInStream(os.Stdin)
ins = streams.NewIn(os.Stdin)
}

reader := bufio.NewReader(ins)
Expand Down
6 changes: 3 additions & 3 deletions cli/command/volume/prune_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"strings"
"testing"

"github.com/docker/cli/cli/command"
"github.com/docker/cli/cli/streams"
"github.com/docker/cli/internal/test"
"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/filters"
Expand Down Expand Up @@ -86,7 +86,7 @@ func TestVolumePrunePromptYes(t *testing.T) {
volumePruneFunc: simplePruneFunc,
})

cli.SetIn(command.NewInStream(ioutil.NopCloser(strings.NewReader(input))))
cli.SetIn(streams.NewIn(ioutil.NopCloser(strings.NewReader(input))))
cmd := NewPruneCommand(cli)
assert.NilError(t, cmd.Execute())
golden.Assert(t, cli.OutBuffer().String(), "volume-prune-yes.golden")
Expand All @@ -102,7 +102,7 @@ func TestVolumePrunePromptNo(t *testing.T) {
volumePruneFunc: simplePruneFunc,
})

cli.SetIn(command.NewInStream(ioutil.NopCloser(strings.NewReader(input))))
cli.SetIn(streams.NewIn(ioutil.NopCloser(strings.NewReader(input))))
cmd := NewPruneCommand(cli)
assert.NilError(t, cmd.Execute())
golden.Assert(t, cli.OutBuffer().String(), "volume-prune-no.golden")
Expand Down
26 changes: 13 additions & 13 deletions cli/command/in.go → cli/streams/in.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package command
package streams

import (
"errors"
Expand All @@ -9,33 +9,33 @@ import (
"github.com/docker/docker/pkg/term"
)

// InStream is an input stream used by the DockerCli to read user input
type InStream struct {
CommonStream
// In is an input stream used by the DockerCli to read user input
type In struct {
commonStream
in io.ReadCloser
}

func (i *InStream) Read(p []byte) (int, error) {
func (i *In) Read(p []byte) (int, error) {
return i.in.Read(p)
}

// Close implements the Closer interface
func (i *InStream) Close() error {
func (i *In) Close() error {
return i.in.Close()
}

// SetRawTerminal sets raw mode on the input terminal
func (i *InStream) SetRawTerminal() (err error) {
if os.Getenv("NORAW") != "" || !i.CommonStream.isTerminal {
func (i *In) SetRawTerminal() (err error) {
if os.Getenv("NORAW") != "" || !i.commonStream.isTerminal {
return nil
}
i.CommonStream.state, err = term.SetRawTerminal(i.CommonStream.fd)
i.commonStream.state, err = term.SetRawTerminal(i.commonStream.fd)
return err
}

// CheckTty checks if we are trying to attach to a container tty
// from a non-tty client input stream, and if so, returns an error.
func (i *InStream) CheckTty(attachStdin, ttyMode bool) error {
func (i *In) CheckTty(attachStdin, ttyMode bool) error {
// In order to attach to a container tty, input stream for the client must
// be a tty itself: redirecting or piping the client standard input is
// incompatible with `docker run -t`, `docker exec -t` or `docker attach`.
Expand All @@ -49,8 +49,8 @@ func (i *InStream) CheckTty(attachStdin, ttyMode bool) error {
return nil
}

// NewInStream returns a new InStream object from a ReadCloser
func NewInStream(in io.ReadCloser) *InStream {
// NewIn returns a new In object from a ReadCloser
func NewIn(in io.ReadCloser) *In {
fd, isTerminal := term.GetFdInfo(in)
return &InStream{CommonStream: CommonStream{fd: fd, isTerminal: isTerminal}, in: in}
return &In{commonStream: commonStream{fd: fd, isTerminal: isTerminal}, in: in}
}
50 changes: 50 additions & 0 deletions cli/streams/out.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package streams

import (
"io"
"os"

"github.com/docker/docker/pkg/term"
"github.com/sirupsen/logrus"
)

// Out is an output stream used by the DockerCli to write normal program
// output.
type Out struct {
commonStream
out io.Writer
}

func (o *Out) Write(p []byte) (int, error) {
return o.out.Write(p)
}

// SetRawTerminal sets raw mode on the input terminal
func (o *Out) SetRawTerminal() (err error) {
if os.Getenv("NORAW") != "" || !o.commonStream.isTerminal {
return nil
}
o.commonStream.state, err = term.SetRawTerminalOutput(o.commonStream.fd)
return err
}

// GetTtySize returns the height and width in characters of the tty
func (o *Out) GetTtySize() (uint, uint) {
if !o.isTerminal {
return 0, 0
}
ws, err := term.GetWinsize(o.fd)
if err != nil {
logrus.Debugf("Error getting size: %s", err)
if ws == nil {
return 0, 0
}
}
return uint(ws.Height), uint(ws.Width)
}

// NewOut returns a new Out object from a Writer
func NewOut(out io.Writer) *Out {
fd, isTerminal := term.GetFdInfo(out)
return &Out{commonStream: commonStream{fd: fd, isTerminal: isTerminal}, out: out}
}
Loading

0 comments on commit eb0ba4f

Please sign in to comment.