github.com/ali-iotechsys/cli@v20.10.0+incompatible/cli/command/cli_options.go (about)

     1  package command
     2  
     3  import (
     4  	"fmt"
     5  	"io"
     6  	"os"
     7  	"strconv"
     8  
     9  	"github.com/docker/cli/cli/context/docker"
    10  	"github.com/docker/cli/cli/context/store"
    11  	"github.com/docker/cli/cli/streams"
    12  	"github.com/moby/term"
    13  )
    14  
    15  // DockerCliOption applies a modification on a DockerCli.
    16  type DockerCliOption func(cli *DockerCli) error
    17  
    18  // WithStandardStreams sets a cli in, out and err streams with the standard streams.
    19  func WithStandardStreams() DockerCliOption {
    20  	return func(cli *DockerCli) error {
    21  		// Set terminal emulation based on platform as required.
    22  		stdin, stdout, stderr := term.StdStreams()
    23  		cli.in = streams.NewIn(stdin)
    24  		cli.out = streams.NewOut(stdout)
    25  		cli.err = stderr
    26  		return nil
    27  	}
    28  }
    29  
    30  // WithCombinedStreams uses the same stream for the output and error streams.
    31  func WithCombinedStreams(combined io.Writer) DockerCliOption {
    32  	return func(cli *DockerCli) error {
    33  		cli.out = streams.NewOut(combined)
    34  		cli.err = combined
    35  		return nil
    36  	}
    37  }
    38  
    39  // WithInputStream sets a cli input stream.
    40  func WithInputStream(in io.ReadCloser) DockerCliOption {
    41  	return func(cli *DockerCli) error {
    42  		cli.in = streams.NewIn(in)
    43  		return nil
    44  	}
    45  }
    46  
    47  // WithOutputStream sets a cli output stream.
    48  func WithOutputStream(out io.Writer) DockerCliOption {
    49  	return func(cli *DockerCli) error {
    50  		cli.out = streams.NewOut(out)
    51  		return nil
    52  	}
    53  }
    54  
    55  // WithErrorStream sets a cli error stream.
    56  func WithErrorStream(err io.Writer) DockerCliOption {
    57  	return func(cli *DockerCli) error {
    58  		cli.err = err
    59  		return nil
    60  	}
    61  }
    62  
    63  // WithContentTrustFromEnv enables content trust on a cli from environment variable DOCKER_CONTENT_TRUST value.
    64  func WithContentTrustFromEnv() DockerCliOption {
    65  	return func(cli *DockerCli) error {
    66  		cli.contentTrust = false
    67  		if e := os.Getenv("DOCKER_CONTENT_TRUST"); e != "" {
    68  			if t, err := strconv.ParseBool(e); t || err != nil {
    69  				// treat any other value as true
    70  				cli.contentTrust = true
    71  			}
    72  		}
    73  		return nil
    74  	}
    75  }
    76  
    77  // WithContentTrust enables content trust on a cli.
    78  func WithContentTrust(enabled bool) DockerCliOption {
    79  	return func(cli *DockerCli) error {
    80  		cli.contentTrust = enabled
    81  		return nil
    82  	}
    83  }
    84  
    85  // WithContextEndpointType add support for an additional typed endpoint in the context store
    86  // Plugins should use this to store additional endpoints configuration in the context store
    87  func WithContextEndpointType(endpointName string, endpointType store.TypeGetter) DockerCliOption {
    88  	return func(cli *DockerCli) error {
    89  		switch endpointName {
    90  		case docker.DockerEndpoint:
    91  			return fmt.Errorf("cannot change %q endpoint type", endpointName)
    92  		}
    93  		cli.contextStoreConfig.SetEndpoint(endpointName, endpointType)
    94  		return nil
    95  	}
    96  }