From 035ef798f6ba1910580d616d7352559b6654f542 Mon Sep 17 00:00:00 2001 From: fabolous005 Date: Sun, 15 Sep 2024 16:05:47 +0200 Subject: [PATCH] add _get_high function and trailing match logic --- config.sh | 3 ++- etools | 6 +++--- helper.sh | 41 ++++++++++++++++++++++++++++++++++++----- 3 files changed, 41 insertions(+), 9 deletions(-) diff --git a/config.sh b/config.sh index 010b0e9..0ae46e7 100755 --- a/config.sh +++ b/config.sh @@ -24,8 +24,9 @@ function _configure() { # The Argyments passed to the find command ETOOLS_FIND_ARGS=${ETOOLS_FIND_ARGS:-''' --exclude profiles --exclude scripts --exclude eclass --exclude metadata + --exclude dev-perl --exclude dev-ml --exclude app-emacs --exclude dev-ruby --exclude acct-user --exclude acct-group - --type directory --format '"\"{}\""' --max-depth 3'''} + --type directory --format '"\"{}\""' --max-depth 3 --case-sensitive'''} # [ -z "${ETOOLS_FIND_CMD}" ] && ETOOLS_FIND_CMD="find" # [ -z "${ETOOLS_FIND_ARGS}" ] && \ diff --git a/etools b/etools index 52246f3..9d07bcc 100644 --- a/etools +++ b/etools @@ -23,15 +23,15 @@ function etools_smart_find() { . ./helper.sh if [[ "${2}" == /* ]]; then # shellcheck disable=SC2046 - _filter $(_formatted_find "$@") + _most_probable "$1" $(_filter $(_formatted_find "$@")) else # shellcheck disable=SC2046 - _filter $(_formatted_find "${1}" "${ETOOLS_REPO_PATH}/${2}") + _most_probable "$1" $(_filter $(_formatted_find "${1}" "${ETOOLS_REPO_PATH}/${2}")) fi else . ./helper.sh # shellcheck disable=SC2046 - _filter $(_formatted_find "${1}" "/var/db/repos") + _most_probable "$1" $(_filter $(_formatted_find "${1}" "/var/db/repos")) fi # unset helper functions ./helper.sh diff --git a/helper.sh b/helper.sh index e0ed2f7..c8d6cd3 100755 --- a/helper.sh +++ b/helper.sh @@ -5,10 +5,12 @@ if [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then for function in \ _formatted_find \ _set_weights \ - _get_heighest \ + _get_highest \ + _get_high \ _default_sort \ _etools_print_assoc_array \ _debug_time \ + _most_probable \ _filter; do unset $function || echo "failed to unset function: $function" @@ -37,16 +39,16 @@ function _set_weights() { # allow reference to sourced value # shellcheck disable=SC2154 for regex in "${!package_weights[@]}"; do - if [[ $package =~ ^*${regex}*$ ]]; then + if [[ $package =~ ${regex} ]]; then _etools_packages[$package]=${package_weights[$regex]} fi done done } -function _get_heighest() { +function _get_highest() { local max_key="" - local max_value=-100 + local max_value=-100000 for key in "${!_etools_packages[@]}"; do if (( _etools_packages["$key"] > max_value )); then @@ -57,6 +59,35 @@ function _get_heighest() { echo "$max_key" } +function _get_high() { + local packages=() + local max_value=-100000 + + for key in "${!_etools_packages[@]}"; do + if (( _etools_packages["$key"] > max_value )); then + packages=("\"$key\"") + max_value=${_etools_packages[$key]} + elif (( _etools_packages[$key] == max_value )); then + packages+=("\"$key\"") + fi + done + echo "${packages[@]}" +} + +function _most_probable() { + # echo $* + # echo $# + # echo $1 + # echo $2 + (( $# == 2 )) && echo "$2" && return 0; + for package in "${@:2}"; do + # echo "$package" + if [[ ${package//\"/} == *"$1" ]]; then + echo "${package//\"/}" && return 0; + fi + done +} + function _default_sort() { for package in "${!_etools_packages[@]}"; do case $package in @@ -103,5 +134,5 @@ function _filter() { $function done # _etools_print_assoc_array - _get_heighest + _get_high }