cli.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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. // CredentialsStore returns a new credentials store based
  58. // on the settings provided in the configuration file.
  59. func (cli *DockerCli) CredentialsStore() credentials.Store {
  60. if cli.configFile.CredentialsStore != "" {
  61. return credentials.NewNativeStore(cli.configFile)
  62. }
  63. return credentials.NewFileStore(cli.configFile)
  64. }
  65. // Initialize the dockerCli runs initialization that must happen after command
  66. // line flags are parsed.
  67. func (cli *DockerCli) Initialize(opts *cliflags.ClientOptions) error {
  68. cli.configFile = LoadDefaultConfigFile(cli.err)
  69. var err error
  70. cli.client, err = NewAPIClientFromFlags(opts.Common, cli.configFile)
  71. if err != nil {
  72. return err
  73. }
  74. if opts.Common.TrustKey == "" {
  75. cli.keyFile = filepath.Join(cliconfig.ConfigDir(), cliflags.DefaultTrustKeyFile)
  76. } else {
  77. cli.keyFile = opts.Common.TrustKey
  78. }
  79. return nil
  80. }
  81. // NewDockerCli returns a DockerCli instance with IO output and error streams set by in, out and err.
  82. func NewDockerCli(in io.ReadCloser, out, err io.Writer) *DockerCli {
  83. return &DockerCli{in: NewInStream(in), out: NewOutStream(out), err: err}
  84. }
  85. // LoadDefaultConfigFile attempts to load the default config file and returns
  86. // an initialized ConfigFile struct if none is found.
  87. func LoadDefaultConfigFile(err io.Writer) *configfile.ConfigFile {
  88. configFile, e := cliconfig.Load(cliconfig.ConfigDir())
  89. if e != nil {
  90. fmt.Fprintf(err, "WARNING: Error loading config file:%v\n", e)
  91. }
  92. if !configFile.ContainsAuth() {
  93. credentials.DetectDefaultStore(configFile)
  94. }
  95. return configFile
  96. }
  97. // NewAPIClientFromFlags creates a new APIClient from command line flags
  98. func NewAPIClientFromFlags(opts *cliflags.CommonOptions, configFile *configfile.ConfigFile) (client.APIClient, error) {
  99. host, err := getServerHost(opts.Hosts, opts.TLSOptions)
  100. if err != nil {
  101. return &client.Client{}, err
  102. }
  103. customHeaders := configFile.HTTPHeaders
  104. if customHeaders == nil {
  105. customHeaders = map[string]string{}
  106. }
  107. customHeaders["User-Agent"] = UserAgent()
  108. verStr := api.DefaultVersion
  109. if tmpStr := os.Getenv("DOCKER_API_VERSION"); tmpStr != "" {
  110. verStr = tmpStr
  111. }
  112. httpClient, err := newHTTPClient(host, opts.TLSOptions)
  113. if err != nil {
  114. return &client.Client{}, err
  115. }
  116. return client.NewClient(host, verStr, httpClient, customHeaders)
  117. }
  118. func getServerHost(hosts []string, tlsOptions *tlsconfig.Options) (host string, err error) {
  119. switch len(hosts) {
  120. case 0:
  121. host = os.Getenv("DOCKER_HOST")
  122. case 1:
  123. host = hosts[0]
  124. default:
  125. return "", errors.New("Please specify only one -H")
  126. }
  127. host, err = dopts.ParseHost(tlsOptions != nil, host)
  128. return
  129. }
  130. func newHTTPClient(host string, tlsOptions *tlsconfig.Options) (*http.Client, error) {
  131. if tlsOptions == nil {
  132. // let the api client configure the default transport.
  133. return nil, nil
  134. }
  135. config, err := tlsconfig.Client(*tlsOptions)
  136. if err != nil {
  137. return nil, err
  138. }
  139. tr := &http.Transport{
  140. TLSClientConfig: config,
  141. }
  142. proto, addr, _, err := client.ParseHost(host)
  143. if err != nil {
  144. return nil, err
  145. }
  146. sockets.ConfigureTransport(tr, proto, addr)
  147. return &http.Client{
  148. Transport: tr,
  149. }, nil
  150. }
  151. // UserAgent returns the user agent string used for making API requests
  152. func UserAgent() string {
  153. return "Docker-Client/" + dockerversion.Version + " (" + runtime.GOOS + ")"
  154. }