docker.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. package main
  2. import (
  3. "errors"
  4. "fmt"
  5. "os"
  6. "github.com/Sirupsen/logrus"
  7. "github.com/docker/docker/api/types/versions"
  8. "github.com/docker/docker/cli"
  9. "github.com/docker/docker/cli/command"
  10. "github.com/docker/docker/cli/command/commands"
  11. cliconfig "github.com/docker/docker/cli/config"
  12. "github.com/docker/docker/cli/debug"
  13. cliflags "github.com/docker/docker/cli/flags"
  14. "github.com/docker/docker/dockerversion"
  15. "github.com/docker/docker/pkg/term"
  16. "github.com/spf13/cobra"
  17. "github.com/spf13/pflag"
  18. )
  19. func newDockerCommand(dockerCli *command.DockerCli) *cobra.Command {
  20. opts := cliflags.NewClientOptions()
  21. var flags *pflag.FlagSet
  22. cmd := &cobra.Command{
  23. Use: "docker [OPTIONS] COMMAND [ARG...]",
  24. Short: "A self-sufficient runtime for containers",
  25. SilenceUsage: true,
  26. SilenceErrors: true,
  27. TraverseChildren: true,
  28. Args: noArgs,
  29. RunE: func(cmd *cobra.Command, args []string) error {
  30. if opts.Version {
  31. showVersion()
  32. return nil
  33. }
  34. return dockerCli.ShowHelp(cmd, args)
  35. },
  36. PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
  37. // daemon command is special, we redirect directly to another binary
  38. if cmd.Name() == "daemon" {
  39. return nil
  40. }
  41. // flags must be the top-level command flags, not cmd.Flags()
  42. opts.Common.SetDefaultOptions(flags)
  43. dockerPreRun(opts)
  44. if err := dockerCli.Initialize(opts); err != nil {
  45. return err
  46. }
  47. return isSupported(cmd, dockerCli.Client().ClientVersion(), dockerCli.HasExperimental())
  48. },
  49. }
  50. cli.SetupRootCommand(cmd)
  51. cmd.SetHelpFunc(func(ccmd *cobra.Command, args []string) {
  52. if dockerCli.Client() == nil { // when using --help, PersistenPreRun is not called, so initialization is needed.
  53. // flags must be the top-level command flags, not cmd.Flags()
  54. opts.Common.SetDefaultOptions(flags)
  55. dockerPreRun(opts)
  56. dockerCli.Initialize(opts)
  57. }
  58. if err := isSupported(ccmd, dockerCli.Client().ClientVersion(), dockerCli.HasExperimental()); err != nil {
  59. ccmd.Println(err)
  60. return
  61. }
  62. hideUnsupportedFeatures(ccmd, dockerCli.Client().ClientVersion(), dockerCli.HasExperimental())
  63. if err := ccmd.Help(); err != nil {
  64. ccmd.Println(err)
  65. }
  66. })
  67. flags = cmd.Flags()
  68. flags.BoolVarP(&opts.Version, "version", "v", false, "Print version information and quit")
  69. flags.StringVar(&opts.ConfigDir, "config", cliconfig.Dir(), "Location of client config files")
  70. opts.Common.InstallFlags(flags)
  71. cmd.SetOutput(dockerCli.Out())
  72. cmd.AddCommand(newDaemonCommand())
  73. commands.AddCommands(cmd, dockerCli)
  74. return cmd
  75. }
  76. func noArgs(cmd *cobra.Command, args []string) error {
  77. if len(args) == 0 {
  78. return nil
  79. }
  80. return fmt.Errorf(
  81. "docker: '%s' is not a docker command.\nSee 'docker --help'", args[0])
  82. }
  83. func main() {
  84. // Set terminal emulation based on platform as required.
  85. stdin, stdout, stderr := term.StdStreams()
  86. logrus.SetOutput(stderr)
  87. dockerCli := command.NewDockerCli(stdin, stdout, stderr)
  88. cmd := newDockerCommand(dockerCli)
  89. if err := cmd.Execute(); err != nil {
  90. if sterr, ok := err.(cli.StatusError); ok {
  91. if sterr.Status != "" {
  92. fmt.Fprintln(stderr, sterr.Status)
  93. }
  94. // StatusError should only be used for errors, and all errors should
  95. // have a non-zero exit status, so never exit with 0
  96. if sterr.StatusCode == 0 {
  97. os.Exit(1)
  98. }
  99. os.Exit(sterr.StatusCode)
  100. }
  101. fmt.Fprintln(stderr, err)
  102. os.Exit(1)
  103. }
  104. }
  105. func showVersion() {
  106. fmt.Printf("Docker version %s, build %s\n", dockerversion.Version, dockerversion.GitCommit)
  107. }
  108. func dockerPreRun(opts *cliflags.ClientOptions) {
  109. cliflags.SetLogLevel(opts.Common.LogLevel)
  110. if opts.ConfigDir != "" {
  111. cliconfig.SetDir(opts.ConfigDir)
  112. }
  113. if opts.Common.Debug {
  114. debug.Enable()
  115. }
  116. }
  117. func hideUnsupportedFeatures(cmd *cobra.Command, clientVersion string, hasExperimental bool) {
  118. cmd.Flags().VisitAll(func(f *pflag.Flag) {
  119. // hide experimental flags
  120. if !hasExperimental {
  121. if _, ok := f.Annotations["experimental"]; ok {
  122. f.Hidden = true
  123. }
  124. }
  125. // hide flags not supported by the server
  126. if flagVersion, ok := f.Annotations["version"]; ok && len(flagVersion) == 1 && versions.LessThan(clientVersion, flagVersion[0]) {
  127. f.Hidden = true
  128. }
  129. })
  130. for _, subcmd := range cmd.Commands() {
  131. // hide experimental subcommands
  132. if !hasExperimental {
  133. if _, ok := subcmd.Tags["experimental"]; ok {
  134. subcmd.Hidden = true
  135. }
  136. }
  137. // hide subcommands not supported by the server
  138. if subcmdVersion, ok := subcmd.Tags["version"]; ok && versions.LessThan(clientVersion, subcmdVersion) {
  139. subcmd.Hidden = true
  140. }
  141. }
  142. }
  143. func isSupported(cmd *cobra.Command, clientVersion string, hasExperimental bool) error {
  144. if !hasExperimental {
  145. if _, ok := cmd.Tags["experimental"]; ok {
  146. return errors.New("only supported with experimental daemon")
  147. }
  148. }
  149. if cmdVersion, ok := cmd.Tags["version"]; ok && versions.LessThan(clientVersion, cmdVersion) {
  150. return fmt.Errorf("only supported with daemon version >= %s", cmdVersion)
  151. }
  152. return nil
  153. }