cli.go 4.2 KB

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