musare.sh 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696
  1. #!/bin/bash
  2. set -e
  3. export PATH=/usr/local/bin:/usr/bin:/bin
  4. # Color variables
  5. CYAN='\033[33;36m';
  6. RED='\033[0;31m'
  7. YELLOW='\033[0;93m'
  8. GREEN='\033[0;32m'
  9. NC='\033[0m'
  10. # Print provided formatted error and exit with code (default 1)
  11. throw()
  12. {
  13. echo -e "${RED}${1}${NC}"
  14. exit "${2:-1}"
  15. }
  16. # Navigate to repository
  17. scriptLocation=$(dirname -- "$(readlink -fn -- "$0"; echo x)")
  18. cd "${scriptLocation%x}"
  19. # Import environment variables
  20. if [[ ! -f .env ]]; then
  21. throw "Error: .env does not exist" 2
  22. fi
  23. # shellcheck disable=SC1091
  24. source .env
  25. # Define docker command
  26. docker="${DOCKER_COMMAND:-docker}"
  27. if [[ ${docker} != "docker" && ${docker} != "podman" ]]; then
  28. throw "Error: Invalid DOCKER_COMMAND"
  29. fi
  30. set +e
  31. # Check if docker is installed
  32. ${docker} --version > /dev/null 2>&1
  33. dockerInstalled=$?
  34. # Define docker compose command
  35. dockerCompose="${docker} compose"
  36. # Check if docker compose is installed
  37. ${dockerCompose} version > /dev/null 2>&1
  38. composeInstalled=$?
  39. if [[ ${composeInstalled} -gt 0 ]]; then
  40. dockerCompose="${docker}-compose"
  41. ${dockerCompose} --version > /dev/null 2>&1
  42. composeInstalled=$?
  43. fi
  44. set -e
  45. # Exit if docker and/or docker compose is not installed
  46. if [[ ${dockerInstalled} -gt 0 || ${composeInstalled} -gt 0 ]]; then
  47. if [[ ${dockerInstalled} -eq 0 && ${composeInstalled} -gt 0 ]]; then
  48. throw "Error: ${dockerCompose} not installed."
  49. fi
  50. if [[ ${dockerInstalled} -gt 0 && ${composeInstalled} -eq 0 ]]; then
  51. throw "Error: ${docker} not installed."
  52. fi
  53. throw "Error: ${docker} and ${dockerCompose} not installed."
  54. fi
  55. # Add docker compose file arguments to command
  56. composeFiles="-f compose.yml"
  57. if [[ ${APP_ENV} == "development" ]]; then
  58. composeFiles="${composeFiles} -f compose.dev.yml"
  59. fi
  60. if [[ ${CONTAINER_MODE} == "local" ]]; then
  61. composeFiles="${composeFiles} -f compose.local.yml"
  62. fi
  63. if [[ -f compose.override.yml ]]; then
  64. composeFiles="${composeFiles} -f compose.override.yml"
  65. elif [[ -f docker-compose.override.yml ]]; then
  66. composeFiles="${composeFiles} -f docker-compose.override.yml"
  67. fi
  68. dockerCompose="${dockerCompose} ${composeFiles}"
  69. # Parse services from arguments string
  70. handleServices()
  71. {
  72. # shellcheck disable=SC2206
  73. validServices=($1)
  74. servicesArray=()
  75. invalidServices=false
  76. for x in "${@:2}"; do
  77. if [[ ${validServices[*]} =~ (^|[[:space:]])"$x"($|[[:space:]]) ]]; then
  78. if ! [[ ${servicesArray[*]} =~ (^|[[:space:]])"$x"($|[[:space:]]) ]]; then
  79. servicesArray+=("${x}")
  80. fi
  81. else
  82. if [[ $invalidServices == false ]]; then
  83. invalidServices="${x}"
  84. else
  85. invalidServices="${invalidServices} ${x}"
  86. fi
  87. fi
  88. done
  89. if [[ $invalidServices == false && ${#servicesArray[@]} -gt 0 ]]; then
  90. echo "1|${servicesArray[*]}"
  91. elif [[ $invalidServices == false ]]; then
  92. echo "1|all"
  93. else
  94. echo "0|Invalid Service(s): ${invalidServices}"
  95. fi
  96. }
  97. # Execute a docker command
  98. runDockerCommand()
  99. {
  100. validCommands=(start stop restart pull build ps logs)
  101. if ! [[ ${validCommands[*]} =~ (^|[[:space:]])"$2"($|[[:space:]]) ]]; then
  102. throw "Error: Invalid runDockerCommand input"
  103. fi
  104. servicesString=$(handleServices "backend frontend mongo postgres redis" "${@:3}")
  105. if [[ ${servicesString:0:1} != 1 ]]; then
  106. throw "${servicesString:2}\n${YELLOW}Usage: ${1} [backend, frontend, mongo, postgres, redis]"
  107. fi
  108. if [[ ${servicesString:2:4} == "all" ]]; then
  109. servicesString=""
  110. pullServices="mongo postgres redis"
  111. buildServices="backend frontend"
  112. else
  113. servicesString=${servicesString:2}
  114. pullArray=()
  115. buildArray=()
  116. if [[ "${servicesString}" == *mongo* ]]; then
  117. pullArray+=("mongo")
  118. fi
  119. if [[ "${servicesString}" == *postgres* ]]; then
  120. pullArray+=("postgres")
  121. fi
  122. if [[ "${servicesString}" == *redis* ]]; then
  123. pullArray+=("redis")
  124. fi
  125. if [[ "${servicesString}" == *backend* ]]; then
  126. buildArray+=("backend")
  127. fi
  128. if [[ "${servicesString}" == *frontend* ]]; then
  129. buildArray+=("frontend")
  130. fi
  131. pullServices="${pullArray[*]}"
  132. buildServices="${buildArray[*]}"
  133. fi
  134. if [[ ${2} == "stop" || ${2} == "restart" ]]; then
  135. # shellcheck disable=SC2086
  136. ${dockerCompose} stop ${servicesString}
  137. fi
  138. if [[ ${2} == "start" || ${2} == "restart" ]]; then
  139. # shellcheck disable=SC2086
  140. ${dockerCompose} up -d ${servicesString}
  141. fi
  142. if [[ ${2} == "pull" && ${pullServices} != "" ]]; then
  143. # shellcheck disable=SC2086
  144. ${dockerCompose} pull ${pullServices}
  145. fi
  146. if [[ ${2} == "build" && ${buildServices} != "" ]]; then
  147. # shellcheck disable=SC2086
  148. ${dockerCompose} build ${buildServices}
  149. fi
  150. if [[ ${2} == "ps" || ${2} == "logs" ]]; then
  151. # shellcheck disable=SC2086
  152. ${dockerCompose} "${2}" ${servicesString}
  153. fi
  154. }
  155. # Get docker container id
  156. getContainerId()
  157. {
  158. if [[ $docker == "docker" ]]; then
  159. containerId=$(${dockerCompose} ps -q "${1}")
  160. else
  161. containerId=$(${dockerCompose} ps \
  162. | sed '0,/CONTAINER/d' \
  163. | awk "/${1}/ {print \$1;exit}")
  164. fi
  165. echo "${containerId}"
  166. }
  167. # Reset services
  168. handleReset()
  169. {
  170. servicesString=$(handleServices "backend frontend mongo postgres redis" "${@:2}")
  171. if [[ ${servicesString:0:1} != 1 ]]; then
  172. throw "${servicesString:2}\n${YELLOW}Usage: ${1} [backend, frontend, mongo, postgres, redis]"
  173. fi
  174. confirmMessage="${GREEN}Are you sure you want to reset all data"
  175. if [[ ${servicesString:2:4} != "all" ]]; then
  176. confirmMessage="${confirmMessage} for $(echo "${servicesString:2}" | tr ' ' ',')"
  177. fi
  178. echo -e "${confirmMessage}? ${YELLOW}[y,n]: ${NC}"
  179. read -r confirm
  180. if [[ "${confirm}" != y* ]]; then
  181. throw "Cancelled reset"
  182. fi
  183. if [[ ${servicesString:2:4} == "all" ]]; then
  184. runDockerCommand "$(basename "$0") $1" stop
  185. ${dockerCompose} rm -v --force
  186. else
  187. # shellcheck disable=SC2086
  188. runDockerCommand "$(basename "$0") $1" stop ${servicesString:2}
  189. # shellcheck disable=SC2086
  190. ${dockerCompose} rm -v --force ${servicesString:2}
  191. fi
  192. }
  193. # Attach to service in docker container
  194. attachContainer()
  195. {
  196. containerId=$(getContainerId "${2}")
  197. if [[ -z $containerId ]]; then
  198. throw "Error: ${2} offline, please start to attach."
  199. fi
  200. case $2 in
  201. backend)
  202. echo -e "${YELLOW}Detach with CTRL+P+Q${NC}"
  203. ${docker} attach "$containerId"
  204. ;;
  205. mongo)
  206. MONGO_VERSION_INT=${MONGO_VERSION:0:1}
  207. echo -e "${YELLOW}Detach with CTRL+D${NC}"
  208. if [[ $MONGO_VERSION_INT -ge 5 ]]; then
  209. ${dockerCompose} exec mongo mongosh musare -u "${MONGO_USER_USERNAME}" -p "${MONGO_USER_PASSWORD}" --eval "disableTelemetry()" --shell
  210. else
  211. ${dockerCompose} exec mongo mongo musare -u "${MONGO_USER_USERNAME}" -p "${MONGO_USER_PASSWORD}"
  212. fi
  213. ;;
  214. postgres)
  215. echo -e "${YELLOW}Detach with CTRL+D${NC}"
  216. PGPASSWORD="${POSTGRES_PASSWORD}" ${dockerCompose} exec postgres psql "${POSTGRES_USERNAME}" musare
  217. ;;
  218. redis)
  219. echo -e "${YELLOW}Detach with CTRL+C${NC}"
  220. ${dockerCompose} exec redis redis-cli -a "${REDIS_PASSWORD}"
  221. ;;
  222. *)
  223. throw "Invalid service ${2}\n${YELLOW}Usage: ${1} [backend, mongo, redis]"
  224. ;;
  225. esac
  226. }
  227. # Lint codebase, docs, scripts, etc
  228. handleLinting()
  229. {
  230. set +e
  231. # shellcheck disable=SC2001
  232. services=$(sed "s/\(\ \)\{0,1\}\(-\)\{0,2\}fix//g;t;q1" <<< "${@:2}")
  233. fixFound=$?
  234. if [[ $fixFound -eq 0 ]]; then
  235. fix="--fix"
  236. echo -e "${GREEN}Auto-fix enabled${NC}"
  237. fi
  238. # shellcheck disable=SC2001
  239. services=$(sed "s/\(\ \)\{0,1\}\(-\)\{0,2\}no-cache//g;t;q1" <<< "${services}")
  240. noCacheFound=$?
  241. cache="--cache"
  242. if [[ $noCacheFound -eq 0 ]]; then
  243. cache=""
  244. echo -e "${YELLOW}ESlint cache disabled${NC}"
  245. fi
  246. set -e
  247. # shellcheck disable=SC2068
  248. servicesString=$(handleServices "backend frontend docs shell" ${services[@]})
  249. if [[ ${servicesString:0:1} != 1 ]]; then
  250. throw "${servicesString:2}\n${YELLOW}Usage: ${1} [backend, frontend, docs, shell] [fix]"
  251. fi
  252. set +e
  253. if [[ ${servicesString:2:4} == "all" || "${servicesString:2}" == *frontend* ]]; then
  254. echo -e "${CYAN}Running frontend lint...${NC}"
  255. # shellcheck disable=SC2086
  256. ${dockerCompose} exec -T frontend npm run lint -- ${cache} ${fix}
  257. frontendExitValue=$?
  258. fi
  259. if [[ ${servicesString:2:4} == "all" || "${servicesString:2}" == *backend* ]]; then
  260. echo -e "${CYAN}Running backend lint...${NC}"
  261. # shellcheck disable=SC2086
  262. ${dockerCompose} exec -T backend npm run lint -- ${cache} ${fix}
  263. backendExitValue=$?
  264. fi
  265. if [[ ${servicesString:2:4} == "all" || "${servicesString:2}" == *docs* ]]; then
  266. echo -e "${CYAN}Running docs lint...${NC}"
  267. # shellcheck disable=SC2086
  268. ${docker} run --rm -v "${scriptLocation}":/workdir ghcr.io/igorshubovych/markdownlint-cli:latest ".wiki" "*.md" ${fix}
  269. docsExitValue=$?
  270. fi
  271. if [[ ${servicesString:2:4} == "all" || "${servicesString:2}" == *shell* ]]; then
  272. echo -e "${CYAN}Running shell lint...${NC}"
  273. ${docker} run --rm -v "${scriptLocation}":/mnt koalaman/shellcheck:stable ./*.sh ./**/*.sh
  274. shellExitValue=$?
  275. fi
  276. set -e
  277. if [[ ${frontendExitValue} -gt 0 || ${backendExitValue} -gt 0 || ${docsExitValue} -gt 0 || ${shellExitValue} -gt 0 ]]; then
  278. exit 1
  279. fi
  280. }
  281. # Validate typescript in services
  282. handleTypescript()
  283. {
  284. set +e
  285. # shellcheck disable=SC2001
  286. services=$(sed "s/\(\ \)\{0,1\}\(-\)\{0,2\}strict//g;t;q1" <<< "${@:2}")
  287. strictFound=$?
  288. if [[ $strictFound -eq 0 ]]; then
  289. strict="--strict"
  290. echo -e "${GREEN}Strict mode enabled${NC}"
  291. fi
  292. set -e
  293. # shellcheck disable=SC2068
  294. servicesString=$(handleServices "backend frontend" ${services[@]})
  295. if [[ ${servicesString:0:1} != 1 ]]; then
  296. throw "${servicesString:2}\n${YELLOW}Usage: ${1} [backend, frontend] [strict]"
  297. fi
  298. set +e
  299. if [[ ${servicesString:2:4} == "all" || "${servicesString:2}" == *frontend* ]]; then
  300. echo -e "${CYAN}Running frontend typescript check...${NC}"
  301. # shellcheck disable=SC2086
  302. ${dockerCompose} exec -T frontend npm run typescript -- ${strict}
  303. frontendExitValue=$?
  304. fi
  305. if [[ ${servicesString:2:4} == "all" || "${servicesString:2}" == *backend* ]]; then
  306. echo -e "${CYAN}Running backend typescript check...${NC}"
  307. # shellcheck disable=SC2086
  308. ${dockerCompose} exec -T backend npm run typescript -- ${strict}
  309. backendExitValue=$?
  310. fi
  311. set -e
  312. if [[ ${frontendExitValue} -gt 0 || ${backendExitValue} -gt 0 ]]; then
  313. exit 1
  314. fi
  315. }
  316. # Execute automated tests in services
  317. handleTests()
  318. {
  319. servicesString=$(handleServices "backend frontend" "${@:2}")
  320. if [[ ${servicesString:0:1} != 1 ]]; then
  321. throw "${servicesString:2}\n${YELLOW}Usage: ${1} [backend, frontend]"
  322. fi
  323. set +e
  324. if [[ ${servicesString:2:4} == "all" || "${servicesString:2}" == *backend* ]]; then
  325. echo -e "${CYAN}Running backend tests...${NC}"
  326. ${dockerCompose} exec -T backend npm run test
  327. backendExitValue=$?
  328. fi
  329. if [[ ${servicesString:2:4} == "all" || "${servicesString:2}" == *frontend* ]]; then
  330. echo -e "${CYAN}Running frontend tests...${NC}"
  331. ${dockerCompose} exec -T frontend npm run test -- --run
  332. frontendExitValue=$?
  333. fi
  334. set -e
  335. if [[ ${backendExitValue} -gt 0 || ${frontendExitValue} -gt 0 ]]; then
  336. exit 1
  337. fi
  338. }
  339. # Execute test coverage in services
  340. handleTestCoverage()
  341. {
  342. servicesString=$(handleServices "frontend" "${@:2}")
  343. if [[ ${servicesString:0:1} != 1 ]]; then
  344. throw "${servicesString:2}\n${YELLOW}Usage: ${1} [frontend]"
  345. fi
  346. set +e
  347. if [[ ${servicesString:2:4} == "all" || "${servicesString:2}" == *frontend* ]]; then
  348. echo -e "${CYAN}Running frontend test coverage report...${NC}"
  349. ${dockerCompose} exec -T frontend npm run coverage
  350. frontendExitValue=$?
  351. fi
  352. set -e
  353. if [[ ${frontendExitValue} -gt 0 ]]; then
  354. exit 1
  355. fi
  356. }
  357. # Update Musare
  358. handleUpdate()
  359. {
  360. musareshModified=$(git diff HEAD -- musare.sh)
  361. git fetch
  362. updated=$(git log --name-only --oneline HEAD..@\{u\})
  363. if [[ ${updated} == "" ]]; then
  364. echo -e "${GREEN}Already up to date${NC}"
  365. exit 0
  366. fi
  367. breakingConfigChange=$(git rev-list "$(git rev-parse HEAD)" | grep d8b73be1de231821db34c677110b7b97e413451f)
  368. if [[ -f backend/config/default.json && -z $breakingConfigChange ]]; then
  369. throw "Configuration has breaking changes. Please rename or remove 'backend/config/default.json' and run the update command again to continue."
  370. fi
  371. musareshChange=$(echo "${updated}" | grep "musare.sh")
  372. dbChange=$(echo "${updated}" | grep "backend/logic/db/schemas")
  373. bcChange=$(echo "${updated}" | grep "backend/config/default.json")
  374. if [[ ( $2 == "auto" && -z $dbChange && -z $bcChange && -z $musareshChange ) || -z $2 ]]; then
  375. if [[ -n $musareshChange && $(git diff @\{u\} -- musare.sh) != "" ]]; then
  376. if [[ $musareshModified != "" ]]; then
  377. throw "musare.sh has been modified, please reset these changes and run the update command again to continue."
  378. else
  379. git checkout @\{u\} -- musare.sh
  380. throw "${YELLOW}musare.sh has been updated, please run the update command again to continue."
  381. fi
  382. else
  383. git pull
  384. echo -e "${CYAN}Updating...${NC}"
  385. runDockerCommand "$(basename "$0") $1" pull
  386. runDockerCommand "$(basename "$0") $1" build
  387. runDockerCommand "$(basename "$0") $1" restart
  388. echo -e "${GREEN}Updated!${NC}"
  389. if [[ -n $dbChange ]]; then
  390. echo -e "${RED}Database schema has changed, please run migration!${NC}"
  391. fi
  392. if [[ -n $bcChange ]]; then
  393. echo -e "${RED}Backend config has changed, please update!${NC}"
  394. fi
  395. fi
  396. elif [[ $2 == "auto" ]]; then
  397. throw "Auto Update Failed! musare.sh, database and/or config has changed!"
  398. fi
  399. }
  400. # Backup the database
  401. handleBackup()
  402. {
  403. if [[ -z "${BACKUP_LOCATION}" ]]; then
  404. backupLocation="${scriptLocation%x}/backups"
  405. else
  406. backupLocation="${BACKUP_LOCATION%/}"
  407. fi
  408. if [[ ! -d "${backupLocation}" ]]; then
  409. echo -e "${YELLOW}Creating backup directory at ${backupLocation}${NC}"
  410. mkdir "${backupLocation}"
  411. fi
  412. if [[ -z "${BACKUP_NAME}" ]]; then
  413. backupLocation="${backupLocation}/musare-$(date +"%Y-%m-%d-%s").dump"
  414. else
  415. backupLocation="${backupLocation}/${BACKUP_NAME}"
  416. fi
  417. echo -e "${YELLOW}Creating backup at ${backupLocation}${NC}"
  418. ${dockerCompose} exec -T mongo sh -c "mongodump --authenticationDatabase musare -u ${MONGO_USER_USERNAME} -p ${MONGO_USER_PASSWORD} -d musare --archive" > "${backupLocation}"
  419. }
  420. # Restore database from dump
  421. handleRestore()
  422. {
  423. if [[ -z $2 ]]; then
  424. echo -e "${GREEN}Please enter the full path of the dump you wish to restore: ${NC}"
  425. read -r restoreFile
  426. else
  427. restoreFile=$2
  428. fi
  429. if [[ -z ${restoreFile} ]]; then
  430. throw "Error: no restore path given, cancelled restoration."
  431. elif [[ -d ${restoreFile} ]]; then
  432. throw "Error: restore path given is a directory, cancelled restoration."
  433. elif [[ ! -f ${restoreFile} ]]; then
  434. throw "Error: no file at restore path given, cancelled restoration."
  435. else
  436. ${dockerCompose} exec -T mongo sh -c "mongorestore --authenticationDatabase musare -u ${MONGO_USER_USERNAME} -p ${MONGO_USER_PASSWORD} --archive" < "${restoreFile}"
  437. fi
  438. }
  439. # Toggle user admin role
  440. handleAdmin()
  441. {
  442. MONGO_VERSION_INT=${MONGO_VERSION:0:1}
  443. case $2 in
  444. add)
  445. if [[ -z $3 ]]; then
  446. echo -e "${GREEN}Please enter the username of the user you wish to make an admin: ${NC}"
  447. read -r adminUser
  448. else
  449. adminUser=$3
  450. fi
  451. if [[ -z $adminUser ]]; then
  452. throw "Error: Username for new admin not provided."
  453. fi
  454. if [[ $MONGO_VERSION_INT -ge 5 ]]; then
  455. ${dockerCompose} exec mongo mongosh musare -u "${MONGO_USER_USERNAME}" -p "${MONGO_USER_PASSWORD}" --eval "disableTelemetry(); db.users.updateOne({username: '${adminUser}'}, {\$set: {role: 'admin'}})"
  456. else
  457. ${dockerCompose} exec mongo mongo musare -u "${MONGO_USER_USERNAME}" -p "${MONGO_USER_PASSWORD}" --eval "db.users.updateOne({username: '${adminUser}'}, {\$set: {role: 'admin'}})"
  458. fi
  459. ;;
  460. remove)
  461. if [[ -z $3 ]]; then
  462. echo -e "${GREEN}Please enter the username of the user you wish to remove as admin: ${NC}"
  463. read -r adminUser
  464. else
  465. adminUser=$3
  466. fi
  467. if [[ -z $adminUser ]]; then
  468. throw "Error: Username for new admin not provided."
  469. fi
  470. if [[ $MONGO_VERSION_INT -ge 5 ]]; then
  471. ${dockerCompose} exec mongo mongosh musare -u "${MONGO_USER_USERNAME}" -p "${MONGO_USER_PASSWORD}" --eval "disableTelemetry(); db.users.updateOne({username: '${adminUser}'}, {\$set: {role: 'default'}})"
  472. else
  473. ${dockerCompose} exec mongo mongo musare -u "${MONGO_USER_USERNAME}" -p "${MONGO_USER_PASSWORD}" --eval "db.users.updateOne({username: '${adminUser}'}, {\$set: {role: 'default'}})"
  474. fi
  475. ;;
  476. *)
  477. throw "Invalid command $2\n${YELLOW}Usage: ${1} [add,remove] username"
  478. ;;
  479. esac
  480. }
  481. # Execute a command in a container
  482. handleExecute()
  483. {
  484. servicesString=$(handleServices "backend frontend mongo postgres redis" "${2}")
  485. if [[ ${servicesString:0:1} != 1 ]]; then
  486. throw "${servicesString:2}\n${YELLOW}Usage: ${1} [backend, frontend, mongo, postgres, redis]"
  487. fi
  488. # shellcheck disable=SC2068
  489. ${dockerCompose} exec "${2}" ${@:3}
  490. }
  491. availableCommands=$(cat << COMMANDS
  492. start - Start services
  493. stop - Stop services
  494. restart - Restart services
  495. status - Service status
  496. logs - View logs for services
  497. update - Update Musare
  498. attach [backend,mongo,postgres,redis] - Attach to backend service, mongo, postgres or redis shell
  499. build - Build services
  500. lint - Run lint on frontend, backend, docs and/or shell
  501. backup - Backup database data to file
  502. restore - Restore database data from backup file
  503. reset - Reset service data
  504. admin [add,remove] - Assign/unassign admin role to/from a user
  505. typescript - Run typescript checks on frontend and/or backend
  506. execute - Execute command in service docker container
  507. COMMANDS
  508. )
  509. # Handle command
  510. case $1 in
  511. start)
  512. echo -e "${CYAN}Musare | Start Services${NC}"
  513. # shellcheck disable=SC2068
  514. runDockerCommand "$(basename "$0") $1" start ${@:2}
  515. ;;
  516. stop)
  517. echo -e "${CYAN}Musare | Stop Services${NC}"
  518. # shellcheck disable=SC2068
  519. runDockerCommand "$(basename "$0") $1" stop ${@:2}
  520. ;;
  521. restart)
  522. echo -e "${CYAN}Musare | Restart Services${NC}"
  523. # shellcheck disable=SC2068
  524. runDockerCommand "$(basename "$0") $1" restart ${@:2}
  525. ;;
  526. build)
  527. echo -e "${CYAN}Musare | Build Services${NC}"
  528. # shellcheck disable=SC2068
  529. runDockerCommand "$(basename "$0") $1" pull ${@:2}
  530. # shellcheck disable=SC2068
  531. runDockerCommand "$(basename "$0") $1" build ${@:2}
  532. ;;
  533. status)
  534. echo -e "${CYAN}Musare | Service Status${NC}"
  535. # shellcheck disable=SC2068
  536. runDockerCommand "$(basename "$0") $1" ps ${@:2}
  537. ;;
  538. reset)
  539. echo -e "${CYAN}Musare | Reset Services${NC}"
  540. # shellcheck disable=SC2068
  541. handleReset "$(basename "$0") $1" ${@:2}
  542. ;;
  543. attach)
  544. echo -e "${CYAN}Musare | Attach${NC}"
  545. attachContainer "$(basename "$0") $1" "$2"
  546. ;;
  547. lint|eslint)
  548. echo -e "${CYAN}Musare | Lint${NC}"
  549. # shellcheck disable=SC2068
  550. handleLinting "$(basename "$0") $1" ${@:2}
  551. ;;
  552. typescript|ts)
  553. echo -e "${CYAN}Musare | TypeScript Check${NC}"
  554. # shellcheck disable=SC2068
  555. handleTypescript "$(basename "$0") $1" ${@:2}
  556. ;;
  557. test)
  558. echo -e "${CYAN}Musare | Test${NC}"
  559. # shellcheck disable=SC2068
  560. handleTests "$(basename "$0") $1" ${@:2}
  561. ;;
  562. test:coverage)
  563. echo -e "${CYAN}Musare | Test Coverage${NC}"
  564. # shellcheck disable=SC2068
  565. handleTestCoverage "$(basename "$0") $1" ${@:2}
  566. ;;
  567. update)
  568. echo -e "${CYAN}Musare | Update${NC}"
  569. # shellcheck disable=SC2068
  570. handleUpdate "$(basename "$0") $1" ${@:2}
  571. ;;
  572. logs)
  573. echo -e "${CYAN}Musare | Logs${NC}"
  574. # shellcheck disable=SC2068
  575. runDockerCommand "$(basename "$0") $1" logs ${@:2}
  576. ;;
  577. backup)
  578. echo -e "${CYAN}Musare | Backup${NC}"
  579. # shellcheck disable=SC2068
  580. handleBackup "$(basename "$0") $1" ${@:2}
  581. ;;
  582. restore)
  583. echo -e "${CYAN}Musare | Restore${NC}"
  584. # shellcheck disable=SC2068
  585. handleRestore "$(basename "$0") $1" ${@:2}
  586. ;;
  587. admin)
  588. echo -e "${CYAN}Musare | Add Admin${NC}"
  589. # shellcheck disable=SC2068
  590. handleAdmin "$(basename "$0") $1" ${@:2}
  591. ;;
  592. execute|exec)
  593. echo -e "${CYAN}Musare | Execute${NC}"
  594. # shellcheck disable=SC2068
  595. handleExecute "$(basename "$0") $1" ${@:2}
  596. ;;
  597. "")
  598. echo -e "${CYAN}Musare | Available Commands${NC}"
  599. echo -e "${YELLOW}${availableCommands}${NC}"
  600. ;;
  601. *)
  602. echo -e "${CYAN}Musare${NC}"
  603. echo -e "${RED}Error: Invalid Command $1${NC}"
  604. echo -e "${CYAN}Available Commands:${NC}"
  605. echo -e "${YELLOW}${availableCommands}${NC}"
  606. exit 1
  607. ;;
  608. esac