exec.go 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. package dockerclassic
  2. import (
  3. "context"
  4. "fmt"
  5. "os"
  6. "os/exec"
  7. "github.com/spf13/cobra"
  8. apicontext "github.com/docker/api/context"
  9. "github.com/docker/api/context/store"
  10. )
  11. // Exec delegates to docker-classic
  12. func Exec(ctx context.Context) {
  13. currentContext := apicontext.CurrentContext(ctx)
  14. s := store.ContextStore(ctx)
  15. _, err := s.Get(currentContext)
  16. // Only run original docker command if the current context is not
  17. // ours.
  18. if err != nil {
  19. cmd := exec.CommandContext(ctx, "docker-classic", os.Args[1:]...)
  20. cmd.Stdin = os.Stdin
  21. cmd.Stdout = os.Stdout
  22. cmd.Stderr = os.Stderr
  23. if err := cmd.Run(); err != nil {
  24. if exiterr, ok := err.(*exec.ExitError); ok {
  25. fmt.Fprintln(os.Stderr, exiterr.Error())
  26. os.Exit(exiterr.ExitCode())
  27. }
  28. fmt.Fprintln(os.Stderr, err)
  29. os.Exit(1)
  30. }
  31. os.Exit(0)
  32. }
  33. }
  34. // ExecCmd delegates the cli command to docker-classic. 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
  35. func ExecCmd(command *cobra.Command) error {
  36. Exec(command.Context())
  37. return nil
  38. }