Advertisement
Tainel

devenv.sh

Mar 28th, 2023 (edited)
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 1.55 KB | Software | 0 0
  1. #!/bin/sh
  2.  
  3. # Exit immediately if a command fails.
  4. set -e
  5. # Treat unset variables as errors.
  6. set -u
  7.  
  8. # Name of the Docker Compose service for the development environment.
  9. readonly SERVICE="devenv"
  10.  
  11. # Function to show a help message about this shell script.
  12. help(){
  13.     # Print the help message line by line with a single command.
  14.     printf "%s\n"\
  15.         "Manage development environment containers."\
  16.         ""\
  17.         "Usage: ${0} [help | start | stop]"\
  18.         ""\
  19.         "Commands:"\
  20.         "  help   Show this help message. Default command."\
  21.         "  start  Start a container."\
  22.         "  stop   Stop all containers."
  23. }
  24.  
  25. # Function to start a development environment container.
  26. start(){
  27.     # Run the Docker Compose command to start the service.
  28.     docker compose run --quiet-pull --rm "${SERVICE}" 2>/dev/null
  29. }
  30.  
  31. # Function to stop all development environment containers.
  32. stop(){
  33.     # Run the Docker Compose command to stop the service.
  34.     docker compose kill "${SERVICE}" 2>/dev/null
  35. }
  36.  
  37. # Check that the number of arguments of the script is at most one.
  38. if [ $# -gt 1 ]; then
  39.     >&2 echo "Too many arguments, expected at most one."
  40.     return 1
  41. fi
  42.  
  43. # Command to run taken from the optional argument. Defaults to "help".
  44. readonly COMMAND="${1:-"help"}"
  45.  
  46. # Try to run the command.
  47. case "${COMMAND}" in
  48.     # Run the command if it's either "help", "start", or "stop".
  49.     "help"|"start"|"stop")
  50.         "${COMMAND}"
  51.         ;;
  52.     # Show an error message otherwise explaining that the argument is invalid.
  53.     *)
  54.         >&2 echo "Invalid argument, expected \"help\", \"start\", or \"stop\"."
  55.         return 1
  56.         ;;
  57. esac
  58.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement