main.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. /*
  2. Copyright (c) 2019 Docker Inc.
  3. Permission is hereby granted, free of charge, to any person
  4. obtaining a copy of this software and associated documentation
  5. files (the "Software"), to deal in the Software without
  6. restriction, including without limitation the rights to use, copy,
  7. modify, merge, publish, distribute, sublicense, and/or sell copies
  8. of the Software, and to permit persons to whom the Software is
  9. furnished to do so, subject to the following conditions:
  10. The above copyright notice and this permission notice shall be
  11. included in all copies or substantial portions of the Software.
  12. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  13. EXPRESS OR IMPLIED,
  14. INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  16. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  17. HOLDERS BE LIABLE FOR ANY CLAIM,
  18. DAMAGES OR OTHER LIABILITY,
  19. WHETHER IN AN ACTION OF CONTRACT,
  20. TORT OR OTHERWISE,
  21. ARISING FROM, OUT OF OR IN CONNECTION WITH
  22. THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  23. */
  24. package main
  25. import (
  26. "fmt"
  27. "io"
  28. "os"
  29. "os/exec"
  30. "path/filepath"
  31. "sort"
  32. "github.com/docker/api/context"
  33. "github.com/sirupsen/logrus"
  34. "github.com/urfave/cli"
  35. )
  36. func init() {
  37. // initial hack to get the path of the project's bin dir
  38. // into the env of this cli for development
  39. path := filepath.Join(os.Getenv("GOPATH"), "src/github.com/docker/api/bin")
  40. if err := os.Setenv("PATH", fmt.Sprintf("$PATH:%s", path)); err != nil {
  41. panic(err)
  42. }
  43. }
  44. func main() {
  45. app := cli.NewApp()
  46. app.Name = "docker"
  47. app.Usage = "Docker for the 2020s"
  48. app.UseShortOptionHandling = true
  49. app.EnableBashCompletion = true
  50. app.Flags = []cli.Flag{
  51. cli.BoolFlag{
  52. Name: "debug",
  53. Usage: "enable debug output in the logs",
  54. },
  55. context.ConfigFlag,
  56. context.ContextFlag,
  57. }
  58. // Make a copy of the default HelpPrinter function
  59. originalHelpPrinter := cli.HelpPrinter
  60. // Change the HelpPrinter function to shell out to the Moby CLI help
  61. // when the current context is pointing to Docker engine
  62. // else we use the copy of the original HelpPrinter
  63. cli.HelpPrinter = func(w io.Writer, templ string, data interface{}) {
  64. ctx, err := context.GetContext()
  65. if err != nil {
  66. logrus.Fatal(err)
  67. }
  68. if ctx.Metadata.Type == "Moby" {
  69. shellOutToDefaultEngine()
  70. } else {
  71. originalHelpPrinter(w, templ, data)
  72. }
  73. }
  74. app.Before = func(clix *cli.Context) error {
  75. if clix.GlobalBool("debug") {
  76. logrus.SetLevel(logrus.DebugLevel)
  77. }
  78. ctx, err := context.GetContext()
  79. if err != nil {
  80. logrus.Fatal(err)
  81. }
  82. if ctx.Metadata.Type == "Moby" {
  83. shellOutToDefaultEngine()
  84. }
  85. // TODO select backend based on context.Metadata.Type
  86. return nil
  87. }
  88. app.Commands = []cli.Command{
  89. contextCommand,
  90. exampleCommand,
  91. }
  92. sort.Sort(cli.FlagsByName(app.Flags))
  93. sort.Sort(cli.CommandsByName(app.Commands))
  94. if err := app.Run(os.Args); err != nil {
  95. fmt.Fprintln(os.Stderr, err)
  96. os.Exit(1)
  97. }
  98. }
  99. func shellOutToDefaultEngine() {
  100. cmd := exec.Command("/Applications/Docker.app/Contents/Resources/bin/docker", os.Args[1:]...)
  101. cmd.Stdin = os.Stdin
  102. cmd.Stdout = os.Stdout
  103. cmd.Stderr = os.Stderr
  104. if err := cmd.Run(); err != nil {
  105. if err != nil {
  106. if exiterr, ok := err.(*exec.ExitError); ok {
  107. os.Exit(exiterr.ExitCode())
  108. }
  109. os.Exit(1)
  110. }
  111. }
  112. os.Exit(0)
  113. }