docker.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. package main
  2. import (
  3. "fmt"
  4. "os"
  5. "path/filepath"
  6. "github.com/Sirupsen/logrus"
  7. "github.com/docker/docker/api/client"
  8. "github.com/docker/docker/cli"
  9. "github.com/docker/docker/cli/cobraadaptor"
  10. cliflags "github.com/docker/docker/cli/flags"
  11. "github.com/docker/docker/cliconfig"
  12. "github.com/docker/docker/dockerversion"
  13. flag "github.com/docker/docker/pkg/mflag"
  14. "github.com/docker/docker/pkg/term"
  15. "github.com/docker/docker/utils"
  16. )
  17. var (
  18. commonFlags = cliflags.InitCommonFlags()
  19. clientFlags = initClientFlags(commonFlags)
  20. flHelp = flag.Bool([]string{"h", "-help"}, false, "Print usage")
  21. flVersion = flag.Bool([]string{"v", "-version"}, false, "Print version information and quit")
  22. )
  23. func main() {
  24. // Set terminal emulation based on platform as required.
  25. stdin, stdout, stderr := term.StdStreams()
  26. logrus.SetOutput(stderr)
  27. flag.Merge(flag.CommandLine, clientFlags.FlagSet, commonFlags.FlagSet)
  28. cobraAdaptor := cobraadaptor.NewCobraAdaptor(clientFlags)
  29. flag.Usage = func() {
  30. fmt.Fprint(stdout, "Usage: docker [OPTIONS] COMMAND [arg...]\n docker [ --help | -v | --version ]\n\n")
  31. fmt.Fprint(stdout, "A self-sufficient runtime for containers.\n\nOptions:\n")
  32. flag.CommandLine.SetOutput(stdout)
  33. flag.PrintDefaults()
  34. help := "\nCommands:\n"
  35. dockerCommands := append(cli.DockerCommandUsage, cobraAdaptor.Usage()...)
  36. for _, cmd := range sortCommands(dockerCommands) {
  37. help += fmt.Sprintf(" %-10.10s%s\n", cmd.Name, cmd.Description)
  38. }
  39. help += "\nRun 'docker COMMAND --help' for more information on a command."
  40. fmt.Fprintf(stdout, "%s\n", help)
  41. }
  42. flag.Parse()
  43. if *flVersion {
  44. showVersion()
  45. return
  46. }
  47. if *flHelp {
  48. // if global flag --help is present, regardless of what other options and commands there are,
  49. // just print the usage.
  50. flag.Usage()
  51. return
  52. }
  53. clientCli := client.NewDockerCli(stdin, stdout, stderr, clientFlags)
  54. c := cli.New(clientCli, NewDaemonProxy(), cobraAdaptor)
  55. if err := c.Run(flag.Args()...); err != nil {
  56. if sterr, ok := err.(cli.StatusError); ok {
  57. if sterr.Status != "" {
  58. fmt.Fprintln(stderr, sterr.Status)
  59. os.Exit(1)
  60. }
  61. os.Exit(sterr.StatusCode)
  62. }
  63. fmt.Fprintln(stderr, err)
  64. os.Exit(1)
  65. }
  66. }
  67. func showVersion() {
  68. if utils.ExperimentalBuild() {
  69. fmt.Printf("Docker version %s, build %s, experimental\n", dockerversion.Version, dockerversion.GitCommit)
  70. } else {
  71. fmt.Printf("Docker version %s, build %s\n", dockerversion.Version, dockerversion.GitCommit)
  72. }
  73. }
  74. func initClientFlags(commonFlags *cliflags.CommonFlags) *cliflags.ClientFlags {
  75. clientFlags := &cliflags.ClientFlags{FlagSet: new(flag.FlagSet), Common: commonFlags}
  76. client := clientFlags.FlagSet
  77. client.StringVar(&clientFlags.ConfigDir, []string{"-config"}, cliconfig.ConfigDir(), "Location of client config files")
  78. clientFlags.PostParse = func() {
  79. clientFlags.Common.PostParse()
  80. if clientFlags.ConfigDir != "" {
  81. cliconfig.SetConfigDir(clientFlags.ConfigDir)
  82. }
  83. if clientFlags.Common.TrustKey == "" {
  84. clientFlags.Common.TrustKey = filepath.Join(cliconfig.ConfigDir(), cliflags.DefaultTrustKeyFile)
  85. }
  86. if clientFlags.Common.Debug {
  87. utils.EnableDebug()
  88. }
  89. }
  90. return clientFlags
  91. }