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