cli.go 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. package test
  2. import (
  3. "io"
  4. "io/ioutil"
  5. "strings"
  6. "github.com/docker/docker/cli/command"
  7. "github.com/docker/docker/cli/config/configfile"
  8. "github.com/docker/docker/client"
  9. )
  10. // FakeCli emulates the default DockerCli
  11. type FakeCli struct {
  12. command.DockerCli
  13. client client.APIClient
  14. configfile *configfile.ConfigFile
  15. out io.Writer
  16. err io.Writer
  17. in io.ReadCloser
  18. }
  19. // NewFakeCli returns a Cli backed by the fakeCli
  20. func NewFakeCli(client client.APIClient, out io.Writer) *FakeCli {
  21. return &FakeCli{
  22. client: client,
  23. out: out,
  24. err: ioutil.Discard,
  25. in: ioutil.NopCloser(strings.NewReader("")),
  26. }
  27. }
  28. // SetIn sets the input of the cli to the specified ReadCloser
  29. func (c *FakeCli) SetIn(in io.ReadCloser) {
  30. c.in = in
  31. }
  32. // SetErr sets the stderr stream for the cli to the specified io.Writer
  33. func (c *FakeCli) SetErr(err io.Writer) {
  34. c.err = err
  35. }
  36. // SetConfigfile sets the "fake" config file
  37. func (c *FakeCli) SetConfigfile(configfile *configfile.ConfigFile) {
  38. c.configfile = configfile
  39. }
  40. // Client returns a docker API client
  41. func (c *FakeCli) Client() client.APIClient {
  42. return c.client
  43. }
  44. // Out returns the output stream (stdout) the cli should write on
  45. func (c *FakeCli) Out() *command.OutStream {
  46. return command.NewOutStream(c.out)
  47. }
  48. // Err returns the output stream (stderr) the cli should write on
  49. func (c *FakeCli) Err() io.Writer {
  50. return c.err
  51. }
  52. // In returns the input stream the cli will use
  53. func (c *FakeCli) In() *command.InStream {
  54. return command.NewInStream(c.in)
  55. }
  56. // ConfigFile returns the cli configfile object (to get client configuration)
  57. func (c *FakeCli) ConfigFile() *configfile.ConfigFile {
  58. return c.configfile
  59. }