cli.go 4.1 KB

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