From 43414109d8039402055a775192b602598847fd92 Mon Sep 17 00:00:00 2001 From: fabolous005 Date: Thu, 12 Sep 2024 23:06:59 +0200 Subject: [PATCH] finish backend of smart_find --- etools-helper.sh | 43 +++++++++++++++++++++++++++++++++---------- etools.sh | 3 +++ 2 files changed, 36 insertions(+), 10 deletions(-) diff --git a/etools-helper.sh b/etools-helper.sh index 44dec71..cc9e0ad 100755 --- a/etools-helper.sh +++ b/etools-helper.sh @@ -7,6 +7,7 @@ if [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then _sort_weights \ _get_heighest \ _sort_table \ + _etools_print_assoc_array \ _filter; do unset $function || echo "failed to unset function: $function" @@ -15,8 +16,8 @@ fi function _formatted_find() { if [ -z "${ETOOLS_FIND_COMMAND}" ]; then - # Prevent wordsplitting if [ "${ETOOLS_FIND_CMD}" = "fd" ]; then + # use wordsplitting # shellcheck disable=SC2086 fd $ETOOLS_FIND_ARGS "${1}" "${2}" [ "${ETOOLS_DEBUG}" ] && einfo fd "${1}" "${2}" "$ETOOLS_FIND_ARGS" >&2 @@ -32,29 +33,51 @@ function _formatted_find() { } function _sort_weights() { - declare -n arr=$1 + # declare -n arr=$1 + echo } function _get_heighest() { - echo + # declare -n _etools_packages=$1 + local max_key="" + local max_value=-100 + + for key in "${!_etools_packages[@]}"; do + if (( _etools_packages["$key"] > max_value )); then + max_value=${_etools_packages["$key"]} + max_key=${key//\"/} + fi + done + echo "$max_key" } function _sort_table() { - echo + # declare -n _etools_packages_sort_table=$1 + # _etools_print_assoc_array + true +} + +function _etools_print_assoc_array { + for key in "${!_etools_packages[@]}"; do + echo "$key: ${_etools_packages[$key]}" + done } function _filter() { (( $# <= 1 )) && ewarn "No packages found, review config options" && return 1; - echo "$@" - declare -A packages + declare -Ag _etools_packages for package in "$@"; do - packages[$(echo "$package" | awk -F'/' '{print $(NF-1)"/"$NF}')]=0 + # allow indirect reference + # shellcheck disable=SC2034 + _etools_packages[$(echo "$package" | awk -F'/' '{print $(NF-1)"/"$NF}')]=0 done + local functions= + functions="$(declare -F | "${ETOOLS_GREP_CMD}" 'etools_find_sort_' | awk '{print $3}')" for function in \ _sort_table \ - $(declare -F | "${ETOOLS_GREP_CMD}" 'etools_find_sort_') \ - _get_heighest; + ${functions[@]}; do - $function packages + $function done + _get_heighest } diff --git a/etools.sh b/etools.sh index 11ea7e0..23d0c3f 100644 --- a/etools.sh +++ b/etools.sh @@ -60,12 +60,15 @@ function etools_smart_find() { eerror "no valid repository name" && return 2; . ./etools-helper.sh if [[ "${2}" == /* ]]; then + # shellcheck disable=SC2046 _filter $(_formatted_find "$@") else + # shellcheck disable=SC2046 _filter $(_formatted_find "${1}" "${ETOOLS_REPO_PATH}/${2}") fi else . ./etools-helper.sh + # shellcheck disable=SC2046 _filter $(_formatted_find "${1}" "/var/db/repos") fi # unset helper functions