cli.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. package command
  2. import (
  3. "errors"
  4. "fmt"
  5. "io"
  6. "net/http"
  7. "os"
  8. "path/filepath"
  9. "runtime"
  10. "github.com/docker/docker/api"
  11. cliflags "github.com/docker/docker/cli/flags"
  12. "github.com/docker/docker/cliconfig"
  13. "github.com/docker/docker/cliconfig/configfile"
  14. "github.com/docker/docker/cliconfig/credentials"
  15. "github.com/docker/docker/client"
  16. "github.com/docker/docker/dockerversion"
  17. dopts "github.com/docker/docker/opts"
  18. "github.com/docker/go-connections/sockets"
  19. "github.com/docker/go-connections/tlsconfig"
  20. )
  21. // Streams is an interface which exposes the standard input and output streams
  22. type Streams interface {
  23. In() *InStream
  24. Out() *OutStream
  25. Err() io.Writer
  26. }
  27. // DockerCli represents the docker command line client.
  28. // Instances of the client can be returned from NewDockerCli.
  29. type DockerCli struct {
  30. configFile *configfile.ConfigFile
  31. in *InStream
  32. out *OutStream
  33. err io.Writer
  34. keyFile string
  35. client client.APIClient
  36. }
  37. // Client returns the APIClient
  38. func (cli *DockerCli) Client() client.APIClient {
  39. return cli.client
  40. }
  41. // Out returns the writer used for stdout
  42. func (cli *DockerCli) Out() *OutStream {
  43. return cli.out
  44. }
  45. // Err returns the writer used for stderr
  46. func (cli *DockerCli) Err() io.Writer {
  47. return cli.err
  48. }
  49. // In returns the reader used for stdin
  50. func (cli *DockerCli) In() *InStream {
  51. return cli.in
  52. }
  53. // ConfigFile returns the ConfigFile
  54. func (cli *DockerCli) ConfigFile() *configfile.ConfigFile {
  55. return cli.configFile
  56. }
  57. // Initialize the dockerCli runs initialization that must happen after command
  58. // line flags are parsed.
  59. func (cli *DockerCli) Initialize(opts *cliflags.ClientOptions) error {
  60. cli.configFile = LoadDefaultConfigFile(cli.err)
  61. var err error
  62. cli.client, err = NewAPIClientFromFlags(opts.Common, cli.configFile)
  63. if err != nil {
  64. return err
  65. }
  66. if opts.Common.TrustKey == "" {
  67. cli.keyFile = filepath.Join(cliconfig.ConfigDir(), cliflags.DefaultTrustKeyFile)
  68. } else {
  69. cli.keyFile = opts.Common.TrustKey
  70. }
  71. return nil
  72. }
  73. // NewDockerCli returns a DockerCli instance with IO output and error streams set by in, out and err.
  74. func NewDockerCli(in io.ReadCloser, out, err io.Writer) *DockerCli {
  75. return &DockerCli{in: NewInStream(in), out: NewOutStream(out), err: err}
  76. }
  77. // LoadDefaultConfigFile attempts to load the default config file and returns
  78. // an initialized ConfigFile struct if none is found.
  79. func LoadDefaultConfigFile(err io.Writer) *configfile.ConfigFile {
  80. configFile, e := cliconfig.Load(cliconfig.ConfigDir())
  81. if e != nil {
  82. fmt.Fprintf(err, "WARNING: Error loading config file:%v\n", e)
  83. }
  84. if !configFile.ContainsAuth() {
  85. credentials.DetectDefaultStore(configFile)
  86. }
  87. return configFile
  88. }
  89. // NewAPIClientFromFlags creates a new APIClient from command line flags
  90. func NewAPIClientFromFlags(opts *cliflags.CommonOptions, configFile *configfile.ConfigFile) (client.APIClient, error) {
  91. host, err := getServerHost(opts.Hosts, opts.TLSOptions)
  92. if err != nil {
  93. return &client.Client{}, err
  94. }
  95. customHeaders := configFile.HTTPHeaders
  96. if customHeaders == nil {
  97. customHeaders = map[string]string{}
  98. }
  99. customHeaders["User-Agent"] = UserAgent()
  100. verStr := api.DefaultVersion
  101. if tmpStr := os.Getenv("DOCKER_API_VERSION"); tmpStr != "" {
  102. verStr = tmpStr
  103. }
  104. httpClient, err := newHTTPClient(host, opts.TLSOptions)
  105. if err != nil {
  106. return &client.Client{}, err
  107. }
  108. return client.NewClient(host, verStr, httpClient, customHeaders)
  109. }
  110. func getServerHost(hosts []string, tlsOptions *tlsconfig.Options) (host string, err error) {
  111. switch len(hosts) {
  112. case 0:
  113. host = os.Getenv("DOCKER_HOST")
  114. case 1:
  115. host = hosts[0]
  116. default:
  117. return "", errors.New("Please specify only one -H")
  118. }
  119. host, err = dopts.ParseHost(tlsOptions != nil, host)
  120. return
  121. }
  122. func newHTTPClient(host string, tlsOptions *tlsconfig.Options) (*http.Client, error) {
  123. if tlsOptions == nil {
  124. // let the api client configure the default transport.
  125. return nil, nil
  126. }
  127. config, err := tlsconfig.Client(*tlsOptions)
  128. if err != nil {
  129. return nil, err
  130. }
  131. tr := &http.Transport{
  132. TLSClientConfig: config,
  133. }
  134. proto, addr, _, err := client.ParseHost(host)
  135. if err != nil {
  136. return nil, err
  137. }
  138. sockets.ConfigureTransport(tr, proto, addr)
  139. return &http.Client{
  140. Transport: tr,
  141. }, nil
  142. }
  143. // UserAgent returns the user agent string used for making API requests
  144. func UserAgent() string {
  145. return "Docker-Client/" + dockerversion.Version + " (" + runtime.GOOS + ")"
  146. }