@@ -19,12 +19,15 @@ import (
1919 cliflags "github.com/docker/cli/cli/flags"
2020 manifeststore "github.com/docker/cli/cli/manifest/store"
2121 registryclient "github.com/docker/cli/cli/registry/client"
22+ "github.com/docker/cli/cli/streams"
2223 "github.com/docker/cli/cli/trust"
24+ "github.com/docker/cli/internal/containerizedengine"
2325 dopts "github.com/docker/cli/opts"
2426 clitypes "github.com/docker/cli/types"
2527 "github.com/docker/docker/api/types"
2628 registrytypes "github.com/docker/docker/api/types/registry"
2729 "github.com/docker/docker/client"
30+ "github.com/docker/docker/pkg/term"
2831 "github.com/docker/go-connections/tlsconfig"
2932 "github.com/pkg/errors"
3033 "github.com/spf13/cobra"
@@ -35,18 +38,19 @@ import (
3538
3639// Streams is an interface which exposes the standard input and output streams
3740type Streams interface {
38- In () * InStream
39- Out () * OutStream
41+ In () * streams. In
42+ Out () * streams. Out
4043 Err () io.Writer
4144}
4245
4346// Cli represents the docker command line client.
4447type Cli interface {
4548 Client () client.APIClient
46- Out () * OutStream
49+ Out () * streams. Out
4750 Err () io.Writer
48- In () * InStream
49- SetIn (in * InStream )
51+ In () * streams.In
52+ SetIn (in * streams.In )
53+ Apply (ops ... DockerCliOption ) error
5054 ConfigFile () * configfile.ConfigFile
5155 ServerInfo () ServerInfo
5256 ClientInfo () ClientInfo
@@ -66,8 +70,8 @@ type Cli interface {
6670// Instances of the client can be returned from NewDockerCli.
6771type DockerCli struct {
6872 configFile * configfile.ConfigFile
69- in * InStream
70- out * OutStream
73+ in * streams. In
74+ out * streams. Out
7175 err io.Writer
7276 client client.APIClient
7377 serverInfo ServerInfo
@@ -96,7 +100,7 @@ func (cli *DockerCli) Client() client.APIClient {
96100}
97101
98102// Out returns the writer used for stdout
99- func (cli * DockerCli ) Out () * OutStream {
103+ func (cli * DockerCli ) Out () * streams. Out {
100104 return cli .out
101105}
102106
@@ -106,12 +110,12 @@ func (cli *DockerCli) Err() io.Writer {
106110}
107111
108112// SetIn sets the reader used for stdin
109- func (cli * DockerCli ) SetIn (in * InStream ) {
113+ func (cli * DockerCli ) SetIn (in * streams. In ) {
110114 cli .in = in
111115}
112116
113117// In returns the reader used for stdin
114- func (cli * DockerCli ) In () * InStream {
118+ func (cli * DockerCli ) In () * streams. In {
115119 return cli .in
116120}
117121
@@ -393,6 +397,16 @@ func (cli *DockerCli) DockerEndpoint() docker.Endpoint {
393397 return cli .dockerEndpoint
394398}
395399
400+ // Apply all the operation on the cli
401+ func (cli * DockerCli ) Apply (ops ... DockerCliOption ) error {
402+ for _ , op := range ops {
403+ if err := op (cli ); err != nil {
404+ return err
405+ }
406+ }
407+ return nil
408+ }
409+
396410// ServerInfo stores details about the supported features and platform of the
397411// server
398412type ServerInfo struct {
@@ -407,9 +421,32 @@ type ClientInfo struct {
407421 DefaultVersion string
408422}
409423
410- // NewDockerCli returns a DockerCli instance with IO output and error streams set by in, out and err.
411- func NewDockerCli (in io.ReadCloser , out , err io.Writer , isTrusted bool , containerizedFn func (string ) (clitypes.ContainerizedClient , error )) * DockerCli {
412- return & DockerCli {in : NewInStream (in ), out : NewOutStream (out ), err : err , contentTrust : isTrusted , newContainerizeClient : containerizedFn }
424+ // NewDockerCli returns a DockerCli instance with all operators applied on it.
425+ // It applies by default the standard streams, the content trust from
426+ // environment and the default containerized client constructor operations.
427+ func NewDockerCli (ops ... DockerCliOption ) (* DockerCli , error ) {
428+ cli := & DockerCli {}
429+ defaultOps := []DockerCliOption {
430+ WithContentTrustFromEnv (),
431+ WithContainerizedClient (containerizedengine .NewClient ),
432+ }
433+ ops = append (defaultOps , ops ... )
434+ if err := cli .Apply (ops ... ); err != nil {
435+ return nil , err
436+ }
437+ if cli .out == nil || cli .in == nil || cli .err == nil {
438+ stdin , stdout , stderr := term .StdStreams ()
439+ if cli .in == nil {
440+ cli .in = streams .NewIn (stdin )
441+ }
442+ if cli .out == nil {
443+ cli .out = streams .NewOut (stdout )
444+ }
445+ if cli .err == nil {
446+ cli .err = stderr
447+ }
448+ }
449+ return cli , nil
413450}
414451
415452func getServerHost (hosts []string , tlsOptions * tlsconfig.Options ) (string , error ) {
0 commit comments