lib.sh 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712
  1. #!/usr/bin/env bash
  2. # -*- coding: utf-8; mode: sh indent-tabs-mode: nil -*-
  3. # SPDX-License-Identifier: AGPL-3.0-or-later
  4. # shellcheck disable=SC2059,SC1117
  5. ADMIN_NAME="${ADMIN_NAME:-$(git config user.name)}"
  6. ADMIN_NAME="${ADMIN_NAME:-$USER}"
  7. ADMIN_EMAIL="${ADMIN_EMAIL:-$(git config user.email)}"
  8. ADMIN_EMAIL="${ADMIN_EMAIL:-$USER@$(hostname)}"
  9. if [[ -z "${REPO_ROOT}" ]]; then
  10. REPO_ROOT=$(dirname "${BASH_SOURCE[0]}")
  11. while [ -h "${REPO_ROOT}" ] ; do
  12. REPO_ROOT=$(readlink "${REPO_ROOT}")
  13. done
  14. REPO_ROOT=$(cd "${REPO_ROOT}/.." && pwd -P )
  15. fi
  16. if [[ -z ${TEMPLATES} ]]; then
  17. TEMPLATES="${REPO_ROOT}/utils/templates"
  18. fi
  19. if [[ -z "$CACHE" ]]; then
  20. CACHE="${REPO_ROOT}/cache"
  21. fi
  22. if [[ -z "$SYSTEMD_UNITS" ]]; then
  23. SYSTEMD_UNITS="/lib/systemd/system"
  24. fi
  25. if [[ -z ${DIFF_CMD} ]]; then
  26. DIFF_CMD="diff -u"
  27. if command -v colordiff >/dev/null; then
  28. DIFF_CMD="colordiff -u"
  29. fi
  30. fi
  31. DOT_CONFIG="${DOT_CONFIG:-${REPO_ROOT}/.config}"
  32. source_dot_config() {
  33. if [[ ! -e "$DOT_CONFIG" ]]; then
  34. info_msg "installing $DOT_CONFIG"
  35. cp "$(dirname "${BASH_SOURCE[0]}")/dot_config" "$DOT_CONFIG"
  36. if [[ ! -z ${SUDO_USER} ]]; then
  37. chown "${SUDO_USER}:${SUDO_USER}" "$DOT_CONFIG"
  38. fi
  39. fi
  40. # shellcheck disable=SC1090
  41. source "${REPO_ROOT}/.config"
  42. }
  43. sudo_or_exit() {
  44. # usage: sudo_or_exit
  45. if [ ! "$(id -u)" -eq 0 ]; then
  46. err_msg "this command requires root (sudo) privilege!" >&2
  47. exit 42
  48. fi
  49. }
  50. required_commands() {
  51. # usage: requires_commands [cmd1 ...]
  52. local exit_val=0
  53. while [ ! -z "$1" ]; do
  54. if ! command -v "$1" &>/dev/null; then
  55. err_msg "missing command $1"
  56. exit_val=42
  57. fi
  58. shift
  59. done
  60. return $exit_val
  61. }
  62. rst_title() {
  63. # usage: rst_title <header-text> [part|chapter|section]
  64. case ${2-chapter} in
  65. part) printf "\n${1//?/=}\n$1\n${1//?/=}\n";;
  66. chapter) printf "\n${1}\n${1//?/=}\n";;
  67. section) printf "\n${1}\n${1//?/-}\n";;
  68. *)
  69. err_msg "invalid argument '${2}' in line $(caller)"
  70. return 42
  71. ;;
  72. esac
  73. }
  74. if command -v fmt >/dev/null; then
  75. export FMT="fmt -u"
  76. else
  77. export FMT="cat"
  78. fi
  79. rst_para() {
  80. # usage: RST_INDENT=1 rst_para "lorem ipsum ..."
  81. local prefix=''
  82. if ! [[ -z $RST_INDENT ]] && [[ $RST_INDENT -gt 0 ]]; then
  83. prefix="$(for i in $(seq 1 "$RST_INDENT"); do printf " "; done)"
  84. echo -en "\n$*\n" | $FMT | prefix_stdout "$prefix"
  85. else
  86. echo -en "\n$*\n" | $FMT
  87. fi
  88. }
  89. err_msg() { echo -e "ERROR: $*" >&2; }
  90. warn_msg() { echo -e "WARN: $*" >&2; }
  91. info_msg() { echo -e "INFO: $*"; }
  92. clean_stdin() {
  93. if [[ $(uname -s) != 'Darwin' ]]; then
  94. while read -r -n1 -t 0.1; do : ; done
  95. fi
  96. }
  97. wait_key(){
  98. # usage: waitKEY [<timeout in sec>]
  99. clean_stdin
  100. local _t=$1
  101. local msg="${MSG:-** press any [KEY] to continue **}"
  102. [[ ! -z $FORCE_TIMEOUT ]] && _t=$FORCE_TIMEOUT
  103. [[ ! -z $_t ]] && _t="-t $_t"
  104. # shellcheck disable=SC2086
  105. read -r -s -n1 $_t -p "$msg"
  106. echo
  107. clean_stdin
  108. }
  109. ask_yn() {
  110. # usage: ask_yn <prompt-text> [Ny|Yn] [<timeout in sec>]
  111. local EXIT_YES=0 # exit status 0 --> successful
  112. local EXIT_NO=1 # exit status 1 --> error code
  113. local _t=$3
  114. [[ ! -z $FORCE_TIMEOUT ]] && _t=$FORCE_TIMEOUT
  115. [[ ! -z $_t ]] && _t="-t $_t"
  116. case "${2}" in
  117. Yn)
  118. local exit_val=${EXIT_YES}
  119. local choice="[YES/no]"
  120. local default="Yes"
  121. ;;
  122. *)
  123. local exit_val=${EXIT_NO}
  124. local choice="[NO/yes]"
  125. local default="No"
  126. ;;
  127. esac
  128. echo
  129. while true; do
  130. clean_stdin
  131. printf "$1 ${choice} "
  132. # shellcheck disable=SC2086
  133. read -r -n1 $_t
  134. if [[ -z $REPLY ]]; then
  135. printf "$default\n"; break
  136. elif [[ $REPLY =~ ^[Yy]$ ]]; then
  137. exit_val=${EXIT_YES}
  138. printf "\n"
  139. break
  140. elif [[ $REPLY =~ ^[Nn]$ ]]; then
  141. exit_val=${EXIT_NO}
  142. printf "\n"
  143. break
  144. fi
  145. _t=""
  146. err_msg "invalid choice"
  147. done
  148. clean_stdin
  149. return $exit_val
  150. }
  151. tee_stderr () {
  152. # usage::
  153. # tee_stderr 1 <<EOF | python -i
  154. # print("hello")
  155. # EOF
  156. # ...
  157. # >>> print("hello")
  158. # hello
  159. local _t="0";
  160. if [[ ! -z $1 ]] ; then _t="$1"; fi
  161. (while read -r line; do
  162. # shellcheck disable=SC2086
  163. sleep $_t
  164. echo -e "$line" >&2
  165. echo "$line"
  166. done)
  167. }
  168. prefix_stdout () {
  169. # usage: <cmd> | prefix_stdout [prefix]
  170. local prefix=" | "
  171. if [[ ! -z $1 ]] ; then prefix="$1"; fi
  172. # shellcheck disable=SC2162
  173. (while IFS= read line; do
  174. echo -e "${prefix}$line"
  175. done)
  176. }
  177. append_line() {
  178. # usage: append_line <line> <file>
  179. #
  180. # Append line if not exists, create file if not exists. E.g::
  181. #
  182. # append_line 'source ~/.foo' ~/bashrc
  183. local LINE=$1
  184. local FILE=$2
  185. grep -qFs -- "$LINE" "$FILE" || echo "$LINE" >> "$FILE"
  186. }
  187. cache_download() {
  188. # usage: cache_download <url> <local-filename>
  189. local exit_value=0
  190. if [[ ! -z ${SUDO_USER} ]]; then
  191. sudo -u "${SUDO_USER}" mkdir -p "${CACHE}"
  192. else
  193. mkdir -p "${CACHE}"
  194. fi
  195. if [[ -f "${CACHE}/$2" ]] ; then
  196. info_msg "already cached: $1"
  197. info_msg " --> ${CACHE}/$2"
  198. fi
  199. if [[ ! -f "${CACHE}/$2" ]]; then
  200. info_msg "caching: $1"
  201. info_msg " --> ${CACHE}/$2"
  202. if [[ ! -z ${SUDO_USER} ]]; then
  203. sudo -u "${SUDO_USER}" wget --progress=bar -O "${CACHE}/$2" "$1" ; exit_value=$?
  204. else
  205. wget --progress=bar -O "${CACHE}/$2" "$1" ; exit_value=$?
  206. fi
  207. if [[ $exit_value = 0 ]]; then
  208. err_msg "failed to download: $1"
  209. fi
  210. fi
  211. }
  212. choose_one() {
  213. # usage:
  214. #
  215. # DEFAULT_SELECT= 2 \
  216. # choose_one <name> "your selection?" "Coffee" "Coffee with milk"
  217. local default=${DEFAULT_SELECT-1}
  218. local REPLY
  219. local env_name=$1 && shift
  220. local choice=$1;
  221. local max="${#@}"
  222. local _t
  223. [[ ! -z $FORCE_TIMEOUT ]] && _t=$FORCE_TIMEOUT
  224. [[ ! -z $_t ]] && _t="-t $_t"
  225. list=("$@")
  226. echo -e "Menu::"
  227. for ((i=1; i<= $((max -1)); i++)); do
  228. if [[ "$i" == "$default" ]]; then
  229. echo -e " $i.) ${list[$i]} [default]"
  230. else
  231. echo -e " $i.) ${list[$i]}"
  232. fi
  233. done
  234. while true; do
  235. clean_stdin
  236. printf "$1 [$default] "
  237. if (( 10 > max )); then
  238. # shellcheck disable=SC2086
  239. read -r -n1 $_t
  240. else
  241. # shellcheck disable=SC2086,SC2229
  242. read -r $_t
  243. fi
  244. # selection fits
  245. [[ $REPLY =~ ^-?[0-9]+$ ]] && (( REPLY > 0 )) && (( REPLY < max )) && break
  246. # take default
  247. [[ -z $REPLY ]] && REPLY=$default && break
  248. _t=""
  249. err_msg "invalid choice"
  250. done
  251. eval "$env_name"='${list[${REPLY}]}'
  252. echo
  253. clean_stdin
  254. }
  255. install_template() {
  256. # usage:
  257. #
  258. # install_template [--no-eval] [--variant=<name>] \
  259. # {file} [{owner} [{group} [{chmod}]]]
  260. #
  261. # E.g. the origin of variant 'raw' of /etc/updatedb.conf is::
  262. #
  263. # ${TEMPLATES}/etc/updatedb.conf:raw
  264. #
  265. # To install variant 'raw' of /etc/updatedb.conf without evaluated
  266. # replacements you can use::
  267. #
  268. # install_template --variant=raw --no-eval \
  269. # /etc/updatedb.conf root root 644
  270. local _reply=""
  271. local do_eval=1
  272. local variant=""
  273. local pos_args=("$0")
  274. for i in "$@"; do
  275. case $i in
  276. --no-eval) do_eval=0; shift ;;
  277. --variant=*) variant=":${i#*=}"; shift ;;
  278. *) pos_args+=("$i") ;;
  279. esac
  280. done
  281. local dst="${pos_args[1]}"
  282. local template_origin="${TEMPLATES}${dst}${variant}"
  283. local template_file="${TEMPLATES}${dst}"
  284. local owner="${pos_args[2]-$(id -un)}"
  285. local group="${pos_args[3]-$(id -gn)}"
  286. local chmod="${pos_args[4]-644}"
  287. info_msg "install (eval=$do_eval): ${dst}"
  288. [[ ! -z $variant ]] && info_msg "variant: ${variant}"
  289. if [[ ! -f "${template_origin}" ]] ; then
  290. err_msg "${template_origin} does not exists"
  291. err_msg "... can't install $dst"
  292. wait_key 30
  293. return 42
  294. fi
  295. if [[ "$do_eval" == "1" ]]; then
  296. template_file="${CACHE}${dst}${variant}"
  297. info_msg "BUILD template ${template_file}"
  298. if [[ ! -z ${SUDO_USER} ]]; then
  299. sudo -u "${SUDO_USER}" mkdir -p "$(dirname "${template_file}")"
  300. else
  301. mkdir -p "$(dirname "${template_file}")"
  302. fi
  303. # shellcheck disable=SC2086
  304. eval "echo \"$(cat ${template_origin})\"" > "${template_file}"
  305. if [[ ! -z ${SUDO_USER} ]]; then
  306. chown "${SUDO_USER}:${SUDO_USER}" "${template_file}"
  307. fi
  308. else
  309. template_file=$template_origin
  310. fi
  311. mkdir -p "$(dirname "${dst}")"
  312. if [[ ! -f "${dst}" ]]; then
  313. info_msg "install: ${template_file}"
  314. sudo -H install -v -o "${owner}" -g "${group}" -m "${chmod}" \
  315. "${template_file}" "${dst}" | prefix_stdout
  316. return $?
  317. fi
  318. if [[ -f "${dst}" ]] && cmp --silent "${template_file}" "${dst}" ; then
  319. info_msg "file ${dst} allready installed"
  320. return 0
  321. fi
  322. info_msg "diffrent file ${dst} allready exists on this host"
  323. while true; do
  324. choose_one _reply "choose next step with file $dst" \
  325. "replace file" \
  326. "leave file unchanged" \
  327. "interactiv shell" \
  328. "diff files"
  329. case $_reply in
  330. "replace file")
  331. info_msg "install: ${template_file}"
  332. sudo -H install -v -o "${owner}" -g "${group}" -m "${chmod}" \
  333. "${template_file}" "${dst}" | prefix_stdout
  334. break
  335. ;;
  336. "leave file unchanged")
  337. break
  338. ;;
  339. "interactiv shell")
  340. echo "// edit ${dst} to your needs"
  341. echo "// exit with CTRL-D"
  342. sudo -H -u "${owner}" -i
  343. $DIFF_CMD "${dst}" "${template_file}"
  344. echo
  345. echo "did you edit file ..."
  346. printf " ${template_file}"
  347. if ask_yn "... to your needs?"; then
  348. break
  349. fi
  350. ;;
  351. "diff files")
  352. $DIFF_CMD "${dst}" "${template_file}" | prefix_stdout
  353. esac
  354. done
  355. }
  356. service_is_available() {
  357. # usage: service_is_available <URL>
  358. local URL="$1"
  359. if [[ -z $URL ]]; then
  360. err_msg "service_is_available: missing arguments"
  361. return 42
  362. fi
  363. http_code=$(curl -H 'Cache-Control: no-cache' \
  364. --silent -o /dev/null --head --write-out '%{http_code}' --insecure \
  365. "${URL}")
  366. exit_val=$?
  367. if [[ $exit_val = 0 ]]; then
  368. info_msg "got $http_code from ${URL}"
  369. fi
  370. case "$http_code" in
  371. 404|410|423) exit_val=$http_code;;
  372. esac
  373. return $exit_val
  374. }
  375. # Apache
  376. # ------
  377. # FIXME: Arch Linux & RHEL should be added
  378. if [[ -z "${APACHE_SITES_AVAILABE}" ]]; then
  379. APACHE_SITES_AVAILABE="/etc/apache2/sites-available"
  380. fi
  381. apache_is_installed() {
  382. (command -v apachectl \
  383. && command -v a2ensite \
  384. && command -v a2dissite ) &>/dev/null
  385. }
  386. apache_reload() {
  387. info_msg "reload apache .."
  388. echo
  389. sudo -H apachectl configtest
  390. sudo -H service apache2 force-reload
  391. }
  392. apache_install_site() {
  393. # usage: apache_install_site [<template option> ...] <mysite.conf>
  394. #
  395. # <template option>: see install_template
  396. local template_opts=()
  397. local pos_args=("$0")
  398. for i in "$@"; do
  399. case $i in
  400. -*) template_opts+=("$i");;
  401. *) pos_args+=("$i");;
  402. esac
  403. done
  404. install_template "${template_opts[@]}" \
  405. "${APACHE_SITES_AVAILABE}/${pos_args[1]}" \
  406. root root 644
  407. apache_enable_site "${pos_args[1]}"
  408. info_msg "installed apache site: ${pos_args[1]}"
  409. }
  410. apache_remove_site() {
  411. # usage: apache_remove_site <mysite.conf>
  412. info_msg "remove apache site: $1"
  413. apache_dissable_site "$1"
  414. rm -f "${APACHE_SITES_AVAILABE}/$1"
  415. }
  416. apache_enable_site() {
  417. # usage: apache_enable_site <mysite.conf>
  418. info_msg "enable apache site: $1"
  419. sudo -H a2ensite -q "$1"
  420. apache_reload
  421. }
  422. apache_dissable_site() {
  423. # usage: apache_disable_site <mysite.conf>
  424. info_msg "disable apache site: $1"
  425. sudo -H a2dissite -q "$1"
  426. apache_reload
  427. }
  428. # uWSGI
  429. # -----
  430. uWSGI_SETUP="${uWSGI_SETUP:=/etc/uwsgi}"
  431. uWSGI_restart() {
  432. # usage: uWSGI_restart()
  433. info_msg "restart uWSGI service"
  434. systemctl restart uwsgi
  435. }
  436. uWSGI_app_available() {
  437. # usage: uWSGI_app_available <myapp.ini>
  438. local CONF="$1"
  439. if [[ -z $CONF ]]; then
  440. err_msg "uWSGI_app_available: missing arguments"
  441. return 42
  442. fi
  443. [[ -f "${uWSGI_SETUP}/apps-available/${CONF}" ]]
  444. }
  445. uWSGI_install_app() {
  446. # usage: uWSGI_install_app [<template option> ...] <myapp.ini>
  447. #
  448. # <template option>: see install_template
  449. local pos_args=("$0")
  450. for i in "$@"; do
  451. case $i in
  452. -*) template_opts+=("$i");;
  453. *) pos_args+=("$i");;
  454. esac
  455. done
  456. install_template "${template_opts[@]}" \
  457. "${uWSGI_SETUP}/apps-available/${pos_args[1]}" \
  458. root root 644
  459. uWSGI_enable_app "${pos_args[1]}"
  460. uWSGI_restart
  461. info_msg "installed uWSGI app: ${pos_args[1]}"
  462. }
  463. uWSGI_remove_app() {
  464. # usage: uWSGI_remove_app <myapp.ini>
  465. local CONF="$1"
  466. info_msg "remove uWSGI app: ${CONF}"
  467. uWSGI_disable_app "${CONF}"
  468. uWSGI_restart
  469. rm -f "${uWSGI_SETUP}/apps-available/${CONF}"
  470. }
  471. uWSGI_app_enabled() {
  472. # usage: uWSGI_app_enabled <myapp.ini>
  473. local CONF="$1"
  474. if [[ -z $CONF ]]; then
  475. err_msg "uWSGI_app_enabled: missing arguments"
  476. return 42
  477. fi
  478. [[ -f "${uWSGI_SETUP}/apps-enabled/${CONF}" ]]
  479. }
  480. # shellcheck disable=SC2164
  481. uWSGI_enable_app() {
  482. # usage: uWSGI_enable_app <myapp.ini>
  483. local CONF="$1"
  484. if [[ -z $CONF ]]; then
  485. err_msg "uWSGI_enable_app: missing arguments"
  486. return 42
  487. fi
  488. pushd "${uWSGI_SETUP}/apps-enabled" >/dev/null
  489. rm -f "$CONF"
  490. # shellcheck disable=SC2226
  491. ln -s "../apps-available/${CONF}"
  492. info_msg "enabled uWSGI app: ${CONF} (restart uWSGI required)"
  493. popd >/dev/null
  494. }
  495. uWSGI_disable_app() {
  496. # usage: uWSGI_disable_app <myapp.ini>
  497. local CONF="$1"
  498. if [[ -z $CONF ]]; then
  499. err_msg "uWSGI_enable_app: missing arguments"
  500. return 42
  501. fi
  502. rm -f "${uWSGI_SETUP}/apps-enabled/${CONF}"
  503. # FIXME: restart uwsgi service won't stop wsgi forked processes of user searx.
  504. # I had to kill them manually here ...
  505. pkill -f "${uWSGI_SETUP}/apps-enabled/${CONF}" -9
  506. info_msg "disabled uWSGI app: ${CONF} (restart uWSGI required)"
  507. }
  508. # distro's package manager
  509. # ------------------------
  510. #
  511. # FIXME: Arch Linux & RHEL should be added
  512. #
  513. pkg_install() {
  514. # usage: TITEL='install foobar' pkg_install foopkg barpkg
  515. rst_title "${TITLE:-installation of packages}" section
  516. echo -en "\npackage(s)::\n\n $*\n" | $FMT
  517. if ! ask_yn "Should packages be installed?" Yn 30; then
  518. return 42
  519. fi
  520. # shellcheck disable=SC2068
  521. apt-get install -y $@
  522. }
  523. pkg_remove() {
  524. # usage: TITEL='remove foobar' pkg_remove foopkg barpkg
  525. rst_title "${TITLE:-remove packages}" section
  526. echo -en "\npackage(s)::\n\n $*\n" | $FMT
  527. if ! ask_yn "Should packages be removed (purge)?" Yn 30; then
  528. return 42
  529. fi
  530. apt-get purge --autoremove --ignore-missing -y "$@"
  531. }
  532. pkg_is_installed() {
  533. # usage: pkg_is_install foopkg || pkg_install foopkg
  534. dpkg -l "$1" &> /dev/null
  535. return $?
  536. }
  537. # git tooling
  538. # -----------
  539. # shellcheck disable=SC2164
  540. git_clone() {
  541. # usage:
  542. #
  543. # git_clone <url> <name> [<branch> [<user>]]
  544. # git_clone <url> <path> [<branch> [<user>]]
  545. #
  546. # First form uses $CACHE/<name> as destination folder, second form clones
  547. # into <path>. If repository is allready cloned, pull from <branch> and
  548. # update working tree (if needed, the caller has to stash local changes).
  549. #
  550. # git clone https://github.com/asciimoo/searx searx-src origin/master searxlogin
  551. #
  552. local url="$1"
  553. local dest="$2"
  554. local branch="$3"
  555. local user="$4"
  556. local bash_cmd="bash"
  557. local remote="origin"
  558. if [[ ! "${dest:0:1}" = "/" ]]; then
  559. dest="$CACHE/$dest"
  560. fi
  561. [[ -z $branch ]] && branch=master
  562. [[ -z $user ]] && [[ ! -z "${SUDO_USER}" ]] && user="${SUDO_USER}"
  563. [[ ! -z $user ]] && bash_cmd="sudo -H -u $user -i"
  564. if [[ -d "${dest}" ]] ; then
  565. info_msg "already cloned: $dest"
  566. tee_stderr 0.1 <<EOF | $bash_cmd 2>&1 | prefix_stdout " |$user| "
  567. cd "${dest}"
  568. git checkout -m -B "$branch" --track "$remote/$branch"
  569. git pull --all
  570. EOF
  571. else
  572. info_msg "clone into: $dest"
  573. tee_stderr 0.1 <<EOF | $bash_cmd 2>&1 | prefix_stdout " |$user| "
  574. mkdir -p "$(dirname "$dest")"
  575. cd "$(dirname "$dest")"
  576. git clone --branch "$branch" --origin "$remote" "$url" "$(basename "$dest")"
  577. EOF
  578. fi
  579. }