exec.go 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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. Exec(ctx)
  34. }
  35. }
  36. func mustDelegateToMoby(ctxType string) bool {
  37. return ctxType == store.DefaultContextType
  38. }
  39. // Exec delegates to com.docker.cli if on moby context
  40. func Exec(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
  55. // returned (process will exit with docker classic exit code), the return type
  56. // is to make it easier to use with cobra commands
  57. func ExecCmd(command *cobra.Command) error {
  58. Exec(command.Context())
  59. return nil
  60. }
  61. // IsDefaultContextCommand checks if the command exists in the classic cli (issues a shellout --help)
  62. func IsDefaultContextCommand(dockerCommand string) bool {
  63. cmd := exec.Command(ComDockerCli, dockerCommand, "--help")
  64. b, e := cmd.CombinedOutput()
  65. if e != nil {
  66. fmt.Println(e)
  67. }
  68. output := string(b)
  69. contains := strings.Contains(output, "Usage:\tdocker "+dockerCommand)
  70. return contains
  71. }
  72. // ExecSilent executes a command and do redirect output to stdOut, return output
  73. func ExecSilent(ctx context.Context) ([]byte, error) {
  74. cmd := exec.CommandContext(ctx, ComDockerCli, os.Args[1:]...)
  75. return cmd.CombinedOutput()
  76. }