Update basic

This commit is contained in:
Thomas Forgione 2020-02-20 17:02:37 +01:00
parent 2b30a0334c
commit e2b50f6a25
1 changed files with 35 additions and 14 deletions

View File

@ -1,5 +1,17 @@
#!/usr/bin/env bash #!/usr/bin/env bash
if [ "$BASIC_BASE_URL" == "" ]; then
base_url="https://gitea.tforgione.fr/basic/"
custom_base_url=false
else
base_url=$BASIC_BASE_URL
echo $base_url | grep /$ > /dev/null 2>&1
if [ $? -ne 0 ]; then
base_url="$base_url"/
fi
custom_base_url=true
fi
eprint() { eprint() {
echo -e >&2 "\x1B[1;31merror:\x1B[0m" $@ echo -e >&2 "\x1B[1;31merror:\x1B[0m" $@
} }
@ -15,15 +27,17 @@ info_n() {
print-help() { print-help() {
echo -e "\033[32mbasic\033[0m" echo -e "\033[32mbasic\033[0m"
echo -e "Thomas Forgione <thomas@forgione.fr>" echo -e "Thomas Forgione <thomas@forgione.fr>"
echo -e "A script that automatically clones templates" echo -e "A script that automatically clones templates from $base_url"
echo -e "Use the environment variable BASIC_BASE_URL to clone templates from somewhere else."
echo echo
print-usage print-usage
} }
print-usage() { print-usage() {
echo -e "\033[33mUSAGE:\033[0m" echo -e "\033[33mUSAGE:\033[0m"
echo -e " \033[32mbasic init <latex|beamer|marp>\033[0m initialize a template in the current directory" echo -e " \033[32mbasic init <template>\033[0m initialize a template in the current directory"
echo -e " \033[32mbasic init <latex|beamer|marp> <directory>\033[0m initialize a template in the specified directory" echo -e " \033[32mbasic init <template> <directory>\033[0m initialize a template in the specified directory"
} }
print-usage-and-exit() { print-usage-and-exit() {
@ -32,13 +46,28 @@ print-usage-and-exit() {
} }
git_clone() { git_clone() {
GIT_TERMINAL_PROMPT=0 git ls-remote $1 > /dev/null 2>&1
if [ $? -ne 0 ]; then
eprint "the repository $1 does not exist or is private"
exit 1
fi
if [ -d $2 ] && [ ! -z "$(ls -A $2)" ]; then
if [ "$2" == "." ]; then
eprint "the current directory is not empty"
info "use \`basic init <template> <directory>\` to specify another directory"
else
eprint "the directory $2 exists and is not empty"
fi
print-usage-and-exit 2
fi
info_n "downloading template..." info_n "downloading template..."
GIT_TERMINAL_PROMPT=0 git clone $@ > /dev/null 2>&1 GIT_TERMINAL_PROMPT=0 git clone $@ > /dev/null 2>&1
if [ $? -ne 0 ]; then if [ $? -ne 0 ]; then
echo >&2 echo >&2
eprint "failed to download template" eprint "failed to download template"
exit 2 print-usage-and-exit 2
fi fi
echo " done!" echo " done!"
} }
@ -46,21 +75,13 @@ git_clone() {
init() { init() {
template=$1 template=$1
case $template in
"latex" | "beamer" | "marp") ;;
*)
eprint "template $template does not exist"
print-usage-and-exit 1
;;
esac
if [ $# -gt 1 ]; then if [ $# -gt 1 ]; then
name=$2 name=$2
else else
name=. name=.
fi fi
git_clone https://gitea.tforgione.fr/tforgione-basic/$1 $name git_clone $base_url/$1 $name
rm -rf $name/.git rm -rf $name/.git
} }