From b5bc1a3d2f73542d614f5b01f502514ea060480a Mon Sep 17 00:00:00 2001 From: fabasoad Date: Thu, 11 Jul 2024 08:59:14 +0900 Subject: [PATCH 1/5] Add logs --- .bpkg.lock | 1 + .gitignore | 1 + Makefile | 5 ++ README.md | 2 - scripts/bpkg-install-packages.sh | 13 ++++++ src/lib/args/parse-all-args.sh | 2 +- src/lib/args/parse-hook-args.sh | 16 +++++-- src/lib/base/grype-common.sh | 14 +++--- src/lib/global-vars/modifiers.sh | 11 ++++- src/lib/global-vars/verify.sh | 11 +++-- src/lib/installation/install.sh | 10 ++-- src/lib/installation/uninstall.sh | 2 +- src/lib/utils/logging.sh | 77 ------------------------------- src/lib/utils/validators.sh | 20 ++++++-- src/main.sh | 36 ++++++++++++--- 15 files changed, 107 insertions(+), 114 deletions(-) create mode 100644 .bpkg.lock create mode 100644 Makefile create mode 100755 scripts/bpkg-install-packages.sh delete mode 100755 src/lib/utils/logging.sh diff --git a/.bpkg.lock b/.bpkg.lock new file mode 100644 index 0000000..7563a93 --- /dev/null +++ b/.bpkg.lock @@ -0,0 +1 @@ +fabasoad/sh-logging@v0.1.0 diff --git a/.gitignore b/.gitignore index 8c9578d..9a61e1e 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,5 @@ .DS_Store +.bpkg .gitleaks.toml .vscode/* !.vscode/settings.json diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..dfea035 --- /dev/null +++ b/Makefile @@ -0,0 +1,5 @@ +.PHONY: install + +install: + @./scripts/bpkg-install-packages.sh + @echo "[pre-commit-grype] Operation completed successfully: install" diff --git a/README.md b/README.md index 8c35f89..50f5969 100644 --- a/README.md +++ b/README.md @@ -5,8 +5,6 @@ ![security](https://github.com/fabasoad/pre-commit-grype/actions/workflows/security.yml/badge.svg) ![linting](https://github.com/fabasoad/pre-commit-grype/actions/workflows/linting.yml/badge.svg) -1. [grype-dir](#grype-dir) - ## Table of Contents - [How it works?](#how-it-works) diff --git a/scripts/bpkg-install-packages.sh b/scripts/bpkg-install-packages.sh new file mode 100755 index 0000000..90bbce9 --- /dev/null +++ b/scripts/bpkg-install-packages.sh @@ -0,0 +1,13 @@ +#!/usr/bin/env sh + +export PREFIX="$(pwd)/src/.bpkg" + +main() { + mkdir -p "${PREFIX}" + lock_file="./.bpkg.lock" + while IFS= read -r line; do + bpkg install "$line" + done < "${lock_file}" +} + +main "$@" diff --git a/src/lib/args/parse-all-args.sh b/src/lib/args/parse-all-args.sh index 9c29396..e1209e6 100755 --- a/src/lib/args/parse-all-args.sh +++ b/src/lib/args/parse-all-args.sh @@ -32,7 +32,7 @@ parse_all_args() { msg="Invalid format of the following argument: \"${arg}\". Please use" msg="${msg} --hook-args to pass args to pre-commit hook or --grype-args" msg="${msg} to pass args to grype. For more information go to https://github.com/fabasoad/pre-commit-grype?tab=readme-ov-file" - log_error "${msg}" + fabasoad_log "error" "${msg}" exit 1 fi diff --git a/src/lib/args/parse-hook-args.sh b/src/lib/args/parse-hook-args.sh index 20d9d98..6171648 100755 --- a/src/lib/args/parse-hook-args.sh +++ b/src/lib/args/parse-hook-args.sh @@ -1,6 +1,9 @@ #!/usr/bin/env sh _set_param() { + local -n logs_map_ref=$1 + shift + set_param_func_name="set_global_$1" args_str="$2" delimiter="$3" @@ -10,7 +13,7 @@ _set_param() { param_val=$(echo "${args_str}" | cut -d ' ' -f 1) # Saving leftover args_str=$(echo "${args_str}" | cut -d ' ' -f 2-) - ${set_param_func_name} "${param_val}" + ${set_param_func_name} logs_map_ref "${param_val}" if [ "${param_val}" = "${args_str}" ]; then echo "" else @@ -19,23 +22,26 @@ _set_param() { } parse_hook_args() { + local -n logs_map_ref=$1 + shift + args_str="$1" if [ -n "${args_str}" ]; then orig_str="${args_str}" while [ ${#args_str} -gt 0 ]; do case "${args_str}" in "${CONFIG_LOG_LEVEL_ARG_NAME}="*) - args_str=$(_set_param "log_level" "${args_str}" "=") + args_str=$(_set_param logs_map_ref "log_level" "${args_str}" "=") ;; "${CONFIG_LOG_LEVEL_ARG_NAME} "*) - args_str=$(_set_param "log_level" "${args_str}" " ") + args_str=$(_set_param logs_map_ref "log_level" "${args_str}" " ") ;; *) - log_warning "Unknown ${args_str} argument has been passed as --hook-args" + logs_map_ref["warning"]="Unknown ${args_str} argument has been passed as --hook-args" ;; esac shift done - log_info "Pre-commit hook arguments: ${orig_str}" + logs_map_ref["info"]="Pre-commit hook arguments: ${orig_str}" fi } diff --git a/src/lib/base/grype-common.sh b/src/lib/base/grype-common.sh index e857f68..71fea21 100755 --- a/src/lib/base/grype-common.sh +++ b/src/lib/base/grype-common.sh @@ -8,21 +8,21 @@ grype_common() { grype_path=$(install) grype_version=$(${grype_path} --version | cut -d ' ' -f 2) - log_info "Grype path: ${grype_path}" - log_info "Grype version: ${grype_version}" - log_info "Grype arguments: ${grype_args}" + fabasoad_log "info" "Grype path: ${grype_path}" + fabasoad_log "info" "Grype version: ${grype_version}" + fabasoad_log "info" "Grype arguments: ${grype_args}" - log_debug "Run Grype scanning:" + fabasoad_log "debug" "Run Grype scanning:" set +e ${grype_path} ${grype_args} grype_exit_code=$? set -e - log_debug "Grype scanning completed" + fabasoad_log "debug" "Grype scanning completed" msg="Grype exit code: ${grype_exit_code}" if [ "${grype_exit_code}" = "0" ]; then - log_info "${msg}" + fabasoad_log "info" "${msg}" else - log_warning "${msg}" + fabasoad_log "warning" "${msg}" fi uninstall "${CONFIG_TEMP_DIR}" diff --git a/src/lib/global-vars/modifiers.sh b/src/lib/global-vars/modifiers.sh index ba77924..2f7347e 100755 --- a/src/lib/global-vars/modifiers.sh +++ b/src/lib/global-vars/modifiers.sh @@ -15,14 +15,21 @@ get_global_log_level() { set -u } +# This function is called by the name dynamically also, such as set_global_${str} set_global_log_level() { + local -n logs_map_ref=$1 + shift + log_level="$1" - is_valid=$(validate_log_level_param "${log_level}") + is_valid=$(validate_log_level_param logs_map_ref "${log_level}") if [ "${is_valid}" = "true" ]; then save_prop "PRE_COMMIT_GRYPE_LOG_LEVEL" "${log_level}" fi } reset_global_log_level() { - set_global_log_level "${CONFIG_LOG_LEVEL_DEFAULT_VAL}" + local -n logs_map_ref=$1 + shift + + set_global_log_level logs_map_ref "${CONFIG_LOG_LEVEL_DEFAULT_VAL}" } diff --git a/src/lib/global-vars/verify.sh b/src/lib/global-vars/verify.sh index 5ef8f5e..fb9f755 100755 --- a/src/lib/global-vars/verify.sh +++ b/src/lib/global-vars/verify.sh @@ -1,18 +1,21 @@ #!/usr/bin/env sh _verify_log_level() { + local -n logs_map_ref=$1 + shift + env_var_name="PRE_COMMIT_GRYPE_LOG_LEVEL" env_var_val="$(get_global_log_level)" - is_valid=$(validate_log_level_global_var "${env_var_val}" "off") + is_valid=$(validate_log_level_global_var logs_map_ref "${env_var_val}" "off") if [ "${is_valid}" = "false" ]; then msg="\"${env_var_name}\" environment variable is invalid (\"${env_var_val}\")." msg="${msg} Possible values: $(echo "${CONFIG_LOG_LEVEL_OPTIONS%,}" | sed 's/,/, /g')." msg="${msg} Resetting to default value: \"${CONFIG_LOG_LEVEL_DEFAULT_VAL}\"." - log_warning "${msg}" - reset_global_log_level + logs_map_ref["warning"]="${msg}" + reset_global_log_level logs_map_ref fi } verify_global_vars() { - _verify_log_level + _verify_log_level "$@" } diff --git a/src/lib/installation/install.sh b/src/lib/installation/install.sh index 65e655e..b2625f6 100755 --- a/src/lib/installation/install.sh +++ b/src/lib/installation/install.sh @@ -1,19 +1,19 @@ #!/usr/bin/env sh install() { - log_debug "Verifying Grype installation" + fabasoad_log "debug" "Verifying Grype installation" if command -v grype &> /dev/null; then grype_path="$(which grype)" - log_debug "Grype is found at ${grype_path}. Installation skipped" + fabasoad_log "debug" "Grype is found at ${grype_path}. Installation skipped" else grype_path="${CONFIG_TEMP_BIN_DIR}/grype" mkdir -p "${CONFIG_TEMP_BIN_DIR}" if [ ! -d "${CONFIG_TEMP_BIN_DIR}" ] || [ ! -f "${grype_path}" ]; then - log_debug "Grype is not found. Downloading latest version:" + fabasoad_log "debug" "Grype is not found. Downloading latest version:" curl -sSfL https://raw.githubusercontent.com/anchore/grype/main/install.sh | sh -s -- -b "${CONFIG_TEMP_BIN_DIR}" - log_debug "Downloading completed" + fabasoad_log "debug" "Downloading completed" else - log_debug "Grype is found at ${grype_path}. Installation skipped" + fabasoad_log "debug" "Grype is found at ${grype_path}. Installation skipped" fi fi echo "${grype_path}" diff --git a/src/lib/installation/uninstall.sh b/src/lib/installation/uninstall.sh index 2bbbbef..b989186 100755 --- a/src/lib/installation/uninstall.sh +++ b/src/lib/installation/uninstall.sh @@ -1,6 +1,6 @@ #!/usr/bin/env sh uninstall() { - log_debug "${CONFIG_TEMP_DIR} directory has been removed" + fabasoad_log "debug" "${CONFIG_TEMP_DIR} directory has been removed" rm -rf "${CONFIG_TEMP_DIR}" } diff --git a/src/lib/utils/logging.sh b/src/lib/utils/logging.sh deleted file mode 100755 index 12b5cc7..0000000 --- a/src/lib/utils/logging.sh +++ /dev/null @@ -1,77 +0,0 @@ -#!/usr/bin/env sh - -_is_debug_ok() { - # ok: debug - # not ok: off, info, warning, error - if [ "$(get_global_log_level)" = "debug" ]; then - echo "true" - else - echo "false" - fi -} - -_is_info_ok() { - # ok: debug, info - # not ok: off, warning, error - if [ "$(get_global_log_level)" = "debug" ] || [ "$(get_global_log_level)" = "info" ]; then - echo "true" - else - echo "false" - fi -} - -_is_warning_ok() { - # ok: debug, info, warning - # not ok: off, error - if [ "$(get_global_log_level)" != "error" ] && [ "$(get_global_log_level)" != "off" ]; then - echo "true" - else - echo "false" - fi -} - -_is_error_ok() { - # ok: debug, info, warning, error - # not ok: off - if [ "$(get_global_log_level)" != "off" ]; then - echo "true" - else - echo "false" - fi -} - -_log() { - prefix="[pre-commit-grype]" - level=$1 - msg=$2 - - 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" - fi -} - -log_info() { - if [ "$(_is_info_ok)" = "true" ]; then - _log "info" "$1" - fi -} - -log_warning() { - if [ "$(_is_warning_ok)" = "true" ]; then - _log "warning" "$1" - fi -} - -log_error() { - if [ "$(_is_error_ok)" = "true" ]; then - _log "error" "$1" - fi -} diff --git a/src/lib/utils/validators.sh b/src/lib/utils/validators.sh index d33a0af..359fff9 100755 --- a/src/lib/utils/validators.sh +++ b/src/lib/utils/validators.sh @@ -1,6 +1,9 @@ #!/usr/bin/env sh validate_enum() { + local -n logs_map_ref=$1 + shift + param_key="$1" param_val="$2" enum_opts="$3," @@ -10,27 +13,36 @@ validate_enum() { echo "true" ;; *) - log_${log_level} "\"${param_key}\" parameter is invalid. Possible values: $(echo "${enum_opts%,}" | sed 's/,/, /g')." + logs_map_ref["${log_level}"]="\"${param_key}\" parameter is invalid. Possible values: $(echo "${enum_opts%,}" | sed 's/,/, /g')." echo "false" ;; esac } _validate_log_level() { + local -n logs_map_ref=$1 + shift + param_key="${1}" param_val="${2}" log_level="${3:-warning}" - validate_enum "${param_key}" "${param_val}" "${CONFIG_LOG_LEVEL_OPTIONS}" "${log_level}" + validate_enum logs_map_ref "${param_key}" "${param_val}" "${CONFIG_LOG_LEVEL_OPTIONS}" "${log_level}" } validate_log_level_param() { + local -n logs_map_ref=$1 + shift + param_val="${1}" log_level="${2:-warning}" - _validate_log_level "${CONFIG_LOG_LEVEL_ARG_NAME}" "${param_val}" "${log_level}" + _validate_log_level logs_map_ref "${CONFIG_LOG_LEVEL_ARG_NAME}" "${param_val}" "${log_level}" } validate_log_level_global_var() { + local -n logs_map_ref=$1 + shift + param_val="${1}" log_level="${2:-warning}" - _validate_log_level "PRE_COMMIT_GRYPE_LOG_LEVEL" "${param_val}" "${log_level}" + _validate_log_level logs_map_ref "PRE_COMMIT_GRYPE_LOG_LEVEL" "${param_val}" "${log_level}" } diff --git a/src/main.sh b/src/main.sh index c0d954b..ad2f228 100755 --- a/src/main.sh +++ b/src/main.sh @@ -1,10 +1,20 @@ #!/usr/bin/env bash +# Global variables + + +_global_config() { + export FABASOAD_LOG_CONFIG_TEXT_COLOR="true" + export FABASOAD_LOG_CONFIG_OUTPUT_FORMAT="text" + export FABASOAD_LOG_CONFIG_HEADER="pre-commit-grype" + export FABASOAD_LOG_CONFIG_LOG_LEVEL="$(get_global_log_level)" +} + # Import all scripts _import_all() { current_file=$(basename "$0") - sh_files=$(find "$(dirname "$(realpath "$0")")" -type f -name "*.sh") - for file in $sh_files; do + exec_files=$(find "src" -type f -perm +111) + for file in $exec_files; do if [ "$(basename "${file}")" != "${current_file}" ]; then . "${file}" fi @@ -21,20 +31,34 @@ main() { declare -A args_map parse_all_args args_map "$(echo "$@" | sed 's/^ *//' | sed 's/ *$//')" - parse_hook_args "${args_map["hook-args"]}" + declare -A logs_map + parse_hook_args logs_map "${args_map["hook-args"]}" - verify_global_vars + # Need to verify parameters that were parsed from hook args + verify_global_vars logs_map + # Setting global config, such as logging + _global_config + # Print logs from map if any + for log_level in "${!logs_map[@]}"; do + fabasoad_log "${log_level}" "${logs_map[$log_level]}" + done case "${cmd_actual}" in "${cmd_grype_dir}") grype_dir "${args_map["grype-args"]}" ;; *) - is_valid=$(validate_enum "hook" "${cmd_actual}" "${cmd_grype_dir}" "error") + declare -A validation_logs_map + is_valid=$(validate_enum validation_logs_map "hook" "${cmd_actual}" "${cmd_grype_dir}" "error") + # Print logs from map if any + for log_level in "${!validation_logs_map[@]}"; do + fabasoad_log "${log_level}" "${validation_logs_map[$log_level]}" + done + if [ "${is_valid}" = "false" ]; then exit 1 else - log_error "Something went wrong" + fabasoad_log "error" "Something went wrong" exit 1 fi ;; From 53f8e56f9b35b6d0a23376eb6316df8e054c9d58 Mon Sep 17 00:00:00 2001 From: fabasoad Date: Sat, 13 Jul 2024 14:56:36 +0900 Subject: [PATCH 2/5] Fix loop issue --- src/lib/args/parse-hook-args.sh | 9 +++------ src/lib/global-vars/modifiers.sh | 10 ++-------- src/lib/global-vars/verify.sh | 13 +++---------- src/lib/utils/validators.sh | 19 +++---------------- src/main.sh | 5 ++--- 5 files changed, 13 insertions(+), 43 deletions(-) diff --git a/src/lib/args/parse-hook-args.sh b/src/lib/args/parse-hook-args.sh index 6171648..a605438 100755 --- a/src/lib/args/parse-hook-args.sh +++ b/src/lib/args/parse-hook-args.sh @@ -1,9 +1,6 @@ #!/usr/bin/env sh _set_param() { - local -n logs_map_ref=$1 - shift - set_param_func_name="set_global_$1" args_str="$2" delimiter="$3" @@ -13,7 +10,7 @@ _set_param() { param_val=$(echo "${args_str}" | cut -d ' ' -f 1) # Saving leftover args_str=$(echo "${args_str}" | cut -d ' ' -f 2-) - ${set_param_func_name} logs_map_ref "${param_val}" + ${set_param_func_name} "${param_val}" if [ "${param_val}" = "${args_str}" ]; then echo "" else @@ -31,10 +28,10 @@ parse_hook_args() { while [ ${#args_str} -gt 0 ]; do case "${args_str}" in "${CONFIG_LOG_LEVEL_ARG_NAME}="*) - args_str=$(_set_param logs_map_ref "log_level" "${args_str}" "=") + args_str=$(_set_param "log_level" "${args_str}" "=") ;; "${CONFIG_LOG_LEVEL_ARG_NAME} "*) - args_str=$(_set_param logs_map_ref "log_level" "${args_str}" " ") + args_str=$(_set_param "log_level" "${args_str}" " ") ;; *) logs_map_ref["warning"]="Unknown ${args_str} argument has been passed as --hook-args" diff --git a/src/lib/global-vars/modifiers.sh b/src/lib/global-vars/modifiers.sh index 2f7347e..a38f8b9 100755 --- a/src/lib/global-vars/modifiers.sh +++ b/src/lib/global-vars/modifiers.sh @@ -17,19 +17,13 @@ get_global_log_level() { # This function is called by the name dynamically also, such as set_global_${str} set_global_log_level() { - local -n logs_map_ref=$1 - shift - log_level="$1" - is_valid=$(validate_log_level_param logs_map_ref "${log_level}") + is_valid=$(validate_log_level_param "${log_level}") if [ "${is_valid}" = "true" ]; then save_prop "PRE_COMMIT_GRYPE_LOG_LEVEL" "${log_level}" fi } reset_global_log_level() { - local -n logs_map_ref=$1 - shift - - set_global_log_level logs_map_ref "${CONFIG_LOG_LEVEL_DEFAULT_VAL}" + set_global_log_level "${CONFIG_LOG_LEVEL_DEFAULT_VAL}" } diff --git a/src/lib/global-vars/verify.sh b/src/lib/global-vars/verify.sh index fb9f755..3067d5a 100755 --- a/src/lib/global-vars/verify.sh +++ b/src/lib/global-vars/verify.sh @@ -1,21 +1,14 @@ #!/usr/bin/env sh _verify_log_level() { - local -n logs_map_ref=$1 - shift - env_var_name="PRE_COMMIT_GRYPE_LOG_LEVEL" env_var_val="$(get_global_log_level)" - is_valid=$(validate_log_level_global_var logs_map_ref "${env_var_val}" "off") + is_valid=$(validate_log_level_global_var "${env_var_val}" "off") if [ "${is_valid}" = "false" ]; then - msg="\"${env_var_name}\" environment variable is invalid (\"${env_var_val}\")." - msg="${msg} Possible values: $(echo "${CONFIG_LOG_LEVEL_OPTIONS%,}" | sed 's/,/, /g')." - msg="${msg} Resetting to default value: \"${CONFIG_LOG_LEVEL_DEFAULT_VAL}\"." - logs_map_ref["warning"]="${msg}" - reset_global_log_level logs_map_ref + reset_global_log_level fi } verify_global_vars() { - _verify_log_level "$@" + _verify_log_level } diff --git a/src/lib/utils/validators.sh b/src/lib/utils/validators.sh index 359fff9..691a18d 100755 --- a/src/lib/utils/validators.sh +++ b/src/lib/utils/validators.sh @@ -1,9 +1,6 @@ #!/usr/bin/env sh validate_enum() { - local -n logs_map_ref=$1 - shift - param_key="$1" param_val="$2" enum_opts="$3," @@ -13,36 +10,26 @@ validate_enum() { echo "true" ;; *) - logs_map_ref["${log_level}"]="\"${param_key}\" parameter is invalid. Possible values: $(echo "${enum_opts%,}" | sed 's/,/, /g')." echo "false" ;; esac } _validate_log_level() { - local -n logs_map_ref=$1 - shift - param_key="${1}" param_val="${2}" log_level="${3:-warning}" - validate_enum logs_map_ref "${param_key}" "${param_val}" "${CONFIG_LOG_LEVEL_OPTIONS}" "${log_level}" + validate_enum "${param_key}" "${param_val}" "${CONFIG_LOG_LEVEL_OPTIONS}" "${log_level}" } validate_log_level_param() { - local -n logs_map_ref=$1 - shift - param_val="${1}" log_level="${2:-warning}" - _validate_log_level logs_map_ref "${CONFIG_LOG_LEVEL_ARG_NAME}" "${param_val}" "${log_level}" + _validate_log_level "${CONFIG_LOG_LEVEL_ARG_NAME}" "${param_val}" "${log_level}" } validate_log_level_global_var() { - local -n logs_map_ref=$1 - shift - param_val="${1}" log_level="${2:-warning}" - _validate_log_level logs_map_ref "PRE_COMMIT_GRYPE_LOG_LEVEL" "${param_val}" "${log_level}" + _validate_log_level "PRE_COMMIT_GRYPE_LOG_LEVEL" "${param_val}" "${log_level}" } diff --git a/src/main.sh b/src/main.sh index ad2f228..fb32210 100755 --- a/src/main.sh +++ b/src/main.sh @@ -1,10 +1,9 @@ #!/usr/bin/env bash # Global variables - - _global_config() { export FABASOAD_LOG_CONFIG_TEXT_COLOR="true" + export FABASOAD_LOG_CONFIG_DATE_FORMAT="%s" export FABASOAD_LOG_CONFIG_OUTPUT_FORMAT="text" export FABASOAD_LOG_CONFIG_HEADER="pre-commit-grype" export FABASOAD_LOG_CONFIG_LOG_LEVEL="$(get_global_log_level)" @@ -35,7 +34,7 @@ main() { parse_hook_args logs_map "${args_map["hook-args"]}" # Need to verify parameters that were parsed from hook args - verify_global_vars logs_map + verify_global_vars # Setting global config, such as logging _global_config # Print logs from map if any From 01b49355d10a2856aa5daf55c5587a831bdf3d1f Mon Sep 17 00:00:00 2001 From: fabasoad Date: Sat, 13 Jul 2024 16:37:16 +0900 Subject: [PATCH 3/5] All args map --- .pre-commit-grype/props/PRE_COMMIT_GRYPE_LOG_LEVEL | 1 + src/main.sh | 8 ++++---- 2 files changed, 5 insertions(+), 4 deletions(-) create mode 100644 .pre-commit-grype/props/PRE_COMMIT_GRYPE_LOG_LEVEL diff --git a/.pre-commit-grype/props/PRE_COMMIT_GRYPE_LOG_LEVEL b/.pre-commit-grype/props/PRE_COMMIT_GRYPE_LOG_LEVEL new file mode 100644 index 0000000..d287cd3 --- /dev/null +++ b/.pre-commit-grype/props/PRE_COMMIT_GRYPE_LOG_LEVEL @@ -0,0 +1 @@ +debug diff --git a/src/main.sh b/src/main.sh index fb32210..d2d946b 100755 --- a/src/main.sh +++ b/src/main.sh @@ -28,10 +28,10 @@ main() { cmd_actual="$1" shift - declare -A args_map - parse_all_args args_map "$(echo "$@" | sed 's/^ *//' | sed 's/ *$//')" + declare -A all_args_map + parse_all_args all_args_map "$(echo "$@" | sed 's/^ *//' | sed 's/ *$//')" declare -A logs_map - parse_hook_args logs_map "${args_map["hook-args"]}" + parse_hook_args logs_map "${all_args_map["hook-args"]}" # Need to verify parameters that were parsed from hook args verify_global_vars @@ -44,7 +44,7 @@ main() { case "${cmd_actual}" in "${cmd_grype_dir}") - grype_dir "${args_map["grype-args"]}" + grype_dir "${all_args_map["grype-args"]}" ;; *) declare -A validation_logs_map From aaa00d2c396e2f31841036d408b407ab9298f0e0 Mon Sep 17 00:00:00 2001 From: fabasoad Date: Sat, 13 Jul 2024 17:59:38 +0900 Subject: [PATCH 4/5] Use latest logging lib --- .bpkg.lock | 2 +- .../props/PRE_COMMIT_GRYPE_LOG_LEVEL | 1 - .pre-commit-hooks.yaml | 2 +- README.md | 8 +-- entrypoint.sh | 6 +++ scripts/bpkg-install-packages.sh | 1 + src/lib/args/parse-all-args.sh | 16 +++--- src/lib/args/parse-hook-args.sh | 45 ++++++++--------- src/lib/config/config-logging.sh | 13 +++++ src/lib/{config.sh => config/defaults.sh} | 9 +++- src/lib/global-vars/modifiers.sh | 29 ----------- src/lib/global-vars/verify.sh | 14 ------ src/lib/utils/validators.sh | 12 +---- src/main.sh | 49 ++++++------------- 14 files changed, 81 insertions(+), 126 deletions(-) delete mode 100644 .pre-commit-grype/props/PRE_COMMIT_GRYPE_LOG_LEVEL create mode 100755 entrypoint.sh create mode 100755 src/lib/config/config-logging.sh rename src/lib/{config.sh => config/defaults.sh} (68%) delete mode 100755 src/lib/global-vars/modifiers.sh delete mode 100755 src/lib/global-vars/verify.sh diff --git a/.bpkg.lock b/.bpkg.lock index 7563a93..ee1e6c0 100644 --- a/.bpkg.lock +++ b/.bpkg.lock @@ -1 +1 @@ -fabasoad/sh-logging@v0.1.0 +fabasoad/sh-logging@v0.1.1 diff --git a/.pre-commit-grype/props/PRE_COMMIT_GRYPE_LOG_LEVEL b/.pre-commit-grype/props/PRE_COMMIT_GRYPE_LOG_LEVEL deleted file mode 100644 index d287cd3..0000000 --- a/.pre-commit-grype/props/PRE_COMMIT_GRYPE_LOG_LEVEL +++ /dev/null @@ -1 +0,0 @@ -debug diff --git a/.pre-commit-hooks.yaml b/.pre-commit-hooks.yaml index f208d58..da75195 100644 --- a/.pre-commit-hooks.yaml +++ b/.pre-commit-hooks.yaml @@ -2,7 +2,7 @@ - id: grype-dir name: Grype Dir description: Scans directory - entry: src/main.sh "grype-dir" + entry: entrypoint.sh "grype-dir" language: script pass_filenames: false verbose: true diff --git a/README.md b/README.md index 50f5969..4200673 100644 --- a/README.md +++ b/README.md @@ -148,8 +148,8 @@ Here is the precedence order of `pre-commit-grype` tool: - Environment variable. - Default value. -For example, if you set `PRE_COMMIT_GRYPE_LOG_LEVEL=off` and `--hook-args=--log-level -debug` then `debug` value will be used. +For example, if you set `PRE_COMMIT_GRYPE_LOG_LEVEL=warning` and `--hook-args=--log-level +error` then `error` value will be used. ##### Log level @@ -159,7 +159,7 @@ please look at the [Grype parameters](#grype). - Parameter name: `--log-level` - Environment variable: `PRE_COMMIT_GRYPE_LOG_LEVEL` -- Possible values: `debug`, `info`, `warning`, `error`, `off` +- Possible values: `debug`, `info`, `warning`, `error` - Default: `info` ### Examples @@ -200,6 +200,6 @@ repos: hooks: - id: grype-dir args: - - --hook-args=--log-level=off + - --hook-args=--log-level=error - --grype-args=--quiet ``` diff --git a/entrypoint.sh b/entrypoint.sh new file mode 100755 index 0000000..a21b472 --- /dev/null +++ b/entrypoint.sh @@ -0,0 +1,6 @@ +#!/usr/bin/env sh + +# Install dependencies +scripts/bpkg-install-packages.sh +# Run hook +src/main.sh "$@" diff --git a/scripts/bpkg-install-packages.sh b/scripts/bpkg-install-packages.sh index 90bbce9..09bb3be 100755 --- a/scripts/bpkg-install-packages.sh +++ b/scripts/bpkg-install-packages.sh @@ -3,6 +3,7 @@ export PREFIX="$(pwd)/src/.bpkg" main() { + rm -rf "${PREFIX}" mkdir -p "${PREFIX}" lock_file="./.bpkg.lock" while IFS= read -r line; do diff --git a/src/lib/args/parse-all-args.sh b/src/lib/args/parse-all-args.sh index e1209e6..b94a7ec 100755 --- a/src/lib/args/parse-all-args.sh +++ b/src/lib/args/parse-all-args.sh @@ -1,11 +1,11 @@ #!/usr/bin/env bash parse_all_args() { - local -n map_ref=$1 + local -n args_map_ref=$1 shift - map_ref["grype-args"]="" - map_ref["hook-args"]="" + args_map_ref["grype-args"]="" + args_map_ref["hook-args"]="" curr_flag="" @@ -25,13 +25,13 @@ parse_all_args() { *) arg=$(echo "${args}" | cut -d ' ' -f 1) if [ "${curr_flag}" = "hook" ]; then - map_ref["hook-args"]="${map_ref["hook-args"]} ${arg}" + args_map_ref["hook-args"]="${args_map_ref["hook-args"]} ${arg}" elif [ "${curr_flag}" = "grype" ]; then - map_ref["grype-args"]="${map_ref["grype-args"]} ${arg}" + args_map_ref["grype-args"]="${args_map_ref["grype-args"]} ${arg}" else msg="Invalid format of the following argument: \"${arg}\". Please use" msg="${msg} --hook-args to pass args to pre-commit hook or --grype-args" - msg="${msg} to pass args to grype. For more information go to https://github.com/fabasoad/pre-commit-grype?tab=readme-ov-file" + msg="${msg} to pass args to grype. For more information see https://github.com/fabasoad/pre-commit-grype?tab=readme-ov-file" fabasoad_log "error" "${msg}" exit 1 fi @@ -47,6 +47,6 @@ parse_all_args() { # Removing leading space is needed here because we concatenate string in a loop # and we start with a empty string. So, first iteration is empty string + space # + next value. Here we remove that empty string from the beginning - map_ref["hook-args"]=$(echo "${map_ref["hook-args"]}" | sed 's/^ *//') - map_ref["grype-args"]=$(echo "${map_ref["grype-args"]}" | sed 's/^ *//') + args_map_ref["hook-args"]=$(echo "${args_map_ref["hook-args"]}" | sed 's/^ *//') + args_map_ref["grype-args"]=$(echo "${args_map_ref["grype-args"]}" | sed 's/^ *//') } diff --git a/src/lib/args/parse-hook-args.sh b/src/lib/args/parse-hook-args.sh index a605438..22b5d1b 100755 --- a/src/lib/args/parse-hook-args.sh +++ b/src/lib/args/parse-hook-args.sh @@ -1,44 +1,41 @@ #!/usr/bin/env sh -_set_param() { - set_param_func_name="set_global_$1" - args_str="$2" - delimiter="$3" - # Removing param key, such as "--log-level" - args_str=$(echo "${args_str}" | cut -d "${delimiter}" -f 2-) - # Taking param value, such as "debug" - param_val=$(echo "${args_str}" | cut -d ' ' -f 1) - # Saving leftover - args_str=$(echo "${args_str}" | cut -d ' ' -f 2-) - ${set_param_func_name} "${param_val}" - if [ "${param_val}" = "${args_str}" ]; then - echo "" - else - echo "${args_str}" - fi -} - parse_hook_args() { - local -n logs_map_ref=$1 + local -n args_map_ref=$1 shift args_str="$1" if [ -n "${args_str}" ]; then orig_str="${args_str}" while [ ${#args_str} -gt 0 ]; do + delimiter="" case "${args_str}" in "${CONFIG_LOG_LEVEL_ARG_NAME}="*) - args_str=$(_set_param "log_level" "${args_str}" "=") + delimiter="=" ;; "${CONFIG_LOG_LEVEL_ARG_NAME} "*) - args_str=$(_set_param "log_level" "${args_str}" " ") + delimiter=" " ;; *) - logs_map_ref["warning"]="Unknown ${args_str} argument has been passed as --hook-args" + fabasoad_log "error" "Unknown \"${args_str}\" argument has been passed to --hook-args" + exit 1 ;; esac - shift + + # Removing param key, such as "--log-level" + param_key=$(echo "${args_str}" | cut -d "${delimiter}" -f 1) + args_str=$(echo "${args_str}" | cut -d "${delimiter}" -f 2-) + # Taking param value, such as "debug" + param_val=$(echo "${args_str}" | cut -d ' ' -f 1) + # Saving leftover + args_str=$(echo "${args_str}" | cut -d ' ' -f 2-) + # If leftover is the same as prev. value then it was the last argument in + # the string, so we finish loop + if [ "${param_val}" = "${args_str}" ]; then + args_str="" + fi + # Saving parameter to the map + args_map_ref["${param_key}"]="${param_val}" done - logs_map_ref["info"]="Pre-commit hook arguments: ${orig_str}" fi } diff --git a/src/lib/config/config-logging.sh b/src/lib/config/config-logging.sh new file mode 100755 index 0000000..bf44924 --- /dev/null +++ b/src/lib/config/config-logging.sh @@ -0,0 +1,13 @@ +#!/usr/bin/env sh + +apply_logging_config() { + log_level="${1:-${CONFIG_LOG_LEVEL_DEFAULT_VAL}}" + + export FABASOAD_LOG_CONFIG_TEXT_COLOR="${CONFIG_LOG_TEXT_COLOR_DEFAULT_VAL}" + export FABASOAD_LOG_CONFIG_DATE_FORMAT="${CONFIG_LOG_DATE_FORMAT_DEFAULT_VAL}" + export FABASOAD_LOG_CONFIG_OUTPUT_FORMAT="${CONFIG_LOG_OUTPUT_FORMAT_DEFAULT_VAL}" + export FABASOAD_LOG_CONFIG_HEADER="${CONFIG_LOG_HEADER_DEFAULT_VAL}" + + validate_enum "${CONFIG_LOG_LEVEL_ARG_NAME}" "${log_level}" "${CONFIG_LOG_LEVEL_OPTIONS}" + export FABASOAD_LOG_CONFIG_LOG_LEVEL="${log_level}" +} diff --git a/src/lib/config.sh b/src/lib/config/defaults.sh similarity index 68% rename from src/lib/config.sh rename to src/lib/config/defaults.sh index 8f62691..04e5e59 100755 --- a/src/lib/config.sh +++ b/src/lib/config/defaults.sh @@ -1,9 +1,16 @@ #!/usr/bin/env sh +# Logging CONFIG_LOG_LEVEL_ARG_NAME="--log-level" CONFIG_LOG_LEVEL_DEFAULT_VAL="info" -CONFIG_LOG_LEVEL_OPTIONS="off,debug,info,warning,error" +CONFIG_LOG_LEVEL_OPTIONS="debug,info,warning,error" +CONFIG_LOG_HEADER_DEFAULT_VAL="pre-commit-grype" +CONFIG_LOG_TEXT_COLOR_DEFAULT_VAL="true" +CONFIG_LOG_DATE_FORMAT_DEFAULT_VAL="%Y-%m-%d %T" +CONFIG_LOG_OUTPUT_FORMAT_DEFAULT_VAL="text" + +# Temp dir CONFIG_TEMP_DIR_NAME=".pre-commit-grype" CONFIG_TEMP_DIR="$(pwd)/${CONFIG_TEMP_DIR_NAME}" CONFIG_TEMP_BIN_DIR="${CONFIG_TEMP_DIR}/bin" diff --git a/src/lib/global-vars/modifiers.sh b/src/lib/global-vars/modifiers.sh deleted file mode 100755 index a38f8b9..0000000 --- a/src/lib/global-vars/modifiers.sh +++ /dev/null @@ -1,29 +0,0 @@ -#!/usr/bin/env sh - -# Gets log level. Precedence: -# (1) Parameter defined via --hook-args, i.e. value saved to temporary directory -# (2) Environment variable -# (3) Default value -get_global_log_level() { - set +u - # Removing trailing and leading spaces is needed here to be sure to have a correct - # value retrieved from environment variable in case user sets it incorrectly - # (with spaces) - get_prop "PRE_COMMIT_GRYPE_LOG_LEVEL" \ - "${PRE_COMMIT_GRYPE_LOG_LEVEL:-${CONFIG_LOG_LEVEL_DEFAULT_VAL}}" \ - | sed 's/^ *//' | sed 's/ *$//' - set -u -} - -# This function is called by the name dynamically also, such as set_global_${str} -set_global_log_level() { - log_level="$1" - is_valid=$(validate_log_level_param "${log_level}") - if [ "${is_valid}" = "true" ]; then - save_prop "PRE_COMMIT_GRYPE_LOG_LEVEL" "${log_level}" - fi -} - -reset_global_log_level() { - set_global_log_level "${CONFIG_LOG_LEVEL_DEFAULT_VAL}" -} diff --git a/src/lib/global-vars/verify.sh b/src/lib/global-vars/verify.sh deleted file mode 100755 index 3067d5a..0000000 --- a/src/lib/global-vars/verify.sh +++ /dev/null @@ -1,14 +0,0 @@ -#!/usr/bin/env sh - -_verify_log_level() { - env_var_name="PRE_COMMIT_GRYPE_LOG_LEVEL" - env_var_val="$(get_global_log_level)" - is_valid=$(validate_log_level_global_var "${env_var_val}" "off") - if [ "${is_valid}" = "false" ]; then - reset_global_log_level - fi -} - -verify_global_vars() { - _verify_log_level -} diff --git a/src/lib/utils/validators.sh b/src/lib/utils/validators.sh index 691a18d..233ce3c 100755 --- a/src/lib/utils/validators.sh +++ b/src/lib/utils/validators.sh @@ -4,24 +4,16 @@ validate_enum() { param_key="$1" param_val="$2" enum_opts="$3," - log_level="${4:-warning}" case ",${enum_opts}" in *",${param_val},"*) - echo "true" ;; *) - echo "false" + fabasoad_log "error" "\"${param_key}\" parameter is invalid. Possible values: $(echo "${enum_opts%,}" | sed 's/,/, /g')." + exit 1 ;; esac } -_validate_log_level() { - param_key="${1}" - param_val="${2}" - log_level="${3:-warning}" - validate_enum "${param_key}" "${param_val}" "${CONFIG_LOG_LEVEL_OPTIONS}" "${log_level}" -} - validate_log_level_param() { param_val="${1}" log_level="${2:-warning}" diff --git a/src/main.sh b/src/main.sh index d2d946b..670ac84 100755 --- a/src/main.sh +++ b/src/main.sh @@ -1,14 +1,5 @@ #!/usr/bin/env bash -# Global variables -_global_config() { - export FABASOAD_LOG_CONFIG_TEXT_COLOR="true" - export FABASOAD_LOG_CONFIG_DATE_FORMAT="%s" - export FABASOAD_LOG_CONFIG_OUTPUT_FORMAT="text" - export FABASOAD_LOG_CONFIG_HEADER="pre-commit-grype" - export FABASOAD_LOG_CONFIG_LOG_LEVEL="$(get_global_log_level)" -} - # Import all scripts _import_all() { current_file=$(basename "$0") @@ -30,36 +21,28 @@ main() { declare -A all_args_map parse_all_args all_args_map "$(echo "$@" | sed 's/^ *//' | sed 's/ *$//')" - declare -A logs_map - parse_hook_args logs_map "${all_args_map["hook-args"]}" - - # Need to verify parameters that were parsed from hook args - verify_global_vars - # Setting global config, such as logging - _global_config - # Print logs from map if any - for log_level in "${!logs_map[@]}"; do - fabasoad_log "${log_level}" "${logs_map[$log_level]}" - done + declare -A hook_args_map + parse_hook_args hook_args_map "${all_args_map["hook-args"]}" + + # Apply configs + set +u + apply_logging_config "${hook_args_map["${CONFIG_LOG_LEVEL_ARG_NAME}"]}" + + if [ "${#hook_args_map[@]}" -ne 0 ]; then + hook_args_str="" + for key in "${!hook_args_map[@]}"; do + hook_args_str="${hook_args_str} ${key}=${hook_args_map[$key]}" + done + fabasoad_log "info" "Pre-commit hook arguments:${hook_args_str}" + fi + set -u case "${cmd_actual}" in "${cmd_grype_dir}") grype_dir "${all_args_map["grype-args"]}" ;; *) - declare -A validation_logs_map - is_valid=$(validate_enum validation_logs_map "hook" "${cmd_actual}" "${cmd_grype_dir}" "error") - # Print logs from map if any - for log_level in "${!validation_logs_map[@]}"; do - fabasoad_log "${log_level}" "${validation_logs_map[$log_level]}" - done - - if [ "${is_valid}" = "false" ]; then - exit 1 - else - fabasoad_log "error" "Something went wrong" - exit 1 - fi + validate_enum "hook" "${cmd_actual}" "${cmd_grype_dir}" ;; esac } From 9e4b9d87950e1d3b6039ad8300384995b84f92f0 Mon Sep 17 00:00:00 2001 From: fabasoad Date: Sat, 13 Jul 2024 18:10:42 +0900 Subject: [PATCH 5/5] Use environment variable --- src/lib/config/config-logging.sh | 7 +++---- src/lib/utils/map.sh | 20 ++++++++++++++++++++ src/lib/utils/validators.sh | 12 ------------ src/main.sh | 8 +------- 4 files changed, 24 insertions(+), 23 deletions(-) create mode 100755 src/lib/utils/map.sh diff --git a/src/lib/config/config-logging.sh b/src/lib/config/config-logging.sh index bf44924..d2cdbdc 100755 --- a/src/lib/config/config-logging.sh +++ b/src/lib/config/config-logging.sh @@ -1,13 +1,12 @@ #!/usr/bin/env sh apply_logging_config() { - log_level="${1:-${CONFIG_LOG_LEVEL_DEFAULT_VAL}}" + log_level="${1:-${PRE_COMMIT_GRYPE_LOG_LEVEL:-${CONFIG_LOG_LEVEL_DEFAULT_VAL}}}" + validate_enum "${CONFIG_LOG_LEVEL_ARG_NAME}" "${log_level}" "${CONFIG_LOG_LEVEL_OPTIONS}" + export FABASOAD_LOG_CONFIG_LOG_LEVEL="${log_level}" export FABASOAD_LOG_CONFIG_TEXT_COLOR="${CONFIG_LOG_TEXT_COLOR_DEFAULT_VAL}" export FABASOAD_LOG_CONFIG_DATE_FORMAT="${CONFIG_LOG_DATE_FORMAT_DEFAULT_VAL}" export FABASOAD_LOG_CONFIG_OUTPUT_FORMAT="${CONFIG_LOG_OUTPUT_FORMAT_DEFAULT_VAL}" export FABASOAD_LOG_CONFIG_HEADER="${CONFIG_LOG_HEADER_DEFAULT_VAL}" - - validate_enum "${CONFIG_LOG_LEVEL_ARG_NAME}" "${log_level}" "${CONFIG_LOG_LEVEL_OPTIONS}" - export FABASOAD_LOG_CONFIG_LOG_LEVEL="${log_level}" } diff --git a/src/lib/utils/map.sh b/src/lib/utils/map.sh new file mode 100755 index 0000000..3e75ba1 --- /dev/null +++ b/src/lib/utils/map.sh @@ -0,0 +1,20 @@ +#!/usr/bin/env sh + +map_to_str() { + local -n map_ref=$1 + shift + + args_delimiter="${1:-" "}" + key_val_delimiter="${2:-"="}" + + str="" + if [ "${#map_ref[@]}" -ne 0 ]; then + for key in "${!map_ref[@]}"; do + str="${str}${args_delimiter}${key}${key_val_delimiter}${map_ref[$key]}" + done + fi + if [ "${#str}" -ne 0 ]; then + str=${str:${#args_delimiter}} + fi + echo "${str}" +} diff --git a/src/lib/utils/validators.sh b/src/lib/utils/validators.sh index 233ce3c..76928f7 100755 --- a/src/lib/utils/validators.sh +++ b/src/lib/utils/validators.sh @@ -13,15 +13,3 @@ validate_enum() { ;; esac } - -validate_log_level_param() { - param_val="${1}" - log_level="${2:-warning}" - _validate_log_level "${CONFIG_LOG_LEVEL_ARG_NAME}" "${param_val}" "${log_level}" -} - -validate_log_level_global_var() { - param_val="${1}" - log_level="${2:-warning}" - _validate_log_level "PRE_COMMIT_GRYPE_LOG_LEVEL" "${param_val}" "${log_level}" -} diff --git a/src/main.sh b/src/main.sh index 670ac84..4c18b9e 100755 --- a/src/main.sh +++ b/src/main.sh @@ -28,13 +28,7 @@ main() { set +u apply_logging_config "${hook_args_map["${CONFIG_LOG_LEVEL_ARG_NAME}"]}" - if [ "${#hook_args_map[@]}" -ne 0 ]; then - hook_args_str="" - for key in "${!hook_args_map[@]}"; do - hook_args_str="${hook_args_str} ${key}=${hook_args_map[$key]}" - done - fabasoad_log "info" "Pre-commit hook arguments:${hook_args_str}" - fi + fabasoad_log "info" "Pre-commit hook arguments: $(map_to_str hook_args_map)" set -u case "${cmd_actual}" in