cli.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. package client
  2. import (
  3. "errors"
  4. "fmt"
  5. "io"
  6. "os"
  7. "runtime"
  8. "github.com/docker/docker/api"
  9. "github.com/docker/docker/api/client/lib"
  10. "github.com/docker/docker/cli"
  11. "github.com/docker/docker/cliconfig"
  12. "github.com/docker/docker/dockerversion"
  13. "github.com/docker/docker/opts"
  14. "github.com/docker/docker/pkg/term"
  15. "github.com/docker/docker/pkg/tlsconfig"
  16. )
  17. // DockerCli represents the docker command line client.
  18. // Instances of the client can be returned from NewDockerCli.
  19. type DockerCli struct {
  20. // initializing closure
  21. init func() error
  22. // configFile has the client configuration file
  23. configFile *cliconfig.ConfigFile
  24. // in holds the input stream and closer (io.ReadCloser) for the client.
  25. in io.ReadCloser
  26. // out holds the output stream (io.Writer) for the client.
  27. out io.Writer
  28. // err holds the error stream (io.Writer) for the client.
  29. err io.Writer
  30. // keyFile holds the key file as a string.
  31. keyFile string
  32. // inFd holds the file descriptor of the client's STDIN (if valid).
  33. inFd uintptr
  34. // outFd holds file descriptor of the client's STDOUT (if valid).
  35. outFd uintptr
  36. // isTerminalIn indicates whether the client's STDIN is a TTY
  37. isTerminalIn bool
  38. // isTerminalOut indicates whether the client's STDOUT is a TTY
  39. isTerminalOut bool
  40. // client is the http client that performs all API operations
  41. client apiClient
  42. }
  43. // Initialize calls the init function that will setup the configuration for the client
  44. // such as the TLS, tcp and other parameters used to run the client.
  45. func (cli *DockerCli) Initialize() error {
  46. if cli.init == nil {
  47. return nil
  48. }
  49. return cli.init()
  50. }
  51. // CheckTtyInput checks if we are trying to attach to a container tty
  52. // from a non-tty client input stream, and if so, returns an error.
  53. func (cli *DockerCli) CheckTtyInput(attachStdin, ttyMode bool) error {
  54. // In order to attach to a container tty, input stream for the client must
  55. // be a tty itself: redirecting or piping the client standard input is
  56. // incompatible with `docker run -t`, `docker exec -t` or `docker attach`.
  57. if ttyMode && attachStdin && !cli.isTerminalIn {
  58. return errors.New("cannot enable tty mode on non tty input")
  59. }
  60. return nil
  61. }
  62. // PsFormat returns the format string specified in the configuration.
  63. // String contains columns and format specification, for example {{ID}\t{{Name}}.
  64. func (cli *DockerCli) PsFormat() string {
  65. return cli.configFile.PsFormat
  66. }
  67. // NewDockerCli returns a DockerCli instance with IO output and error streams set by in, out and err.
  68. // The key file, protocol (i.e. unix) and address are passed in as strings, along with the tls.Config. If the tls.Config
  69. // is set the client scheme will be set to https.
  70. // The client will be given a 32-second timeout (see https://github.com/docker/docker/pull/8035).
  71. func NewDockerCli(in io.ReadCloser, out, err io.Writer, clientFlags *cli.ClientFlags) *DockerCli {
  72. cli := &DockerCli{
  73. in: in,
  74. out: out,
  75. err: err,
  76. keyFile: clientFlags.Common.TrustKey,
  77. }
  78. cli.init = func() error {
  79. clientFlags.PostParse()
  80. configFile, e := cliconfig.Load(cliconfig.ConfigDir())
  81. if e != nil {
  82. fmt.Fprintf(cli.err, "WARNING: Error loading config file:%v\n", e)
  83. }
  84. cli.configFile = configFile
  85. host, err := getServerHost(clientFlags.Common.Hosts, clientFlags.Common.TLSOptions)
  86. if err != nil {
  87. return err
  88. }
  89. customHeaders := cli.configFile.HTTPHeaders
  90. if customHeaders == nil {
  91. customHeaders = map[string]string{}
  92. }
  93. customHeaders["User-Agent"] = "Docker-Client/" + dockerversion.Version + " (" + runtime.GOOS + ")"
  94. client, err := lib.NewClient(host, string(api.Version), clientFlags.Common.TLSOptions, customHeaders)
  95. if err != nil {
  96. return err
  97. }
  98. cli.client = client
  99. if cli.in != nil {
  100. cli.inFd, cli.isTerminalIn = term.GetFdInfo(cli.in)
  101. }
  102. if cli.out != nil {
  103. cli.outFd, cli.isTerminalOut = term.GetFdInfo(cli.out)
  104. }
  105. return nil
  106. }
  107. return cli
  108. }
  109. func getServerHost(hosts []string, tlsOptions *tlsconfig.Options) (host string, err error) {
  110. switch len(hosts) {
  111. case 0:
  112. host = os.Getenv("DOCKER_HOST")
  113. case 1:
  114. host = hosts[0]
  115. default:
  116. return "", errors.New("Please specify only one -H")
  117. }
  118. defaultHost := opts.DefaultTCPHost
  119. if tlsOptions != nil {
  120. defaultHost = opts.DefaultTLSHost
  121. }
  122. host, err = opts.ParseHost(defaultHost, host)
  123. return
  124. }