From dc6464ab543f87c87f916c0629925f4de71690c5 Mon Sep 17 00:00:00 2001 From: ayakael Date: Sat, 7 Apr 2018 20:31:51 -0400 Subject: [PATCH] [parser] Added --git-dir option. Otherwise, GIT_DIR is defined as the current working directory [cmd_deploy] [cmd_du] [cmd_exclude] [cmd_fsck] [cmd_include] [cmd_update] Added checks to ensure DB_FILE and GIT_DIR exist --- src/cmd_deploy | 1 + src/cmd_du | 4 ++- src/cmd_exclude | 3 ++- src/cmd_fsck | 2 ++ src/cmd_include | 2 ++ src/cmd_update | 2 ++ src/parser | 65 +++++++++++++++++++++++++++++++++++++++++-------- 7 files changed, 67 insertions(+), 12 deletions(-) diff --git a/src/cmd_deploy b/src/cmd_deploy index 48998a4..fecd830 100644 --- a/src/cmd_deploy +++ b/src/cmd_deploy @@ -16,6 +16,7 @@ cmd_deploy() { local ERR=false local DB_FILE="${TARGET}/${_OPT_DB_FILE}" [[ ! -f "${DB_FILE}" ]] && return 1 + [[ ! -f "${GIT_DIR}/.git" ]] && return 2 if [[ -z "$(grep LAST_DEPLOY ${DB_FILE})" ]]; then echo -e "LAST_DEPLOY\t$(git -C "${GIT_DIR}" rev-list HEAD | tail -n 1)" >> ${DB_FILE} diff --git a/src/cmd_du b/src/cmd_du index 7a6b02b..b3787f1 100644 --- a/src/cmd_du +++ b/src/cmd_du @@ -14,7 +14,9 @@ cmd_du() { local GIT_DIR="${1}" local TARGET="${2}" local DB_FILE="${TARGET}/${_OPT_DB_FILE}" - + [[ ! -f "${DB_FILE}" ]] && return 1 + [[ ! -f "${GIT_DIR}/.git" ]] && return 2 + local imageidList=($(awk 'BEGIN{FS="\t"}{if($1=="true"){print $2}}' ${DB_FILE} | awk '!seen[$0]++')) local DU="$(print_imageid_du ${GIT_DIR} ${imageidList[@]})" _msg ECHO "Current estimated disk usage: ${DU} bytes" diff --git a/src/cmd_exclude b/src/cmd_exclude index a61f375..332c4a6 100644 --- a/src/cmd_exclude +++ b/src/cmd_exclude @@ -20,7 +20,8 @@ cmd_exclude() { local TARGET="${2}" local DB_FILE="${TARGET}/${_OPT_DB_FILE}"; shift 2 local conditionList=("${@}") - echo ${conditionList[@]} + [[ ! -f "${DB_FILE}" ]] && return 1 + [[ ! -f "${GIT_DIR}/.git" ]] && return 2 for condition in ${conditionList[@]}; do _msg EXEC "Excluding all tracks that match the following condition: ${condition}" diff --git a/src/cmd_fsck b/src/cmd_fsck index 9ad24b1..cea1f18 100644 --- a/src/cmd_fsck +++ b/src/cmd_fsck @@ -14,6 +14,8 @@ cmd_fsck() { local GIT_DIR="${1}" local TARGET="${2}" local DB_FILE="${TARGET}/${_OPT_DB_FILE}" + [[ ! -f "${DB_FILE}" ]] && return 1 + [[ ! -f "${GIT_DIR}/.git" ]] && return 2 chk_deployed_ids "${TARGET}" "${DB_FILE}" chk_nonexistent_ids "${TARGET}" "${DB_FILE}" diff --git a/src/cmd_include b/src/cmd_include index 31c3cfd..0614c19 100644 --- a/src/cmd_include +++ b/src/cmd_include @@ -20,6 +20,8 @@ cmd_include() { local TARGET="${2}" local DB_FILE="${TARGET}/${_OPT_DB_FILE}"; shift 2 local conditionList=("${@}") + [[ ! -f "${DB_FILE}" ]] && return 1 + [[ ! -f "${GIT_DIR}/.git" ]] && return 2 for condition in ${conditionList[@]}; do _msg EXEC "Including all tracks that match the following condition: ${condition}" diff --git a/src/cmd_update b/src/cmd_update index 33e120b..bd392f9 100644 --- a/src/cmd_update +++ b/src/cmd_update @@ -14,6 +14,8 @@ cmd_update() { local GIT_DIR="${1}" local TARGET="${2}" local DB_FILE="${TARGET}/${_OPT_DB_FILE}" + [[ ! -f "${DB_FILE}" ]] && return 1 + [[ ! -f "${GIT_DIR}/.git" ]] && return 2 # In the event that LAST_COMMIT OR DB_FILE does not exist, echo out that the LAST_COMMIT # is the first COMMIT of GIT_DIR, thus stating that no database update has ever occured diff --git a/src/parser b/src/parser index b70cb45..a0463ab 100644 --- a/src/parser +++ b/src/parser @@ -4,35 +4,80 @@ # Parses arguments and commands from shell ### +GIT_DIR="./" + +#Recursive argument parser +while true; do + case ${1} in + --help) + help + exit + ;; + + --info) + help info + exit + ;; + + + --git-dir=*) + if [ -z "${1#*=}" ]; then + help + else + log ECHO "--git-dir called. Git directory now defined as: ${1#*=}" + GIT_DIR="${1#*=}" + fi + + ;; + *) + break + ;; + + esac + shift +done + + + case "${1}" in exclude) shift - cmd_exclude ${@} + cmd_exclude "${GIT_DIR}" ${@} + [[ $? -eq 1 ]] && echo "Database file non-existent" + [[ $? -eq 2 ]] && echo "Git directory not a valid git repository" ;; include) shift - cmd_include ${@} - ;; + cmd_include$ "${GIT_DIR}" ${@} + [[ $? -eq 1 ]] && echo "Database file non-existent" + [[ $? -eq 2 ]] && echo "Git directory not a valid git repository" + ;; deploy) shift - cmd_deploy ${@} - ;; + cmd_deploy "${GIT_DIR}" ${@} + [[ $? -eq 1 ]] && echo "Database file non-existent" + [[ $? -eq 2 ]] && echo "Git directory not a valid git repository" + ;; update) shift - cmd_update ${@} - ;; + cmd_update "${GIT_DIR}" ${@} + [[ $? -eq 1 ]] && echo "Database file non-existent" + [[ $? -eq 2 ]] && echo "Git directory not a valid git repository" + ;; du) shift - cmd_du "${@}" - ;; + cmd_du "${GIT_DIR}" "${@}" + [[ $? -eq 1 ]] && echo "Database file non-existent" + [[ $? -eq 2 ]] && echo "Git directory not a valid git repository" + ;; fsck) shift - cmd_fsck "${@}" + cmd_fsck "${GIT_DIR}" "${@}" ;; esac