exec.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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. "strings"
  20. "github.com/spf13/cobra"
  21. apicontext "github.com/docker/api/context"
  22. "github.com/docker/api/context/store"
  23. )
  24. var delegatedContextTypes = []string{store.DefaultContextType}
  25. // ComDockerCli name of the classic cli binary
  26. const ComDockerCli = "com.docker.cli"
  27. // ExecIfDefaultCtxType delegates to com.docker.cli if on moby or AWS context (until there is an AWS backend)
  28. func ExecIfDefaultCtxType(ctx context.Context) {
  29. currentContext := apicontext.CurrentContext(ctx)
  30. s := store.ContextStore(ctx)
  31. currentCtx, err := s.Get(currentContext)
  32. // Only run original docker command if the current context is not ours.
  33. if err != nil || mustDelegateToMoby(currentCtx.Type()) {
  34. Exec(ctx)
  35. }
  36. }
  37. func mustDelegateToMoby(ctxType string) bool {
  38. for _, ctype := range delegatedContextTypes {
  39. if ctxType == ctype {
  40. return true
  41. }
  42. }
  43. return false
  44. }
  45. // Exec delegates to com.docker.cli if on moby context
  46. func Exec(ctx context.Context) {
  47. cmd := exec.CommandContext(ctx, ComDockerCli, os.Args[1:]...)
  48. cmd.Stdin = os.Stdin
  49. cmd.Stdout = os.Stdout
  50. cmd.Stderr = os.Stderr
  51. if err := cmd.Run(); err != nil {
  52. if exiterr, ok := err.(*exec.ExitError); ok {
  53. os.Exit(exiterr.ExitCode())
  54. }
  55. fmt.Fprintln(os.Stderr, err)
  56. os.Exit(1)
  57. }
  58. os.Exit(0)
  59. }
  60. // ExecCmd delegates the cli command to com.docker.cli. The error is never
  61. // returned (process will exit with docker classic exit code), the return type
  62. // is to make it easier to use with cobra commands
  63. func ExecCmd(command *cobra.Command) error {
  64. Exec(command.Context())
  65. return nil
  66. }
  67. // IsDefaultContextCommand checks if the command exists in the classic cli (issues a shellout --help)
  68. func IsDefaultContextCommand(dockerCommand string) bool {
  69. cmd := exec.Command(ComDockerCli, dockerCommand, "--help")
  70. b, e := cmd.CombinedOutput()
  71. if e != nil {
  72. fmt.Println(e)
  73. }
  74. output := string(b)
  75. contains := strings.Contains(output, "Usage:\tdocker "+dockerCommand)
  76. return contains
  77. }
  78. // ExecSilent executes a command and do redirect output to stdOut, return output
  79. func ExecSilent(ctx context.Context) ([]byte, error) {
  80. cmd := exec.CommandContext(ctx, ComDockerCli, os.Args[1:]...)
  81. return cmd.CombinedOutput()
  82. }