Skip to content

Commit 9eb632d

Browse files
authored
Merge pull request #4387 from thaJeztah/single_cli_option
cli/command: merge DockerCliOption and InitializeOpt types
2 parents 7b3a60f + 7af509c commit 9eb632d

4 files changed

Lines changed: 29 additions & 22 deletions

File tree

cli-plugins/plugin/plugin.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ func RunPlugin(dockerCli *command.DockerCli, plugin *cobra.Command, meta manager
8383
cmd.SetContext(ctx)
8484
closeOnCLISocketClose(cancel)
8585

86-
var opts []command.InitializeOpt
86+
var opts []command.CLIOption
8787
if os.Getenv("DOCKER_CLI_PLUGIN_USE_DIAL_STDIO") != "" {
8888
opts = append(opts, withPluginClientConn(plugin.Name()))
8989
}
@@ -129,7 +129,7 @@ func Run(makeCmd func(command.Cli) *cobra.Command, meta manager.Metadata) {
129129
}
130130
}
131131

132-
func withPluginClientConn(name string) command.InitializeOpt {
132+
func withPluginClientConn(name string) command.CLIOption {
133133
return command.WithInitializeClient(func(dockerCli *command.DockerCli) (client.APIClient, error) {
134134
cmd := "docker"
135135
if x := os.Getenv(manager.ReexecEnvvar); x != "" {

cli/cobra.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ func (tcmd *TopLevelCommand) HandleGlobalFlags() (*cobra.Command, []string, erro
176176
}
177177

178178
// Initialize finalises global option parsing and initializes the docker client.
179-
func (tcmd *TopLevelCommand) Initialize(ops ...command.InitializeOpt) error {
179+
func (tcmd *TopLevelCommand) Initialize(ops ...command.CLIOption) error {
180180
tcmd.opts.SetDefaultOptions(tcmd.flags)
181181
return tcmd.dockerCli.Initialize(tcmd.opts, ops...)
182182
}

cli/command/cli.go

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ type Cli interface {
4949
Client() client.APIClient
5050
Streams
5151
SetIn(in *streams.In)
52-
Apply(ops ...DockerCliOption) error
52+
Apply(ops ...CLIOption) error
5353
ConfigFile() *configfile.ConfigFile
5454
ServerInfo() ServerInfo
5555
NotaryClient(imgRefAndAuth trust.ImageRefAndAuth, actions []string) (notaryclient.Repository, error)
@@ -194,11 +194,8 @@ func (cli *DockerCli) RegistryClient(allowInsecure bool) registryclient.Registry
194194
return registryclient.NewRegistryClient(resolver, UserAgent(), allowInsecure)
195195
}
196196

197-
// InitializeOpt is the type of the functional options passed to DockerCli.Initialize
198-
type InitializeOpt func(dockerCli *DockerCli) error
199-
200197
// WithInitializeClient is passed to DockerCli.Initialize by callers who wish to set a particular API Client for use by the CLI.
201-
func WithInitializeClient(makeClient func(dockerCli *DockerCli) (client.APIClient, error)) InitializeOpt {
198+
func WithInitializeClient(makeClient func(dockerCli *DockerCli) (client.APIClient, error)) CLIOption {
202199
return func(dockerCli *DockerCli) error {
203200
var err error
204201
dockerCli.client, err = makeClient(dockerCli)
@@ -208,7 +205,7 @@ func WithInitializeClient(makeClient func(dockerCli *DockerCli) (client.APIClien
208205

209206
// Initialize the dockerCli runs initialization that must happen after command
210207
// line flags are parsed.
211-
func (cli *DockerCli) Initialize(opts *cliflags.ClientOptions, ops ...InitializeOpt) error {
208+
func (cli *DockerCli) Initialize(opts *cliflags.ClientOptions, ops ...CLIOption) error {
212209
for _, o := range ops {
213210
if err := o(cli); err != nil {
214211
return err
@@ -450,7 +447,7 @@ func (cli *DockerCli) initialize() error {
450447
}
451448

452449
// Apply all the operation on the cli
453-
func (cli *DockerCli) Apply(ops ...DockerCliOption) error {
450+
func (cli *DockerCli) Apply(ops ...CLIOption) error {
454451
for _, op := range ops {
455452
if err := op(cli); err != nil {
456453
return err
@@ -479,8 +476,8 @@ type ServerInfo struct {
479476
// NewDockerCli returns a DockerCli instance with all operators applied on it.
480477
// It applies by default the standard streams, and the content trust from
481478
// environment.
482-
func NewDockerCli(ops ...DockerCliOption) (*DockerCli, error) {
483-
defaultOps := []DockerCliOption{
479+
func NewDockerCli(ops ...CLIOption) (*DockerCli, error) {
480+
defaultOps := []CLIOption{
484481
WithContentTrustFromEnv(),
485482
WithDefaultContextStoreConfig(),
486483
WithStandardStreams(),

cli/command/cli_options.go

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,21 @@ import (
1010
"github.com/moby/term"
1111
)
1212

13+
// CLIOption applies a modification on a DockerCli.
14+
type CLIOption func(cli *DockerCli) error
15+
1316
// DockerCliOption applies a modification on a DockerCli.
14-
type DockerCliOption func(cli *DockerCli) error
17+
//
18+
// Deprecated: use [CLIOption] instead.
19+
type DockerCliOption = CLIOption
20+
21+
// InitializeOpt is the type of the functional options passed to DockerCli.Initialize
22+
//
23+
// Deprecated: use [CLIOption] instead.
24+
type InitializeOpt = CLIOption
1525

1626
// WithStandardStreams sets a cli in, out and err streams with the standard streams.
17-
func WithStandardStreams() DockerCliOption {
27+
func WithStandardStreams() CLIOption {
1828
return func(cli *DockerCli) error {
1929
// Set terminal emulation based on platform as required.
2030
stdin, stdout, stderr := term.StdStreams()
@@ -26,7 +36,7 @@ func WithStandardStreams() DockerCliOption {
2636
}
2737

2838
// WithCombinedStreams uses the same stream for the output and error streams.
29-
func WithCombinedStreams(combined io.Writer) DockerCliOption {
39+
func WithCombinedStreams(combined io.Writer) CLIOption {
3040
return func(cli *DockerCli) error {
3141
cli.out = streams.NewOut(combined)
3242
cli.err = combined
@@ -35,31 +45,31 @@ func WithCombinedStreams(combined io.Writer) DockerCliOption {
3545
}
3646

3747
// WithInputStream sets a cli input stream.
38-
func WithInputStream(in io.ReadCloser) DockerCliOption {
48+
func WithInputStream(in io.ReadCloser) CLIOption {
3949
return func(cli *DockerCli) error {
4050
cli.in = streams.NewIn(in)
4151
return nil
4252
}
4353
}
4454

4555
// WithOutputStream sets a cli output stream.
46-
func WithOutputStream(out io.Writer) DockerCliOption {
56+
func WithOutputStream(out io.Writer) CLIOption {
4757
return func(cli *DockerCli) error {
4858
cli.out = streams.NewOut(out)
4959
return nil
5060
}
5161
}
5262

5363
// WithErrorStream sets a cli error stream.
54-
func WithErrorStream(err io.Writer) DockerCliOption {
64+
func WithErrorStream(err io.Writer) CLIOption {
5565
return func(cli *DockerCli) error {
5666
cli.err = err
5767
return nil
5868
}
5969
}
6070

6171
// WithContentTrustFromEnv enables content trust on a cli from environment variable DOCKER_CONTENT_TRUST value.
62-
func WithContentTrustFromEnv() DockerCliOption {
72+
func WithContentTrustFromEnv() CLIOption {
6373
return func(cli *DockerCli) error {
6474
cli.contentTrust = false
6575
if e := os.Getenv("DOCKER_CONTENT_TRUST"); e != "" {
@@ -73,23 +83,23 @@ func WithContentTrustFromEnv() DockerCliOption {
7383
}
7484

7585
// WithContentTrust enables content trust on a cli.
76-
func WithContentTrust(enabled bool) DockerCliOption {
86+
func WithContentTrust(enabled bool) CLIOption {
7787
return func(cli *DockerCli) error {
7888
cli.contentTrust = enabled
7989
return nil
8090
}
8191
}
8292

8393
// WithDefaultContextStoreConfig configures the cli to use the default context store configuration.
84-
func WithDefaultContextStoreConfig() DockerCliOption {
94+
func WithDefaultContextStoreConfig() CLIOption {
8595
return func(cli *DockerCli) error {
8696
cli.contextStoreConfig = DefaultContextStoreConfig()
8797
return nil
8898
}
8999
}
90100

91101
// WithAPIClient configures the cli to use the given API client.
92-
func WithAPIClient(c client.APIClient) DockerCliOption {
102+
func WithAPIClient(c client.APIClient) CLIOption {
93103
return func(cli *DockerCli) error {
94104
cli.client = c
95105
return nil

0 commit comments

Comments
 (0)