docker.go 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. package main
  2. import (
  3. "fmt"
  4. "os"
  5. "github.com/Sirupsen/logrus"
  6. "github.com/docker/docker/api/client"
  7. "github.com/docker/docker/autogen/dockerversion"
  8. "github.com/docker/docker/cli"
  9. flag "github.com/docker/docker/pkg/mflag"
  10. "github.com/docker/docker/pkg/reexec"
  11. "github.com/docker/docker/pkg/term"
  12. "github.com/docker/docker/utils"
  13. )
  14. func main() {
  15. if reexec.Init() {
  16. return
  17. }
  18. // Set terminal emulation based on platform as required.
  19. stdin, stdout, stderr := term.StdStreams()
  20. logrus.SetOutput(stderr)
  21. flag.Merge(flag.CommandLine, clientFlags.FlagSet, commonFlags.FlagSet)
  22. flag.Usage = func() {
  23. fmt.Fprint(os.Stdout, "Usage: docker [OPTIONS] COMMAND [arg...]\n"+daemonUsage+" docker [ -h | --help | -v | --version ]\n\n")
  24. fmt.Fprint(os.Stdout, "A self-sufficient runtime for containers.\n\nOptions:\n")
  25. flag.CommandLine.SetOutput(os.Stdout)
  26. flag.PrintDefaults()
  27. help := "\nCommands:\n"
  28. for _, cmd := range dockerCommands {
  29. help += fmt.Sprintf(" %-10.10s%s\n", cmd.name, cmd.description)
  30. }
  31. help += "\nRun 'docker COMMAND --help' for more information on a command."
  32. fmt.Fprintf(os.Stdout, "%s\n", help)
  33. }
  34. flag.Parse()
  35. if *flVersion {
  36. showVersion()
  37. return
  38. }
  39. clientCli := client.NewDockerCli(stdin, stdout, stderr, clientFlags)
  40. // TODO: remove once `-d` is retired
  41. handleGlobalDaemonFlag()
  42. if *flHelp {
  43. // if global flag --help is present, regardless of what other options and commands there are,
  44. // just print the usage.
  45. flag.Usage()
  46. return
  47. }
  48. c := cli.New(clientCli, daemonCli)
  49. if err := c.Run(flag.Args()...); err != nil {
  50. if sterr, ok := err.(cli.StatusError); ok {
  51. if sterr.Status != "" {
  52. fmt.Fprintln(os.Stderr, sterr.Status)
  53. os.Exit(1)
  54. }
  55. os.Exit(sterr.StatusCode)
  56. }
  57. fmt.Fprintln(os.Stderr, err)
  58. os.Exit(1)
  59. }
  60. }
  61. func showVersion() {
  62. if utils.ExperimentalBuild() {
  63. fmt.Printf("Docker version %s, build %s, experimental\n", dockerversion.VERSION, dockerversion.GITCOMMIT)
  64. } else {
  65. fmt.Printf("Docker version %s, build %s\n", dockerversion.VERSION, dockerversion.GITCOMMIT)
  66. }
  67. }