exec.go 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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 context
  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
  32. // ours.
  33. if err != nil || currentCtx.Type() == store.DefaultContextType {
  34. shellOut(ctx)
  35. }
  36. }
  37. func shellOut(ctx context.Context) {
  38. cmd := exec.CommandContext(ctx, ComDockerCli, os.Args[1:]...)
  39. cmd.Stdin = os.Stdin
  40. cmd.Stdout = os.Stdout
  41. cmd.Stderr = os.Stderr
  42. if err := cmd.Run(); err != nil {
  43. if exiterr, ok := err.(*exec.ExitError); ok {
  44. os.Exit(exiterr.ExitCode())
  45. }
  46. fmt.Fprintln(os.Stderr, err)
  47. os.Exit(1)
  48. }
  49. os.Exit(0)
  50. }
  51. // 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
  52. func ExecCmd(command *cobra.Command) error {
  53. shellOut(command.Context())
  54. return nil
  55. }
  56. // IsDefaultContextCommand checks if the command exists in the classic cli (issues a shellout --help)
  57. func IsDefaultContextCommand(dockerCommand string) bool {
  58. cmd := exec.Command(ComDockerCli, dockerCommand, "--help")
  59. b, e := cmd.CombinedOutput()
  60. if e != nil {
  61. fmt.Println(e)
  62. }
  63. output := string(b)
  64. contains := strings.Contains(output, "Usage:\tdocker "+dockerCommand)
  65. return contains
  66. }
  67. // ExecSilent executes a command and do redirect output to stdOut, return output
  68. func ExecSilent(ctx context.Context) ([]byte, error) {
  69. cmd := exec.CommandContext(ctx, ComDockerCli, os.Args[1:]...)
  70. return cmd.CombinedOutput()
  71. }