From dd3d39b28565721d95f2be6b785db7844f88e38d Mon Sep 17 00:00:00 2001 From: Henrik Bengtsson Date: Tue, 9 Jan 2024 23:32:16 -0800 Subject: [PATCH] BUG FIX: port4me --prepend= would sort BUG FIX: port4me --include= would not give an error [#69] --- bash/NEWS.md | 4 +++- bash/incl/port4me.bash | 32 ++++++++++++++++++++++++++------ bash/port4me | 2 +- bash/tests/port4me.bats | 12 ++++++------ python/tests/port4me.bats | 12 ++++++------ r/tests/port4me.bats | 12 ++++++------ 6 files changed, 48 insertions(+), 26 deletions(-) diff --git a/bash/NEWS.md b/bash/NEWS.md index a869617..da7572c 100644 --- a/bash/NEWS.md +++ b/bash/NEWS.md @@ -33,7 +33,9 @@ * `port4me --test=0` would output a random port, instead of giving an out-of-range error. - + + * `port4me --prepend=` would sort ``. + # Version 0.6.0 [2023-07-13] diff --git a/bash/incl/port4me.bash b/bash/incl/port4me.bash index cfb2da7..b0f43e4 100644 --- a/bash/incl/port4me.bash +++ b/bash/incl/port4me.bash @@ -57,6 +57,12 @@ _p4m_error() { exit 1 } +_p4m_signal_error() { + if grep -q -E "ERROR<<<(.*)>>>" <<< "$*"; then + _p4m_error "$(sed -E "s/.*ERROR<<<(.*)>>>,*/\1/g" <<< "$*")" + fi +} + #' Check if TCP port can be opened #' #' Examples: @@ -173,9 +179,10 @@ _p4m_string_to_uint() { _p4m_parse_ports() { local spec=${1:?} + local sort=${2:-true} local specs local -a ports - + ## Prune and pre-parse input spec=${spec//\{chrome\}/${PORT4ME_EXCLUDE_UNSAFE_CHROME}} spec=${spec//\{firefox\}/${PORT4ME_EXCLUDE_UNSAFE_FIREFOX}} @@ -184,7 +191,10 @@ _p4m_parse_ports() { spec=${spec## } spec=${spec%% } spec=${spec// /$'\n'} - spec=$(sort -n -u <<< "${spec}") + spec=$(uniq <<< "${spec}") + if $sort; then + spec=$(sort -u <<< "${spec}") + fi ## Split input into lines mapfile -t specs <<< "${spec}" @@ -198,6 +208,11 @@ _p4m_parse_ports() { ports+=($(seq "$from" "$to")) elif grep -q -E "^${pattern}$" <<< "$spec"; then ports+=("$spec") + elif grep -q -E "^[[:blank:]]*$" <<< "$spec"; then + true + else + echo "ERROR<<>>" + return 0 fi done @@ -251,7 +266,7 @@ port4me() { local -i list=${PORT4ME_LIST:-0} local -i test=${PORT4ME_TEST:-0} - local -i exclude include prepend + local -a exclude include prepend local -i count tries tmp_int ## Assert Bash (>= 4) @@ -270,10 +285,15 @@ port4me() { _p4m_can_port_be_opened "${test}" return $? fi - + mapfile -t exclude < <(_p4m_parse_ports "${PORT4ME_EXCLUDE},${PORT4ME_EXCLUDE_SITE},${PORT4ME_EXCLUDE_UNSAFE}") + _p4m_signal_error "${exclude[@]}" + mapfile -t include < <(_p4m_parse_ports "${PORT4ME_INCLUDE},${PORT4ME_INCLUDE_SITE}") - mapfile -t prepend < <(_p4m_parse_ports "${PORT4ME_PREPEND},${PORT4ME_PREPEND_SITE}") + _p4m_signal_error "${include[@]}" + + mapfile -t prepend < <(_p4m_parse_ports "${PORT4ME_PREPEND},${PORT4ME_PREPEND_SITE}" false) + _p4m_signal_error "${prepend[@]}" if [[ -n ${PORT4ME_INCLUDE_MIN} ]]; then tmp_int=${PORT4ME_INCLUDE_MIN} @@ -342,7 +362,7 @@ port4me() { ${PORT4ME_DEBUG:-false} && >&2 printf "Port drawn: %d\n" "$port" fi - ## Skip? + ## Exclude? if (( ${#exclude[@]} > 0 )); then if [[ " ${exclude[*]} " == *" $port "* ]]; then ${PORT4ME_DEBUG:-false} && >&2 printf "Port excluded: %d\n" "$port" diff --git a/bash/port4me b/bash/port4me index e2e6add..75054a2 100755 --- a/bash/port4me +++ b/bash/port4me @@ -39,7 +39,7 @@ #' Requirements: #' * Bash (>= 4) #' -#' Version: 0.6.0-9013 +#' Version: 0.6.0-9014 #' Copyright: Henrik Bengtsson (2022-2024) #' License: MIT diff --git a/bash/tests/port4me.bats b/bash/tests/port4me.bats index 6c13cb9..d291c18 100644 --- a/bash/tests/port4me.bats +++ b/bash/tests/port4me.bats @@ -81,18 +81,18 @@ setup() { assert_output "19654" } -@test " --user=alice --prepend=4321,11001 --list=5" { - run "${cli_call[@]}" --user=alice --prepend=4321,11001 --list=5 +@test " --user=alice --prepend=11001,4321 --list=5" { + run "${cli_call[@]}" --user=alice --prepend=11001,4321 --list=5 assert_success - truth=(4321 11001 30845 19654 32310) + truth=(11001 4321 30845 19654 32310) [[ "${lines[*]}" == "${truth[*]}" ]] } -@test " --user=alice --list=5 with PORT4ME_PREPEND=4321,11001" { - export PORT4ME_PREPEND=4321,11001 +@test " --user=alice --list=5 with PORT4ME_PREPEND=11001,4321" { + export PORT4ME_PREPEND=11001,4321 run "${cli_call[@]}" --user=alice --list=5 assert_success - truth=(4321 11001 30845 19654 32310) + truth=(11001 4321 30845 19654 32310) [[ "${lines[*]}" == "${truth[*]}" ]] } diff --git a/python/tests/port4me.bats b/python/tests/port4me.bats index 6c13cb9..d291c18 100644 --- a/python/tests/port4me.bats +++ b/python/tests/port4me.bats @@ -81,18 +81,18 @@ setup() { assert_output "19654" } -@test " --user=alice --prepend=4321,11001 --list=5" { - run "${cli_call[@]}" --user=alice --prepend=4321,11001 --list=5 +@test " --user=alice --prepend=11001,4321 --list=5" { + run "${cli_call[@]}" --user=alice --prepend=11001,4321 --list=5 assert_success - truth=(4321 11001 30845 19654 32310) + truth=(11001 4321 30845 19654 32310) [[ "${lines[*]}" == "${truth[*]}" ]] } -@test " --user=alice --list=5 with PORT4ME_PREPEND=4321,11001" { - export PORT4ME_PREPEND=4321,11001 +@test " --user=alice --list=5 with PORT4ME_PREPEND=11001,4321" { + export PORT4ME_PREPEND=11001,4321 run "${cli_call[@]}" --user=alice --list=5 assert_success - truth=(4321 11001 30845 19654 32310) + truth=(11001 4321 30845 19654 32310) [[ "${lines[*]}" == "${truth[*]}" ]] } diff --git a/r/tests/port4me.bats b/r/tests/port4me.bats index 6c13cb9..d291c18 100644 --- a/r/tests/port4me.bats +++ b/r/tests/port4me.bats @@ -81,18 +81,18 @@ setup() { assert_output "19654" } -@test " --user=alice --prepend=4321,11001 --list=5" { - run "${cli_call[@]}" --user=alice --prepend=4321,11001 --list=5 +@test " --user=alice --prepend=11001,4321 --list=5" { + run "${cli_call[@]}" --user=alice --prepend=11001,4321 --list=5 assert_success - truth=(4321 11001 30845 19654 32310) + truth=(11001 4321 30845 19654 32310) [[ "${lines[*]}" == "${truth[*]}" ]] } -@test " --user=alice --list=5 with PORT4ME_PREPEND=4321,11001" { - export PORT4ME_PREPEND=4321,11001 +@test " --user=alice --list=5 with PORT4ME_PREPEND=11001,4321" { + export PORT4ME_PREPEND=11001,4321 run "${cli_call[@]}" --user=alice --list=5 assert_success - truth=(4321 11001 30845 19654 32310) + truth=(11001 4321 30845 19654 32310) [[ "${lines[*]}" == "${truth[*]}" ]] }