exec.go 2.6 KB

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