exec.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. /*
  2. Copyright 2020 Docker, Inc.
  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. "strings"
  21. "github.com/spf13/cobra"
  22. apicontext "github.com/docker/compose-cli/context"
  23. "github.com/docker/compose-cli/context/store"
  24. "github.com/docker/compose-cli/metrics"
  25. )
  26. var delegatedContextTypes = []string{store.DefaultContextType}
  27. // ComDockerCli name of the classic cli binary
  28. const ComDockerCli = "com.docker.cli"
  29. // ExecIfDefaultCtxType delegates to com.docker.cli if on moby context
  30. func ExecIfDefaultCtxType(ctx context.Context, root *cobra.Command) {
  31. currentContext := apicontext.CurrentContext(ctx)
  32. s := store.ContextStore(ctx)
  33. currentCtx, err := s.Get(currentContext)
  34. // Only run original docker command if the current context is not ours.
  35. if err != nil || mustDelegateToMoby(currentCtx.Type()) {
  36. Exec(root)
  37. }
  38. }
  39. func mustDelegateToMoby(ctxType string) bool {
  40. for _, ctype := range delegatedContextTypes {
  41. if ctxType == ctype {
  42. return true
  43. }
  44. }
  45. return false
  46. }
  47. // Exec delegates to com.docker.cli if on moby context
  48. func Exec(root *cobra.Command) {
  49. cmd := exec.Command(ComDockerCli, os.Args[1:]...)
  50. cmd.Stdin = os.Stdin
  51. cmd.Stdout = os.Stdout
  52. cmd.Stderr = os.Stderr
  53. signals := make(chan os.Signal, 1)
  54. childExit := make(chan bool)
  55. signal.Notify(signals) // catch all signals
  56. go func() {
  57. for {
  58. select {
  59. case sig := <-signals:
  60. if cmd.Process == nil {
  61. continue // can happen if receiving signal before the process is actually started
  62. }
  63. // nolint errcheck
  64. cmd.Process.Signal(sig)
  65. case <-childExit:
  66. return
  67. }
  68. }
  69. }()
  70. err := cmd.Run()
  71. childExit <- true
  72. if err != nil {
  73. metrics.Track(store.DefaultContextName, os.Args[1:], root.PersistentFlags(), metrics.FailureStatus)
  74. if exiterr, ok := err.(*exec.ExitError); ok {
  75. os.Exit(exiterr.ExitCode())
  76. }
  77. fmt.Fprintln(os.Stderr, err)
  78. os.Exit(1)
  79. }
  80. metrics.Track(store.DefaultContextName, os.Args[1:], root.PersistentFlags(), metrics.SuccessStatus)
  81. os.Exit(0)
  82. }
  83. // IsDefaultContextCommand checks if the command exists in the classic cli (issues a shellout --help)
  84. func IsDefaultContextCommand(dockerCommand string) bool {
  85. cmd := exec.Command(ComDockerCli, dockerCommand, "--help")
  86. b, e := cmd.CombinedOutput()
  87. if e != nil {
  88. fmt.Println(e)
  89. }
  90. output := string(b)
  91. contains := strings.Contains(output, "Usage:\tdocker "+dockerCommand)
  92. return contains
  93. }
  94. // ExecSilent executes a command and do redirect output to stdOut, return output
  95. func ExecSilent(ctx context.Context) ([]byte, error) {
  96. cmd := exec.CommandContext(ctx, ComDockerCli, os.Args[1:]...)
  97. return cmd.CombinedOutput()
  98. }