cli.go 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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/cli/config/credentials"
  9. "github.com/docker/docker/client"
  10. )
  11. // FakeCli emulates the default DockerCli
  12. type FakeCli struct {
  13. command.DockerCli
  14. client client.APIClient
  15. configfile *configfile.ConfigFile
  16. out *command.OutStream
  17. err io.Writer
  18. in *command.InStream
  19. store credentials.Store
  20. }
  21. // NewFakeCli returns a Cli backed by the fakeCli
  22. func NewFakeCli(client client.APIClient, out io.Writer) *FakeCli {
  23. return &FakeCli{
  24. client: client,
  25. out: command.NewOutStream(out),
  26. err: ioutil.Discard,
  27. in: command.NewInStream(ioutil.NopCloser(strings.NewReader(""))),
  28. }
  29. }
  30. // SetIn sets the input of the cli to the specified ReadCloser
  31. func (c *FakeCli) SetIn(in *command.InStream) {
  32. c.in = in
  33. }
  34. // SetErr sets the stderr stream for the cli to the specified io.Writer
  35. func (c *FakeCli) SetErr(err io.Writer) {
  36. c.err = err
  37. }
  38. // SetConfigfile sets the "fake" config file
  39. func (c *FakeCli) SetConfigfile(configfile *configfile.ConfigFile) {
  40. c.configfile = configfile
  41. }
  42. // Client returns a docker API client
  43. func (c *FakeCli) Client() client.APIClient {
  44. return c.client
  45. }
  46. // Out returns the output stream (stdout) the cli should write on
  47. func (c *FakeCli) Out() *command.OutStream {
  48. return c.out
  49. }
  50. // Err returns the output stream (stderr) the cli should write on
  51. func (c *FakeCli) Err() io.Writer {
  52. return c.err
  53. }
  54. // In returns the input stream the cli will use
  55. func (c *FakeCli) In() *command.InStream {
  56. return c.in
  57. }
  58. // ConfigFile returns the cli configfile object (to get client configuration)
  59. func (c *FakeCli) ConfigFile() *configfile.ConfigFile {
  60. return c.configfile
  61. }
  62. // CredentialsStore returns the fake store the cli will use
  63. func (c *FakeCli) CredentialsStore(serverAddress string) credentials.Store {
  64. if c.store == nil {
  65. c.store = NewFakeStore()
  66. }
  67. return c.store
  68. }