From a151081f1ac866daec900d36173112bce32f9ce1 Mon Sep 17 00:00:00 2001 From: Bobonium Date: Sun, 27 Oct 2019 11:55:24 +0100 Subject: [PATCH 1/4] YAML_FILE path needs to be adjusted after switching directory --- gitfile.sh | 1 + 1 file changed, 1 insertion(+) diff --git a/gitfile.sh b/gitfile.sh index 47af9a2..6261bfd 100755 --- a/gitfile.sh +++ b/gitfile.sh @@ -61,4 +61,5 @@ if [ ! -f "${YAML_FILE}" ]; then fi cd "$(dirname "${YAML_FILE}")" +YAML_FILE="./$(echo ${YAML_FILE} | rev | cut -d "/" -f 1 | rev)" parseYaml ${YAML_FILE} ${DEFAULT_GIT_CLONE_PATH} From 1ccd1f4de8f61f915df5ea99b24b68bb7b19e7cb Mon Sep 17 00:00:00 2001 From: Bobonium Date: Sun, 27 Oct 2019 11:56:37 +0100 Subject: [PATCH 2/4] ignore comments in gitfile --- gitfile.sh | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/gitfile.sh b/gitfile.sh index 6261bfd..477e78c 100755 --- a/gitfile.sh +++ b/gitfile.sh @@ -25,11 +25,11 @@ function cloneRepo function parseYaml { - YAML_FILE=${1} + YAML_FILE_CONTENT=$(cat ${1} | egrep -v "^\s*#") DEFAULT_GIT_CLONE_PATH=${2} - LAST_LINE_NUMBER="$(wc -l ${YAML_FILE} | awk '{print $1}')" - mapfile -t LINE_NUMBERS < <(cat -n ${YAML_FILE} | egrep -v "source:|version:|path:" | egrep "[0-9]{1,10}.*:\s*$" | awk '{print $1}') - mapfile -t DIR_NAMES < <(cat -n ${YAML_FILE} | egrep -v "source:|version:|path:" | egrep "[0-9]{1,10}.*:\s*$" | awk '{print $2}') + LAST_LINE_NUMBER="$(echo "${YAML_FILE_CONTENT}" | wc -l | awk '{print $1}')" + mapfile -t LINE_NUMBERS < <(echo "${YAML_FILE_CONTENT}" | cat -n | egrep -v "source:|version:|path:" | egrep "[0-9]{1,10}.*:\s*$" | awk '{print $1}') + mapfile -t DIR_NAMES < <(echo "${YAML_FILE_CONTENT}" | cat -n | egrep -v "source:|version:|path:" | egrep "[0-9]{1,10}.*:\s*$" | awk '{print $2}') for (( i=0; i < ${#LINE_NUMBERS[@]}; ++i )) do FROM=$(expr ${LINE_NUMBERS[$i]} + 1) @@ -37,9 +37,9 @@ function parseYaml if [ "$i" -ne "$(expr ${#LINE_NUMBERS[@]} - 1 )" ]; then TO=$(expr ${LINE_NUMBERS[$i + 1]} - 1) fi - SOURCE=$(sed -n "${FROM},${TO}p" ${YAML_FILE} | grep "source:" | awk '{print $2}' | cut -d'"' -f2) - VERSION=$(sed -n "${FROM},${TO}p" ${YAML_FILE} | grep "version:" | awk '{print $2}' | cut -d'"' -f2 || echo "master") - GIT_CLONE_PATH=$(sed -n "${FROM},${TO}p" ${YAML_FILE} | grep "path:" | awk '{print $2}' | cut -d'"' -f2 || echo "${DEFAULT_GIT_CLONE_PATH}") + SOURCE=$(echo "${YAML_FILE_CONTENT}" | sed -n "${FROM},${TO}p" | grep "source:" | awk '{print $2}' | cut -d'"' -f2) + VERSION=$(echo "${YAML_FILE_CONTENT}" | sed -n "${FROM},${TO}p" | grep "version:" | awk '{print $2}' | cut -d'"' -f2 || echo "master") + GIT_CLONE_PATH=$(echo "${YAML_FILE_CONTENT}" | sed -n "${FROM},${TO}p" | grep "path:" | awk '{print $2}' | cut -d'"' -f2 || echo "${DEFAULT_GIT_CLONE_PATH}") cloneRepo ${SOURCE} ${VERSION} ${GIT_CLONE_PATH%/}/${DIR_NAMES[$i]%:} done } From 8eb03b6a040aff5945925d4a55a80396fe5398ca Mon Sep 17 00:00:00 2001 From: Bobonium Date: Sun, 27 Oct 2019 11:57:41 +0100 Subject: [PATCH 3/4] add echo output of 'source' repo to be able to see progress --- gitfile.sh | 1 + 1 file changed, 1 insertion(+) diff --git a/gitfile.sh b/gitfile.sh index 477e78c..a859d2e 100755 --- a/gitfile.sh +++ b/gitfile.sh @@ -7,6 +7,7 @@ function cloneRepo SOURCE=${1} VERSION=${2} GIT_CLONE_PATH=${3//\~/$HOME} + echo "${SOURCE}" if [ ! -d "${GIT_CLONE_PATH}" ]; then git clone "${SOURCE}" ${GIT_CLONE_PATH} -q fi From 181cb64f83c7a357d0542cb0d61efcc981ad3058 Mon Sep 17 00:00:00 2001 From: Bobonium Date: Sun, 27 Oct 2019 11:58:54 +0100 Subject: [PATCH 4/4] added argument parsing and help text --- gitfile.sh | 37 ++++++++++++++++++++++++++++++------- 1 file changed, 30 insertions(+), 7 deletions(-) diff --git a/gitfile.sh b/gitfile.sh index a859d2e..0a13bc1 100755 --- a/gitfile.sh +++ b/gitfile.sh @@ -2,6 +2,21 @@ set -euo pipefail +function printHelpText +{ + echo "Usage:" + echo " ${0} [OPTIONS]" + echo "" + echo "application Options:" + echo " -p, --default-clone-path= Default path to git clone into (default: ./)" + echo " -f, --gitfile= File path to the Gitfile (default: ./gitfile)" + echo "" + echo "Help Options:" + echo " -h, --help Show this help message and exit" + echo "" + exit 0 +} + function cloneRepo { SOURCE=${1} @@ -46,15 +61,23 @@ function parseYaml } DEFAULT_GIT_CLONE_PATH="." -if [ "$#" -eq 1 ]; then - DEFAULT_GIT_CLONE_PATH=${1%/} -fi +YAML_FILE="./.gitfile" +while [ "$#" -gt 0 ]; do + case "$1" in + -p) DEFAULT_GIT_CLONE_PATH="${2%/}"; shift 2;; + -f) YAML_FILE="$2"; shift 2;; + --default-clone-path=*) DEFAULT_GIT_CLONE_PATH="${1#*=}"; shift 1;; + --gitfile=*) YAML_FILE="${1#*=}"; shift 1;; + --default-clone-path|--gitfile) echo "$1 requires an argument" >&2; exit 1;; -YAML_FILE="./.gitfile" -if [ "$#" -eq 2 ]; then - YAML_FILE="${2}" -fi + -h) printHelpText;; + --help) printHelpText;; + + -*) echo "unknown option: $1" >&2; exit 1;; + *) handle_argument "$1"; shift 1;; + esac +done if [ ! -f "${YAML_FILE}" ]; then echo "[ERROR] '${YAML_FILE}' does not exist"