exec.go 2.7 KB

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