cli.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. package client
  2. import (
  3. "crypto/tls"
  4. "encoding/json"
  5. "errors"
  6. "fmt"
  7. "io"
  8. "net"
  9. "net/http"
  10. "os"
  11. "reflect"
  12. "strings"
  13. "text/template"
  14. "time"
  15. flag "github.com/docker/docker/pkg/mflag"
  16. "github.com/docker/docker/pkg/term"
  17. "github.com/docker/docker/registry"
  18. "github.com/docker/libtrust"
  19. )
  20. type DockerCli struct {
  21. proto string
  22. addr string
  23. configFile *registry.ConfigFile
  24. in io.ReadCloser
  25. out io.Writer
  26. err io.Writer
  27. key libtrust.PrivateKey
  28. tlsConfig *tls.Config
  29. scheme string
  30. // inFd holds file descriptor of the client's STDIN, if it's a valid file
  31. inFd uintptr
  32. // outFd holds file descriptor of the client's STDOUT, if it's a valid file
  33. outFd uintptr
  34. // isTerminalIn describes if client's STDIN is a TTY
  35. isTerminalIn bool
  36. // isTerminalOut describes if client's STDOUT is a TTY
  37. isTerminalOut bool
  38. transport *http.Transport
  39. }
  40. var funcMap = template.FuncMap{
  41. "json": func(v interface{}) string {
  42. a, _ := json.Marshal(v)
  43. return string(a)
  44. },
  45. }
  46. func (cli *DockerCli) getMethod(args ...string) (func(...string) error, bool) {
  47. camelArgs := make([]string, len(args))
  48. for i, s := range args {
  49. if len(s) == 0 {
  50. return nil, false
  51. }
  52. camelArgs[i] = strings.ToUpper(s[:1]) + strings.ToLower(s[1:])
  53. }
  54. methodName := "Cmd" + strings.Join(camelArgs, "")
  55. method := reflect.ValueOf(cli).MethodByName(methodName)
  56. if !method.IsValid() {
  57. return nil, false
  58. }
  59. return method.Interface().(func(...string) error), true
  60. }
  61. // Cmd executes the specified command
  62. func (cli *DockerCli) Cmd(args ...string) error {
  63. if len(args) > 1 {
  64. method, exists := cli.getMethod(args[:2]...)
  65. if exists {
  66. return method(args[2:]...)
  67. }
  68. }
  69. if len(args) > 0 {
  70. method, exists := cli.getMethod(args[0])
  71. if !exists {
  72. fmt.Fprintf(cli.err, "docker: '%s' is not a docker command. See 'docker --help'.\n", args[0])
  73. os.Exit(1)
  74. }
  75. return method(args[1:]...)
  76. }
  77. return cli.CmdHelp()
  78. }
  79. func (cli *DockerCli) Subcmd(name, signature, description string, exitOnError bool) *flag.FlagSet {
  80. var errorHandling flag.ErrorHandling
  81. if exitOnError {
  82. errorHandling = flag.ExitOnError
  83. } else {
  84. errorHandling = flag.ContinueOnError
  85. }
  86. flags := flag.NewFlagSet(name, errorHandling)
  87. flags.Usage = func() {
  88. options := ""
  89. if flags.FlagCountUndeprecated() > 0 {
  90. options = "[OPTIONS] "
  91. }
  92. fmt.Fprintf(cli.out, "\nUsage: docker %s %s%s\n\n%s\n\n", name, options, signature, description)
  93. flags.SetOutput(cli.out)
  94. flags.PrintDefaults()
  95. os.Exit(0)
  96. }
  97. return flags
  98. }
  99. func (cli *DockerCli) LoadConfigFile() (err error) {
  100. cli.configFile, err = registry.LoadConfig(os.Getenv("HOME"))
  101. if err != nil {
  102. fmt.Fprintf(cli.err, "WARNING: %s\n", err)
  103. }
  104. return err
  105. }
  106. func (cli *DockerCli) CheckTtyInput(attachStdin, ttyMode bool) error {
  107. // In order to attach to a container tty, input stream for the client must
  108. // be a tty itself: redirecting or piping the client standard input is
  109. // incompatible with `docker run -t`, `docker exec -t` or `docker attach`.
  110. if ttyMode && attachStdin && !cli.isTerminalIn {
  111. return errors.New("cannot enable tty mode on non tty input")
  112. }
  113. return nil
  114. }
  115. func NewDockerCli(in io.ReadCloser, out, err io.Writer, key libtrust.PrivateKey, proto, addr string, tlsConfig *tls.Config) *DockerCli {
  116. var (
  117. inFd uintptr
  118. outFd uintptr
  119. isTerminalIn = false
  120. isTerminalOut = false
  121. scheme = "http"
  122. )
  123. if tlsConfig != nil {
  124. scheme = "https"
  125. }
  126. if in != nil {
  127. if file, ok := in.(*os.File); ok {
  128. inFd = file.Fd()
  129. isTerminalIn = term.IsTerminal(inFd)
  130. }
  131. }
  132. if out != nil {
  133. if file, ok := out.(*os.File); ok {
  134. outFd = file.Fd()
  135. isTerminalOut = term.IsTerminal(outFd)
  136. }
  137. }
  138. if err == nil {
  139. err = out
  140. }
  141. // The transport is created here for reuse during the client session
  142. tr := &http.Transport{
  143. Proxy: http.ProxyFromEnvironment,
  144. TLSClientConfig: tlsConfig,
  145. }
  146. // Why 32? See issue 8035
  147. timeout := 32 * time.Second
  148. if proto == "unix" {
  149. // no need in compressing for local communications
  150. tr.DisableCompression = true
  151. tr.Dial = func(_, _ string) (net.Conn, error) {
  152. return net.DialTimeout(proto, addr, timeout)
  153. }
  154. } else {
  155. tr.Dial = (&net.Dialer{Timeout: timeout}).Dial
  156. }
  157. return &DockerCli{
  158. proto: proto,
  159. addr: addr,
  160. in: in,
  161. out: out,
  162. err: err,
  163. key: key,
  164. inFd: inFd,
  165. outFd: outFd,
  166. isTerminalIn: isTerminalIn,
  167. isTerminalOut: isTerminalOut,
  168. tlsConfig: tlsConfig,
  169. scheme: scheme,
  170. transport: tr,
  171. }
  172. }