You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

50 lines
1.2 KiB

  1. //nolint: gosec
  2. package main
  3. import (
  4. "fmt"
  5. "os"
  6. osexec "os/exec"
  7. "path/filepath"
  8. )
  9. // execute executes a shell command.
  10. func exec(args ...string) error {
  11. cmd := osexec.Command(args[0], args[1:]...)
  12. out, err := cmd.CombinedOutput()
  13. switch err := err.(type) {
  14. case nil:
  15. return nil
  16. case *osexec.ExitError:
  17. return fmt.Errorf("failed to run %q:\n%v", args, string(out))
  18. default:
  19. return err
  20. }
  21. }
  22. // execVerbose executes a shell command while displaying its output.
  23. func execVerbose(args ...string) error {
  24. cmd := osexec.Command(args[0], args[1:]...)
  25. cmd.Stdout = os.Stdout
  26. cmd.Stderr = os.Stderr
  27. return cmd.Run()
  28. }
  29. // execCompose runs a Docker Compose command for a testnet.
  30. func execCompose(dir string, args ...string) error {
  31. return exec(append(
  32. []string{"docker-compose", "-f", filepath.Join(dir, "docker-compose.yml")},
  33. args...)...)
  34. }
  35. // execComposeVerbose runs a Docker Compose command for a testnet and displays its output.
  36. func execComposeVerbose(dir string, args ...string) error {
  37. return execVerbose(append(
  38. []string{"docker-compose", "-f", filepath.Join(dir, "docker-compose.yml")},
  39. args...)...)
  40. }
  41. // execDocker runs a Docker command.
  42. func execDocker(args ...string) error {
  43. return exec(append([]string{"docker"}, args...)...)
  44. }