exec.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. /*
  2. Copyright 2020 Docker Compose CLI authors
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package mobycli
  14. import (
  15. "context"
  16. "fmt"
  17. "os"
  18. "os/exec"
  19. "os/signal"
  20. "runtime"
  21. "strings"
  22. "github.com/spf13/cobra"
  23. apicontext "github.com/docker/compose-cli/context"
  24. "github.com/docker/compose-cli/context/store"
  25. "github.com/docker/compose-cli/metrics"
  26. )
  27. var delegatedContextTypes = []string{store.DefaultContextType}
  28. // ComDockerCli name of the classic cli binary
  29. const ComDockerCli = "com.docker.cli"
  30. // ExecIfDefaultCtxType delegates to com.docker.cli if on moby context
  31. func ExecIfDefaultCtxType(ctx context.Context, root *cobra.Command) {
  32. currentContext := apicontext.CurrentContext(ctx)
  33. s := store.ContextStore(ctx)
  34. currentCtx, err := s.Get(currentContext)
  35. // Only run original docker command if the current context is not ours.
  36. if err != nil || mustDelegateToMoby(currentCtx.Type()) {
  37. Exec(root)
  38. }
  39. }
  40. func mustDelegateToMoby(ctxType string) bool {
  41. for _, ctype := range delegatedContextTypes {
  42. if ctxType == ctype {
  43. return true
  44. }
  45. }
  46. return false
  47. }
  48. // Exec delegates to com.docker.cli if on moby context
  49. func Exec(root *cobra.Command) {
  50. execBinary := ComDockerCli
  51. if runtime.GOOS == "windows" { // workaround for windows issue https://github.com/golang/go/issues/38736
  52. var err error
  53. execBinary, err = LookPath(ComDockerCli)
  54. if err != nil {
  55. fmt.Fprintln(os.Stderr, err)
  56. os.Exit(1)
  57. }
  58. }
  59. cmd := exec.Command(execBinary, os.Args[1:]...)
  60. cmd.Stdin = os.Stdin
  61. cmd.Stdout = os.Stdout
  62. cmd.Stderr = os.Stderr
  63. signals := make(chan os.Signal, 1)
  64. childExit := make(chan bool)
  65. signal.Notify(signals) // catch all signals
  66. go func() {
  67. for {
  68. select {
  69. case sig := <-signals:
  70. if cmd.Process == nil {
  71. continue // can happen if receiving signal before the process is actually started
  72. }
  73. // nolint errcheck
  74. cmd.Process.Signal(sig)
  75. case <-childExit:
  76. return
  77. }
  78. }
  79. }()
  80. err := cmd.Run()
  81. childExit <- true
  82. if err != nil {
  83. metrics.Track(store.DefaultContextType, os.Args[1:], metrics.FailureStatus)
  84. if exiterr, ok := err.(*exec.ExitError); ok {
  85. os.Exit(exiterr.ExitCode())
  86. }
  87. fmt.Fprintln(os.Stderr, err)
  88. os.Exit(1)
  89. }
  90. metrics.Track(store.DefaultContextType, os.Args[1:], metrics.SuccessStatus)
  91. os.Exit(0)
  92. }
  93. // IsDefaultContextCommand checks if the command exists in the classic cli (issues a shellout --help)
  94. func IsDefaultContextCommand(dockerCommand string) bool {
  95. cmd := exec.Command(ComDockerCli, dockerCommand, "--help")
  96. b, e := cmd.CombinedOutput()
  97. if e != nil {
  98. fmt.Println(e)
  99. }
  100. output := string(b)
  101. contains := strings.Contains(output, "Usage:\tdocker "+dockerCommand)
  102. return contains
  103. }
  104. // ExecSilent executes a command and do redirect output to stdOut, return output
  105. func ExecSilent(ctx context.Context, args ...string) ([]byte, error) {
  106. if len(args) == 0 {
  107. args = os.Args[1:]
  108. }
  109. cmd := exec.CommandContext(ctx, ComDockerCli, args...)
  110. return cmd.CombinedOutput()
  111. }