sail 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. #!/usr/bin/env bash
  2. UNAMEOUT="$(uname -s)"
  3. WHITE='\033[1;37m'
  4. NC='\033[0m'
  5. # Verify operating system is supported...
  6. case "${UNAMEOUT}" in
  7. Linux*) MACHINE=linux;;
  8. Darwin*) MACHINE=mac;;
  9. *) MACHINE="UNKNOWN"
  10. esac
  11. if [ "$MACHINE" == "UNKNOWN" ]; then
  12. echo "Unsupported operating system [$(uname -s)]. Laravel Sail supports macOS, Linux, and Windows (WSL2)." >&2
  13. exit 1
  14. fi
  15. # Define environment variables...
  16. export APP_PORT=${APP_PORT:-80}
  17. export APP_SERVICE=${APP_SERVICE:-"laravel.test"}
  18. export DB_PORT=${DB_PORT:-3306}
  19. export WWWUSER=${WWWUSER:-$UID}
  20. export WWWGROUP=${WWWGROUP:-$(id -g)}
  21. if [ "$MACHINE" == "linux" ]; then
  22. export SEDCMD="sed -i"
  23. elif [ "$MACHINE" == "mac" ]; then
  24. export SEDCMD="sed -i .bak"
  25. fi
  26. # Ensure that Docker is running...
  27. if ! docker info > /dev/null 2>&1; then
  28. echo -e "${WHITE}Docker is not running.${NC}" >&2
  29. exit 1
  30. fi
  31. # Determine if Sail is currently up...
  32. PSRESULT="$(docker-compose ps -q)"
  33. if docker-compose ps | grep 'Exit'; then
  34. echo -e "${WHITE}Shutting down old Sail processes...${NC}" >&2
  35. docker-compose down > /dev/null 2>&1
  36. EXEC="no"
  37. elif [ -n "$PSRESULT" ]; then
  38. EXEC="yes"
  39. else
  40. EXEC="no"
  41. fi
  42. # Function that outputs Sail is not running...
  43. function sail_is_not_running {
  44. echo -e "${WHITE}Sail is not running.${NC}" >&2
  45. echo "" >&2
  46. echo -e "${WHITE}You may Sail using the following commands:${NC} './vendor/bin/sail up' or './vendor/bin/sail up -d'" >&2
  47. exit 1
  48. }
  49. if [ $# -gt 0 ]; then
  50. # Source the ".env" file so Laravel's environment variables are available...
  51. if [ -f ./.env ]; then
  52. source ./.env
  53. fi
  54. # Proxy PHP commands to the "php" binary on the application container...
  55. if [ "$1" == "php" ]; then
  56. shift 1
  57. if [ "$EXEC" == "yes" ]; then
  58. docker-compose exec \
  59. -u sail \
  60. "$APP_SERVICE" \
  61. php "$@"
  62. else
  63. sail_is_not_running
  64. fi
  65. # Proxy Composer commands to the "composer" binary on the application container...
  66. elif [ "$1" == "composer" ]; then
  67. shift 1
  68. if [ "$EXEC" == "yes" ]; then
  69. docker-compose exec \
  70. -u sail \
  71. "$APP_SERVICE" \
  72. composer "$@"
  73. else
  74. sail_is_not_running
  75. fi
  76. # Proxy Artisan commands to the "artisan" binary on the application container...
  77. elif [ "$1" == "artisan" ] || [ "$1" == "art" ]; then
  78. shift 1
  79. if [ "$EXEC" == "yes" ]; then
  80. docker-compose exec \
  81. -u sail \
  82. "$APP_SERVICE" \
  83. php artisan "$@"
  84. else
  85. sail_is_not_running
  86. fi
  87. # Proxy the "test" command to the "php artisan test" Artisan command...
  88. elif [ "$1" == "test" ]; then
  89. shift 1
  90. if [ "$EXEC" == "yes" ]; then
  91. docker-compose exec \
  92. -u sail \
  93. "$APP_SERVICE" \
  94. php artisan test "$@"
  95. else
  96. sail_is_not_running
  97. fi
  98. # Proxy the "dusk" command to the "php artisan dusk" Artisan command...
  99. elif [ "$1" == "dusk" ]; then
  100. shift 1
  101. if [ "$EXEC" == "yes" ]; then
  102. docker-compose exec \
  103. -u sail \
  104. -e "APP_URL=http://laravel.test" \
  105. -e "DUSK_DRIVER_URL=http://selenium:4444/wd/hub" \
  106. "$APP_SERVICE" \
  107. php artisan dusk "$@"
  108. else
  109. sail_is_not_running
  110. fi
  111. # Proxy the "dusk:fails" command to the "php artisan dusk:fails" Artisan command...
  112. elif [ "$1" == "dusk:fails" ]; then
  113. shift 1
  114. if [ "$EXEC" == "yes" ]; then
  115. docker-compose exec \
  116. -u sail \
  117. -e "APP_URL=http://laravel.test" \
  118. -e "DUSK_DRIVER_URL=http://selenium:4444/wd/hub" \
  119. "$APP_SERVICE" \
  120. php artisan dusk:fails "$@"
  121. else
  122. sail_is_not_running
  123. fi
  124. # Initiate a Laravel Tinker session within the application container...
  125. elif [ "$1" == "tinker" ] ; then
  126. shift 1
  127. if [ "$EXEC" == "yes" ]; then
  128. docker-compose exec \
  129. -u sail \
  130. "$APP_SERVICE" \
  131. php artisan tinker
  132. else
  133. sail_is_not_running
  134. fi
  135. # Proxy Node commands to the "node" binary on the application container...
  136. elif [ "$1" == "node" ]; then
  137. shift 1
  138. if [ "$EXEC" == "yes" ]; then
  139. docker-compose exec \
  140. -u sail \
  141. "$APP_SERVICE" \
  142. node "$@"
  143. else
  144. sail_is_not_running
  145. fi
  146. # Proxy NPM commands to the "npm" binary on the application container...
  147. elif [ "$1" == "npm" ]; then
  148. shift 1
  149. if [ "$EXEC" == "yes" ]; then
  150. docker-compose exec \
  151. -u sail \
  152. "$APP_SERVICE" \
  153. npm "$@"
  154. else
  155. sail_is_not_running
  156. fi
  157. # Proxy NPX commands to the "npx" binary on the application container...
  158. elif [ "$1" == "npx" ]; then
  159. shift 1
  160. if [ "$EXEC" == "yes" ]; then
  161. docker-compose exec \
  162. -u sail \
  163. "$APP_SERVICE" \
  164. npx "$@"
  165. else
  166. sail_is_not_running
  167. fi
  168. # Proxy YARN commands to the "yarn" binary on the application container...
  169. elif [ "$1" == "yarn" ]; then
  170. shift 1
  171. if [ "$EXEC" == "yes" ]; then
  172. docker-compose exec \
  173. -u sail \
  174. "$APP_SERVICE" \
  175. yarn "$@"
  176. else
  177. sail_is_not_running
  178. fi
  179. # Initiate a MySQL CLI terminal session within the "mysql" container...
  180. elif [ "$1" == "mysql" ]; then
  181. shift 1
  182. if [ "$EXEC" == "yes" ]; then
  183. docker-compose exec \
  184. mysql \
  185. bash -c 'MYSQL_PWD=${MYSQL_PASSWORD} mysql -u ${MYSQL_USER} ${MYSQL_DATABASE}'
  186. else
  187. sail_is_not_running
  188. fi
  189. # Initiate a MySQL CLI terminal session within the "mariadb" container...
  190. elif [ "$1" == "mariadb" ]; then
  191. shift 1
  192. if [ "$EXEC" == "yes" ]; then
  193. docker-compose exec \
  194. mariadb \
  195. bash -c 'MYSQL_PWD=${MYSQL_PASSWORD} mysql -u ${MYSQL_USER} ${MYSQL_DATABASE}'
  196. else
  197. sail_is_not_running
  198. fi
  199. # Initiate a PostgreSQL CLI terminal session within the "pgsql" container...
  200. elif [ "$1" == "psql" ]; then
  201. shift 1
  202. if [ "$EXEC" == "yes" ]; then
  203. docker-compose exec \
  204. pgsql \
  205. bash -c 'PGPASSWORD=${PGPASSWORD} psql -U ${POSTGRES_USER} ${POSTGRES_DB}'
  206. else
  207. sail_is_not_running
  208. fi
  209. # Initiate a Bash shell within the application container...
  210. elif [ "$1" == "shell" ] || [ "$1" == "bash" ]; then
  211. shift 1
  212. if [ "$EXEC" == "yes" ]; then
  213. docker-compose exec \
  214. -u sail \
  215. "$APP_SERVICE" \
  216. bash
  217. else
  218. sail_is_not_running
  219. fi
  220. # Initiate a root user Bash shell within the application container...
  221. elif [ "$1" == "root-shell" ] ; then
  222. shift 1
  223. if [ "$EXEC" == "yes" ]; then
  224. docker-compose exec \
  225. "$APP_SERVICE" \
  226. bash
  227. else
  228. sail_is_not_running
  229. fi
  230. # Share the site...
  231. elif [ "$1" == "share" ]; then
  232. shift 1
  233. if [ "$EXEC" == "yes" ]; then
  234. docker run --init beyondcodegmbh/expose-server:latest share http://host.docker.internal:"$APP_PORT" \
  235. --server-host=laravel-sail.site \
  236. --server-port=8080 \
  237. "$@"
  238. else
  239. sail_is_not_running
  240. fi
  241. # Pass unknown commands to the "docker-compose" binary...
  242. else
  243. docker-compose "$@"
  244. fi
  245. else
  246. docker-compose ps
  247. fi