fix(): fix suggestions and false positive 'bpkg-run' rc

devops
jwerle 2 years ago
parent 4756585845
commit c2f8a2c2be

@ -126,11 +126,14 @@ bpkg_run () {
shift
# shellcheck disable=SC2068
eval "$prefix" ${args[@]} $@
"$prefix" ${args[@]} $@
return $?
fi
pushd . >/dev/null || return $?
if ! pushd .; then
bpkg_error "Failed to 'pushd' to current working directory."
return 1
fi
if (( 0 == should_clean )); then
dest=$(bpkg_install --no-prune -g "$1" 2>/dev/null | grep 'Cloning' | sed 's/.* to //g' | xargs echo)
@ -139,10 +142,14 @@ bpkg_run () {
fi
if [ -z "$dest" ]; then
return $?
bpkg_error "The command '$1' was not found locally in a 'bpkg.json' or published as a package."
return 1
fi
cd "$dest" || return $?
if ! cd "$dest"; then
bpkg_error "Failed to change directory to package: '$dest'."
return 1
fi
if [ -z "$name" ]; then
name="$(bpkg_package name 2>/dev/null)"
@ -151,7 +158,10 @@ bpkg_run () {
cmd="$(bpkg_package commands "$1" 2>/dev/null)"
shift
popd >/dev/null || return $?
if ! popd; then
bpkg_error "Failed to 'popd' to previous working directory."
return 1
fi
if (( 1 == should_emit_source )); then
which "$name"
@ -179,11 +189,11 @@ bpkg_run () {
shift
# shellcheck disable=SC2068
eval "$prefix" ${args[@]} $@
"$prefix" ${args[@]} $@
fi
# shellcheck disable=SC2068
eval "$(which "$name")" $@
"$(which "$name")" $@
fi
fi

@ -1,19 +1,33 @@
#!/usr/bin/env bash
## suggest version
VERSION="0.1.0"
## output usage
usage () {
echo "usage: suggest [-hV] <query>"
}
## main
count_lines () {
local -i count=0
while read -r line; do
echo "$line"
(( count++ ))
done
return "$count"
}
prefix_lines_with_length () {
while read -r line; do
echo "$(echo -n "$line" | wc -c | tr -d ' ')" "$line"
done
return $?
}
sort_lines () {
prefix_lines_with_length | sort -n | awk '{ $1=""; print $0 }'
return $?
}
suggest () {
local found paths seen find_supports_maxdepth
declare -a paths=()
declare -a seen=()
declare -a found=()
local count=0
local query="$1"
case "$query" in
@ -22,8 +36,36 @@ suggest () {
return 0
;;
-V|--version)
echo "$VERSION"
*)
if [ "-" = "${query:0:1}" ]; then
echo >&2 "error: Unknown argument \`$query'"
return 1
fi
;;
esac
find_suggestions "$@" | sort_lines | count_lines
count=$? ## count is stored in last return value
if (( count > 0 )); then
printf >&2 "suggest: %d result(s) found\n" "$count"
else
echo >&2 "suggest: Couldn't anything to match \`$query'"
return 1
fi
return 0
}
## main
find_suggestions () {
local paths seen find_supports_maxdepth
declare -a paths=()
declare -a seen=()
local query="$1"
case "$query" in
-h|--help)
usage
return 0
;;
@ -41,67 +83,41 @@ suggest () {
find_supports_maxdepth=0
fi
## search path
{
local res=""
IFS=':' read -r -a paths <<< "$PATH"
for (( i = 0; i < ${#paths[@]}; ++i )); do
local path="${paths[$i]}"
local skip=0
IFS=':' read -r -a paths <<< "$PATH"
for (( i = 0; i < ${#paths[@]}; ++i )); do
local path="${paths[$i]}"
local skip=0
## omit non existent paths
if ! test -d "$path"; then
continue
else
for (( n = 0; n < "${#seen[@]}"; ++n )); do
if [ "$path" = "${seen[$n]}" ]; then
skip=1;
break;
fi
done
## omit non existent paths
if ! test -d "$path"; then
## check if skip needed
if [ "1" = "$skip" ]; then
continue
else
for (( n = 0; n < "${#seen[@]}"; ++n )); do
if [ "$path" = "${seen[$n]}" ]; then
skip=1;
break;
fi
done
## check if skip needed
if [ "1" = "$skip" ]; then
continue
fi
fi
fi
## mark seen
seen+=("$path")
## mark seen
seen+=("$path")
if (( find_supports_maxdepth == 1 )); then
res=$(find "$path" -name "$query*" -prune -print -maxdepth 1 2>/dev/null | tr '\n' ' ');
else
res=$(find "$path" -name "$query*" -prune -print >/dev/null | tr '\n' ' ');
fi
## find in path
if [ -z "$res" ]; then
continue
fi
if (( find_supports_maxdepth == 1 )); then
# echo find "$path" -name "$query*" -prune -print -maxdepth 1 >&2
find "$path" -name "$query*" -prune -print -maxdepth 1 2>/dev/null
else
echo >&2 " warn: Using 'find' command with '-maxdepth' option. Results may appear slowly"
find "$path" -name "$query*" -prune -print -maxdepth 1 2>/dev/null
fi
done
## add to found count
# shellcheck disable=SC2207
found+=($(echo -n "$res"))
done
}
## get total
count="${#found[@]}"
if (( count == 1 )); then
echo "${found[0]}"
elif (( count > 0 )); then
printf "suggest: found %d result(s)\n" "$count"
echo
for (( i = 0; i < count; ++i )); do
printf "%d %s\n" "$(echo -n "${found[$i]}" | wc -c | tr -d ' ')" "${found[$i]}"
done | sort -n | awk '{ print $2 }' | xargs printf ' %s\n'
else
echo "suggest: Couldn't anything to match \`$query'"
return 1
fi
return 0
return $?
}
## export or run

@ -40,7 +40,7 @@ bpkg_initrc() {
## check parameter consistency
bpkg_validate () {
if [ ${#BPKG_GIT_REMOTES[@]} -ne ${#BPKG_REMOTES[@]} ]; then
error "$(printf 'BPKG_GIT_REMOTES[%d] differs in size from BPKG_REMOTES[%d] array' "${#BPKG_GIT_REMOTES[@]}" "${#BPKG_REMOTES[@]}")"
bpkg_error "$(printf 'BPKG_GIT_REMOTES[%d] differs in size from BPKG_REMOTES[%d] array' "${#BPKG_GIT_REMOTES[@]}" "${#BPKG_REMOTES[@]}")"
return 1
fi
return 0

Loading…
Cancel
Save