Skip to content

Commit

Permalink
Test verify global vars
Browse files Browse the repository at this point in the history
  • Loading branch information
fabasoad committed Jun 29, 2024
1 parent 609e110 commit ff36262
Show file tree
Hide file tree
Showing 8 changed files with 80 additions and 19 deletions.
4 changes: 2 additions & 2 deletions src/lib/args/parse-hook-args.sh
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ UTILS_DIR_PATH="${LIB_DIR_PATH}/utils"

_set_log_level() {
log_level="$1"
is_valid=$(validate_enum "--log-level" "${log_level}" "off,debug,info,warning,error")
is_valid=$(validate_log_level "${log_level}")
if [ "${is_valid}" = "true" ]; then
GLOB_LOG_LEVEL="${log_level}"
fi
Expand Down Expand Up @@ -44,7 +44,7 @@ parse_hook_args() {
args_str=$(_set_param "log_level" "${args_str}" " ")
;;
*)
log_warning "Unknown $1 argument has been passed as --hook-args"
log_warning "Unknown ${args_str} argument has been passed as --hook-args"
;;
esac
shift
Expand Down
3 changes: 0 additions & 3 deletions src/lib/default.sh

This file was deleted.

3 changes: 3 additions & 0 deletions src/lib/global-vars/default.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/usr/bin/env sh

PRE_COMMIT_GRYPE_LOG_LEVEL_DEFAULT_VAL="info"
16 changes: 16 additions & 0 deletions src/lib/global-vars/set.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#!/usr/bin/env sh

MAIN_SCRIPT_PATH=$(realpath "$0")
SRC_DIR_PATH=$(dirname "${MAIN_SCRIPT_PATH}")
LIB_DIR_PATH="${SRC_DIR_PATH}/lib"
ENV_VARS_DIR_PATH="${LIB_DIR_PATH}/env-vars"

. "${ENV_VARS_DIR_PATH}/default.sh"

set_global_log_level() {
export PRE_COMMIT_GRYPE_LOG_LEVEL="$1"
}

reset_global_log_level() {
set_log_level "${PRE_COMMIT_GRYPE_LOG_LEVEL_DEFAULT_VAL}"
}
32 changes: 32 additions & 0 deletions src/lib/global-vars/verify.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#!/usr/bin/env sh

MAIN_SCRIPT_PATH=$(realpath "$0")
SRC_DIR_PATH=$(dirname "${MAIN_SCRIPT_PATH}")
LIB_DIR_PATH="${SRC_DIR_PATH}/lib"
GLOBAL_VARS_DIR_PATH="${LIB_DIR_PATH}/global-vars"
UTILS_DIR_PATH="${LIB_DIR_PATH}/utils"

. "${GLOBAL_VARS_DIR_PATH}/default.sh"
. "${GLOBAL_VARS_DIR_PATH}/set.sh"
. "${UTILS_DIR_PATH}/logging.sh"
. "${UTILS_DIR_PATH}/validators.sh"

_verify_log_level() {
if [ -z "${PRE_COMMIT_GRYPE_LOG_LEVEL}" ]; then
reset_global_log_level
else
opts="off,debug,info,warning,error"
is_valid=$(validate_enum "PRE_COMMIT_GRYPE_LOG_LEVEL" "${PRE_COMMIT_GRYPE_LOG_LEVEL}" "${opts}" "off")
if [ "${is_valid}" = "false" ]; then
msg="\"PRE_COMMIT_GRYPE_LOG_LEVEL\" environment variable is invalid. Possible"
msg="${msg} values: $(echo "${opts%,}" | sed 's/,/, /g'). Setting back"
msg="${msg} to default value: \"${PRE_COMMIT_GRYPE_LOG_LEVEL_DEFAULT_VAL}\"."
log_warning "${msg}"
reset_global_log_level
fi
fi
}

verify_global_vars() {
_verify_log_level
}
2 changes: 2 additions & 0 deletions src/lib/utils/logging.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ log() {
printf "%s %s level=%s %s\n" "$prefix" "$(date +'%Y-%m-%d %T')" "$level" "$msg" >&2
}

log_off() {}

log_debug() {
if [ "$(is_debug_ok)" = "true" ]; then
log "debug" "$1"
Expand Down
4 changes: 4 additions & 0 deletions src/lib/utils/validators.sh
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,7 @@ validate_enum() {
;;
esac
}

validate_log_level() {
validate_enum "--log-level" "$1" "off,debug,info,warning,error"
}
35 changes: 21 additions & 14 deletions src/main.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,32 @@ MAIN_SCRIPT_PATH=$(realpath "$0")
SRC_DIR_PATH=$(dirname "${MAIN_SCRIPT_PATH}")
HOOKS_DIR_PATH="${SRC_DIR_PATH}/hooks"
LIB_DIR_PATH="${SRC_DIR_PATH}/lib"
GLOBAL_VARS_DIR_PATH="${LIB_DIR_PATH}/global-vars"
UTILS_DIR_PATH="${LIB_DIR_PATH}/utils"

. "${LIB_DIR_PATH}/default.sh"
. "${GLOBAL_VARS_DIR_PATH}/verify.sh"
. "${UTILS_DIR_PATH}/logging.sh"
. "${UTILS_DIR_PATH}/validators.sh"

cmd_grype_dir="grype-dir"
cmd_actual="$1"
main() {
verify_global_vars

. "${HOOKS_DIR_PATH}/${cmd_actual}.sh"
shift
if [ "${cmd_actual}" = "${cmd_grype_dir}" ]; then
grype_dir "$(echo "$@" | sed 's/^ *//')"
else
is_valid=$(validate_enum "hook" "${cmd_actual}" "${cmd_grype_dir}" "error")
if [ "${is_valid}" = "false" ]; then
exit 1
cmd_grype_dir="grype-dir"
cmd_actual="$1"

. "${HOOKS_DIR_PATH}/${cmd_actual}.sh"
shift
if [ "${cmd_actual}" = "${cmd_grype_dir}" ]; then
grype_dir "$(echo "$@" | sed 's/^ *//')"
else
log_error "Something went wrong"
exit 1
is_valid=$(validate_enum "hook" "${cmd_actual}" "${cmd_grype_dir}" "error")
if [ "${is_valid}" = "false" ]; then
exit 1
else
log_error "Something went wrong"
exit 1
fi
fi
fi
}

main "$@"

0 comments on commit ff36262

Please sign in to comment.