cli.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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. "github.com/docker/docker/pkg/homedir"
  16. flag "github.com/docker/docker/pkg/mflag"
  17. "github.com/docker/docker/pkg/term"
  18. "github.com/docker/docker/registry"
  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. keyFile string
  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 signature != "" {
  90. signature = " " + signature
  91. }
  92. if flags.FlagCountUndeprecated() > 0 {
  93. options = " [OPTIONS]"
  94. }
  95. fmt.Fprintf(cli.out, "\nUsage: docker %s%s%s\n\n%s\n\n", name, options, signature, description)
  96. flags.SetOutput(cli.out)
  97. flags.PrintDefaults()
  98. os.Exit(0)
  99. }
  100. return flags
  101. }
  102. func (cli *DockerCli) LoadConfigFile() (err error) {
  103. cli.configFile, err = registry.LoadConfig(homedir.Get())
  104. if err != nil {
  105. fmt.Fprintf(cli.err, "WARNING: %s\n", err)
  106. }
  107. return err
  108. }
  109. func (cli *DockerCli) CheckTtyInput(attachStdin, ttyMode bool) error {
  110. // In order to attach to a container tty, input stream for the client must
  111. // be a tty itself: redirecting or piping the client standard input is
  112. // incompatible with `docker run -t`, `docker exec -t` or `docker attach`.
  113. if ttyMode && attachStdin && !cli.isTerminalIn {
  114. return errors.New("cannot enable tty mode on non tty input")
  115. }
  116. return nil
  117. }
  118. func NewDockerCli(in io.ReadCloser, out, err io.Writer, keyFile string, proto, addr string, tlsConfig *tls.Config) *DockerCli {
  119. var (
  120. inFd uintptr
  121. outFd uintptr
  122. isTerminalIn = false
  123. isTerminalOut = false
  124. scheme = "http"
  125. )
  126. if tlsConfig != nil {
  127. scheme = "https"
  128. }
  129. if in != nil {
  130. if file, ok := in.(*os.File); ok {
  131. inFd = file.Fd()
  132. isTerminalIn = term.IsTerminal(inFd)
  133. }
  134. }
  135. if out != nil {
  136. if file, ok := out.(*os.File); ok {
  137. outFd = file.Fd()
  138. isTerminalOut = term.IsTerminal(outFd)
  139. }
  140. }
  141. if err == nil {
  142. err = out
  143. }
  144. // The transport is created here for reuse during the client session
  145. tr := &http.Transport{
  146. TLSClientConfig: tlsConfig,
  147. }
  148. // Why 32? See issue 8035
  149. timeout := 32 * time.Second
  150. if proto == "unix" {
  151. // no need in compressing for local communications
  152. tr.DisableCompression = true
  153. tr.Dial = func(_, _ string) (net.Conn, error) {
  154. return net.DialTimeout(proto, addr, timeout)
  155. }
  156. } else {
  157. tr.Proxy = http.ProxyFromEnvironment
  158. tr.Dial = (&net.Dialer{Timeout: timeout}).Dial
  159. }
  160. return &DockerCli{
  161. proto: proto,
  162. addr: addr,
  163. in: in,
  164. out: out,
  165. err: err,
  166. keyFile: keyFile,
  167. inFd: inFd,
  168. outFd: outFd,
  169. isTerminalIn: isTerminalIn,
  170. isTerminalOut: isTerminalOut,
  171. tlsConfig: tlsConfig,
  172. scheme: scheme,
  173. transport: tr,
  174. }
  175. }