This script, which i source in my terminal, offers the function _cphetzner, that is used to create a git repository. This function uses another function _feed_variable that prompts user for missing variables.
My questions at this point are:
- Is this the right way to return from functions - by using
return 1? - If
set -o errexitis uncommented, does the script abort on anyreturn 1(e.g. from function_feed_variableor only the one that return to the shell? - If
set -o errexitis uncommented, what is the right way to exit the function without closing the terminal? - I feel like i am not designing the script in a way that it cleans up on errors. Any recommendations?
#!/usr/bin/env bash
# +---------------+
# | Bash settings |
# +---------------+
# abort on nonzero exitstatus
# set -o errexit
# abort on unbound variable
# set -o nounset
# don't hide errors within pipes
# set -o pipefail
# +----------------+
# | Bash Variables |
# +----------------+
# +----------------+
# | Script Content |
# +----------------+
function _feed_variable() {
local prompt_msg=$1
local var_to_set=$2
echo "$prompt_msg"
read answer
case $answer in
[Yy]*)
echo "Enter $2: "
read new_value
eval $var_to_set="'$new_value'"
return 0
;;
[Nn]*)
echo "Clearing up and quitting ..."
return 1
;;
esac
}
function _cphetzner() {
pushd $PWD > /dev/null
POSITIONAL_ARGS=()
REPO_NAME=
# TARGET_FOLDER=$PWD/$REPO_NAME
TARGET_FOLDER=
SOURCE_TEMPLATE=
while [[ $# -gt 0 ]]; do
case $1 in
-r|--repository)
REPO_NAME="$2"
shift
shift
;;
-s|--sourcetemplate)
SOURCE_TEMPLATE="$2"
shift
shift
;;
-t|--targetfolder)
TARGET_FOLDER="$2"
shift
shift
;;
-*|--*)
echo "Unknown option $1"
exit 1
;;
*)
POSITIONAL_ARGS+=("$1") # save positional arg
shift # past argument
;;
esac
done
#=====[ Print Info ]==================================
echo "SOURCE_TEMPLATE is:\t" $SOURCE_TEMPLATE
echo "TARGET_FOLDER is:\t" $TARGET_FOLDER
echo "{POSITIONAL_ARGS is [@]}:\t" ${POSITIONAL_ARGS[@]}
echo "REPO_NAME is:\t" $REPO_NAME
set -- "${POSITIONAL_ARGS[@]}"
if [[ -n $1 ]]; then
echo "Last line of file specified as non-opt/last argument:"
tail -1 "$1"
fi
#=====[ Create folder and initiate repository ]=======
# Check if user input has repo name
if [[ -z "$REPO_NAME" ]]; then
_feed_variable "Do you want to feed me a repository name? [y/n]" "REPO_NAME" ||
return 1
fi
# Check if repository in given path exists
if [[ -z "$TARGET_FOLDER" ]]; then
_feed_variable "Do you want to feed me a target name? [y/n]" "TARGET_FOLDER" ||
return 1
fi
while [[ -d "${TARGET_FOLDER}/${REPO_NAME}" ]]
do
echo "Repository already exist in this folder" &&
_feed_variable "Do you want to feed me another target name? [y/n]" "TARGET_FOLDER" ||
return 1
done
# Create folder
mkdir -p $TARGET_FOLDER/$REPO_NAME
# # Check if template folder exists
# [[ -z $SOURCE_TEMPLATE ]] && echo "No template folder" ||
# cp -a $SOURCE_TEMPLATE/ $TARGET_FOLDER/$REPO_NAME
#====[ Initiate Repo ]================================
cd $TARGET_FOLDER/$REPO_NAME
git init
git remote add origin hetzner:/yolo/$REPO_NAME.git
git add .
git commit -m "Initial commit"
echo -e "[DONE] Repo $REPO_NAME created successfully"
echo -ne "Do you want to push repo to Hetzner now? [y/n]"
read answer
case $answer in
[Yy]*)
echo "Pushing to Hetzner ..."
git push -u -f origin master
;;
[Nn]*)
;;
esac
echo "Have a great day ..."
return 0
popd > /dev/null
}