cli.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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.Println("Error: Command not found:", args[0])
  73. return cli.CmdHelp()
  74. }
  75. return method(args[1:]...)
  76. }
  77. return cli.CmdHelp()
  78. }
  79. func (cli *DockerCli) Subcmd(name, signature, description string) *flag.FlagSet {
  80. flags := flag.NewFlagSet(name, flag.ContinueOnError)
  81. flags.Usage = func() {
  82. options := ""
  83. if flags.FlagCountUndeprecated() > 0 {
  84. options = "[OPTIONS] "
  85. }
  86. fmt.Fprintf(cli.err, "\nUsage: docker %s %s%s\n\n%s\n\n", name, options, signature, description)
  87. flags.PrintDefaults()
  88. os.Exit(2)
  89. }
  90. return flags
  91. }
  92. func (cli *DockerCli) LoadConfigFile() (err error) {
  93. cli.configFile, err = registry.LoadConfig(os.Getenv("HOME"))
  94. if err != nil {
  95. fmt.Fprintf(cli.err, "WARNING: %s\n", err)
  96. }
  97. return err
  98. }
  99. func (cli *DockerCli) CheckTtyInput(attachStdin, ttyMode bool) error {
  100. // In order to attach to a container tty, input stream for the client must
  101. // be a tty itself: redirecting or piping the client standard input is
  102. // incompatible with `docker run -t`, `docker exec -t` or `docker attach`.
  103. if ttyMode && attachStdin && !cli.isTerminalIn {
  104. return errors.New("cannot enable tty mode on non tty input")
  105. }
  106. return nil
  107. }
  108. func NewDockerCli(in io.ReadCloser, out, err io.Writer, key libtrust.PrivateKey, proto, addr string, tlsConfig *tls.Config) *DockerCli {
  109. var (
  110. inFd uintptr
  111. outFd uintptr
  112. isTerminalIn = false
  113. isTerminalOut = false
  114. scheme = "http"
  115. )
  116. if tlsConfig != nil {
  117. scheme = "https"
  118. }
  119. if in != nil {
  120. if file, ok := in.(*os.File); ok {
  121. inFd = file.Fd()
  122. isTerminalIn = term.IsTerminal(inFd)
  123. }
  124. }
  125. if out != nil {
  126. if file, ok := out.(*os.File); ok {
  127. outFd = file.Fd()
  128. isTerminalOut = term.IsTerminal(outFd)
  129. }
  130. }
  131. if err == nil {
  132. err = out
  133. }
  134. // The transport is created here for reuse during the client session
  135. tr := &http.Transport{
  136. TLSClientConfig: tlsConfig,
  137. }
  138. // Why 32? See issue 8035
  139. timeout := 32 * time.Second
  140. if proto == "unix" {
  141. // no need in compressing for local communications
  142. tr.DisableCompression = true
  143. tr.Dial = func(_, _ string) (net.Conn, error) {
  144. return net.DialTimeout(proto, addr, timeout)
  145. }
  146. } else {
  147. tr.Dial = (&net.Dialer{Timeout: timeout}).Dial
  148. }
  149. return &DockerCli{
  150. proto: proto,
  151. addr: addr,
  152. in: in,
  153. out: out,
  154. err: err,
  155. key: key,
  156. inFd: inFd,
  157. outFd: outFd,
  158. isTerminalIn: isTerminalIn,
  159. isTerminalOut: isTerminalOut,
  160. tlsConfig: tlsConfig,
  161. scheme: scheme,
  162. transport: tr,
  163. }
  164. }