musare.sh 22 KB

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