2019-01-28 08:52:58 -05:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
2023-09-09 18:27:44 -04:00
|
|
|
"context"
|
2019-01-28 08:52:58 -05:00
|
|
|
"io"
|
|
|
|
"os"
|
|
|
|
"strconv"
|
|
|
|
|
|
|
|
"github.com/docker/cli/cli/streams"
|
2022-12-05 15:35:05 -05:00
|
|
|
"github.com/docker/docker/client"
|
2020-04-16 05:23:37 -04:00
|
|
|
"github.com/moby/term"
|
2019-01-28 08:52:58 -05:00
|
|
|
)
|
|
|
|
|
2023-06-20 18:36:25 -04:00
|
|
|
// CLIOption applies a modification on a DockerCli.
|
|
|
|
type CLIOption func(cli *DockerCli) error
|
|
|
|
|
2019-01-28 08:52:58 -05:00
|
|
|
// DockerCliOption applies a modification on a DockerCli.
|
2023-06-20 18:36:25 -04:00
|
|
|
//
|
|
|
|
// Deprecated: use [CLIOption] instead.
|
|
|
|
type DockerCliOption = CLIOption
|
|
|
|
|
|
|
|
// InitializeOpt is the type of the functional options passed to DockerCli.Initialize
|
|
|
|
//
|
|
|
|
// Deprecated: use [CLIOption] instead.
|
|
|
|
type InitializeOpt = CLIOption
|
2019-01-28 08:52:58 -05:00
|
|
|
|
|
|
|
// WithStandardStreams sets a cli in, out and err streams with the standard streams.
|
2023-06-20 18:36:25 -04:00
|
|
|
func WithStandardStreams() CLIOption {
|
2019-01-28 08:52:58 -05:00
|
|
|
return func(cli *DockerCli) error {
|
|
|
|
// Set terminal emulation based on platform as required.
|
|
|
|
stdin, stdout, stderr := term.StdStreams()
|
|
|
|
cli.in = streams.NewIn(stdin)
|
|
|
|
cli.out = streams.NewOut(stdout)
|
|
|
|
cli.err = stderr
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-09 18:27:44 -04:00
|
|
|
// WithBaseContext sets the base context of a cli. It is used to propagate
|
|
|
|
// the context from the command line to the client.
|
|
|
|
func WithBaseContext(ctx context.Context) CLIOption {
|
|
|
|
return func(cli *DockerCli) error {
|
|
|
|
cli.baseCtx = ctx
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-28 08:52:58 -05:00
|
|
|
// WithCombinedStreams uses the same stream for the output and error streams.
|
2023-06-20 18:36:25 -04:00
|
|
|
func WithCombinedStreams(combined io.Writer) CLIOption {
|
2019-01-28 08:52:58 -05:00
|
|
|
return func(cli *DockerCli) error {
|
|
|
|
cli.out = streams.NewOut(combined)
|
|
|
|
cli.err = combined
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// WithInputStream sets a cli input stream.
|
2023-06-20 18:36:25 -04:00
|
|
|
func WithInputStream(in io.ReadCloser) CLIOption {
|
2019-01-28 08:52:58 -05:00
|
|
|
return func(cli *DockerCli) error {
|
|
|
|
cli.in = streams.NewIn(in)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// WithOutputStream sets a cli output stream.
|
2023-06-20 18:36:25 -04:00
|
|
|
func WithOutputStream(out io.Writer) CLIOption {
|
2019-01-28 08:52:58 -05:00
|
|
|
return func(cli *DockerCli) error {
|
|
|
|
cli.out = streams.NewOut(out)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// WithErrorStream sets a cli error stream.
|
2023-06-20 18:36:25 -04:00
|
|
|
func WithErrorStream(err io.Writer) CLIOption {
|
2019-01-28 08:52:58 -05:00
|
|
|
return func(cli *DockerCli) error {
|
|
|
|
cli.err = err
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// WithContentTrustFromEnv enables content trust on a cli from environment variable DOCKER_CONTENT_TRUST value.
|
2023-06-20 18:36:25 -04:00
|
|
|
func WithContentTrustFromEnv() CLIOption {
|
2019-01-28 08:52:58 -05:00
|
|
|
return func(cli *DockerCli) error {
|
|
|
|
cli.contentTrust = false
|
|
|
|
if e := os.Getenv("DOCKER_CONTENT_TRUST"); e != "" {
|
|
|
|
if t, err := strconv.ParseBool(e); t || err != nil {
|
|
|
|
// treat any other value as true
|
|
|
|
cli.contentTrust = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// WithContentTrust enables content trust on a cli.
|
2023-06-20 18:36:25 -04:00
|
|
|
func WithContentTrust(enabled bool) CLIOption {
|
2019-01-28 08:52:58 -05:00
|
|
|
return func(cli *DockerCli) error {
|
|
|
|
cli.contentTrust = enabled
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-03 06:09:20 -05:00
|
|
|
// WithDefaultContextStoreConfig configures the cli to use the default context store configuration.
|
2023-06-20 18:36:25 -04:00
|
|
|
func WithDefaultContextStoreConfig() CLIOption {
|
2021-03-03 06:09:20 -05:00
|
|
|
return func(cli *DockerCli) error {
|
|
|
|
cli.contextStoreConfig = DefaultContextStoreConfig()
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
2022-12-05 15:35:05 -05:00
|
|
|
|
|
|
|
// WithAPIClient configures the cli to use the given API client.
|
2023-06-20 18:36:25 -04:00
|
|
|
func WithAPIClient(c client.APIClient) CLIOption {
|
2022-12-05 15:35:05 -05:00
|
|
|
return func(cli *DockerCli) error {
|
|
|
|
cli.client = c
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|