main.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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. "os"
  28. "os/exec"
  29. "path/filepath"
  30. "sort"
  31. "github.com/docker/api/context"
  32. "github.com/sirupsen/logrus"
  33. "github.com/urfave/cli"
  34. )
  35. func init() {
  36. // initial hack to get the path of the project's bin dir
  37. // into the env of this cli for development
  38. path := filepath.Join(os.Getenv("GOPATH"), "src/github.com/docker/api/bin")
  39. if err := os.Setenv("PATH", fmt.Sprintf("$PATH:%s", path)); err != nil {
  40. panic(err)
  41. }
  42. }
  43. func main() {
  44. app := cli.NewApp()
  45. app.Name = "docker"
  46. app.Usage = "Docker for the 2020s"
  47. app.UseShortOptionHandling = true
  48. app.EnableBashCompletion = true
  49. app.Flags = []cli.Flag{
  50. cli.BoolFlag{
  51. Name: "debug",
  52. Usage: "enable debug output in the logs",
  53. },
  54. context.ConfigFlag,
  55. context.ContextFlag,
  56. }
  57. app.Before = func(clix *cli.Context) error {
  58. if clix.GlobalBool("debug") {
  59. logrus.SetLevel(logrus.DebugLevel)
  60. }
  61. ctx, err := context.GetContext()
  62. if err != nil {
  63. logrus.Fatal(err)
  64. }
  65. if ctx.Metadata.Type == "Moby" {
  66. shellOutToDefaultEngine()
  67. os.Exit(0)
  68. }
  69. // TODO select backend based on context.Metadata.Type
  70. return nil
  71. }
  72. app.Commands = []cli.Command{
  73. contextCommand,
  74. exampleCommand,
  75. }
  76. sort.Sort(cli.FlagsByName(app.Flags))
  77. sort.Sort(cli.CommandsByName(app.Commands))
  78. if err := app.Run(os.Args); err != nil {
  79. fmt.Fprintln(os.Stderr, err)
  80. os.Exit(1)
  81. }
  82. }
  83. func shellOutToDefaultEngine() {
  84. cmd :=exec.Command("/Applications/Docker.app/Contents/Resources/bin/docker", os.Args[1:]...)
  85. cmd.Stdin = os.Stdin
  86. cmd.Stdout = os.Stdout
  87. cmd.Stderr = os.Stderr
  88. if err := cmd.Run(); err != nil {
  89. logrus.Fatal(err)
  90. }
  91. }