github.com/thajeztah/cli@v0.0.0-20240223162942-dc6bfac81a8b/cli/command/cli_options.go (about)

     1  package command
     2  
     3  import (
     4  	"context"
     5  	"io"
     6  	"os"
     7  	"strconv"
     8  
     9  	"github.com/docker/cli/cli/streams"
    10  	"github.com/docker/docker/client"
    11  	"github.com/moby/term"
    12  )
    13  
    14  // CLIOption is a functional argument to apply options to a [DockerCli]. These
    15  // options can be passed to [NewDockerCli] to initialize a new CLI, or
    16  // applied with [DockerCli.Initialize] or [DockerCli.Apply].
    17  type CLIOption func(cli *DockerCli) error
    18  
    19  // WithStandardStreams sets a cli in, out and err streams with the standard streams.
    20  func WithStandardStreams() CLIOption {
    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  // WithBaseContext sets the base context of a cli. It is used to propagate
    32  // the context from the command line to the client.
    33  func WithBaseContext(ctx context.Context) CLIOption {
    34  	return func(cli *DockerCli) error {
    35  		cli.baseCtx = ctx
    36  		return nil
    37  	}
    38  }
    39  
    40  // WithCombinedStreams uses the same stream for the output and error streams.
    41  func WithCombinedStreams(combined io.Writer) CLIOption {
    42  	return func(cli *DockerCli) error {
    43  		cli.out = streams.NewOut(combined)
    44  		cli.err = combined
    45  		return nil
    46  	}
    47  }
    48  
    49  // WithInputStream sets a cli input stream.
    50  func WithInputStream(in io.ReadCloser) CLIOption {
    51  	return func(cli *DockerCli) error {
    52  		cli.in = streams.NewIn(in)
    53  		return nil
    54  	}
    55  }
    56  
    57  // WithOutputStream sets a cli output stream.
    58  func WithOutputStream(out io.Writer) CLIOption {
    59  	return func(cli *DockerCli) error {
    60  		cli.out = streams.NewOut(out)
    61  		return nil
    62  	}
    63  }
    64  
    65  // WithErrorStream sets a cli error stream.
    66  func WithErrorStream(err io.Writer) CLIOption {
    67  	return func(cli *DockerCli) error {
    68  		cli.err = err
    69  		return nil
    70  	}
    71  }
    72  
    73  // WithContentTrustFromEnv enables content trust on a cli from environment variable DOCKER_CONTENT_TRUST value.
    74  func WithContentTrustFromEnv() CLIOption {
    75  	return func(cli *DockerCli) error {
    76  		cli.contentTrust = false
    77  		if e := os.Getenv("DOCKER_CONTENT_TRUST"); e != "" {
    78  			if t, err := strconv.ParseBool(e); t || err != nil {
    79  				// treat any other value as true
    80  				cli.contentTrust = true
    81  			}
    82  		}
    83  		return nil
    84  	}
    85  }
    86  
    87  // WithContentTrust enables content trust on a cli.
    88  func WithContentTrust(enabled bool) CLIOption {
    89  	return func(cli *DockerCli) error {
    90  		cli.contentTrust = enabled
    91  		return nil
    92  	}
    93  }
    94  
    95  // WithDefaultContextStoreConfig configures the cli to use the default context store configuration.
    96  func WithDefaultContextStoreConfig() CLIOption {
    97  	return func(cli *DockerCli) error {
    98  		cli.contextStoreConfig = DefaultContextStoreConfig()
    99  		return nil
   100  	}
   101  }
   102  
   103  // WithAPIClient configures the cli to use the given API client.
   104  func WithAPIClient(c client.APIClient) CLIOption {
   105  	return func(cli *DockerCli) error {
   106  		cli.client = c
   107  		return nil
   108  	}
   109  }