[pacman-dev,01/10] libmakepkg/util/option: Refactor checking to reduce code duplication

Message ID b82295e010281dec887c727944b3e172dc9e95b1.1527783895.git.jan.steffens@gmail.com
State Superseded, archived
Headers show
Series [pacman-dev,01/10] libmakepkg/util/option: Refactor checking to reduce code duplication | expand

Commit Message

Jan Alexander Steffens May 31, 2018, 4:24 p.m. UTC
Pull out the expected=y/n check into a separate function and make use of
the fact we can just concatenate the fallback arrays to get the same
result.
---
 scripts/libmakepkg/util/option.sh.in | 85 +++++++++-------------------
 1 file changed, 28 insertions(+), 57 deletions(-)

Comments

Allan McRae June 19, 2018, 2:58 p.m. UTC | #1
On 01/06/18 02:24, Jan Alexander Steffens (heftig) wrote:
> Pull out the expected=y/n check into a separate function and make use of
> the fact we can just concatenate the fallback arrays to get the same
> result.
> ---

Breaks using options=(!strip) in a PKGBUILD.

>  scripts/libmakepkg/util/option.sh.in | 85 +++++++++-------------------
>  1 file changed, 28 insertions(+), 57 deletions(-)
> 
> diff --git a/scripts/libmakepkg/util/option.sh.in b/scripts/libmakepkg/util/option.sh.in
> index 46e0568d..28357e27 100644
> --- a/scripts/libmakepkg/util/option.sh.in
> +++ b/scripts/libmakepkg/util/option.sh.in
> @@ -48,95 +48,66 @@ in_opt_array() {
>  }
>  
>  
> +##
> +#  usage : check_opt_array( $option, $expected_val, $haystack )
> +# return : 0   - matches expected
> +#          1   - does not match expected
> +#          127 - not found
> +##
> +check_opt_array() {
> +	local option=$1 expected=$2; shift 2
> +
> +	in_opt_array "$option" "$@"
> +	case $? in
> +		0) # assert enabled
> +			[[ $expected = y ]]
> +			return ;;
> +		1) # assert disabled
> +			[[ $expected = n ]]
> +			return ;;
> +	esac
> +
> +	# not found
> +	return 127
> +}
> +
> +
>  ##
>  # Checks to see if options are present in makepkg.conf or PKGBUILD;
>  # PKGBUILD options always take precedence.
>  #
>  #  usage : check_option( $option, $expected_val )
>  # return : 0   - matches expected
>  #          1   - does not match expected
>  #          127 - not found
>  ##
>  check_option() {
> -	in_opt_array "$1" ${options[@]}
> -	case $? in
> -		0) # assert enabled
> -			[[ $2 = y ]]
> -			return ;;
> -		1) # assert disabled
> -			[[ $2 = n ]]
> -			return
> -	esac
> -
> -	# fall back to makepkg.conf options
> -	in_opt_array "$1" ${OPTIONS[@]}
> -	case $? in
> -		0) # assert enabled
> -			[[ $2 = y ]]
> -			return ;;
> -		1) # assert disabled
> -			[[ $2 = n ]]
> -			return
> -	esac
> -
> -	# not found
> -	return 127
> +	check_opt_array "$@" "${options[@]}" "${OPTIONS[@]}"

These need switched.

>  }
>  
>  
>  ##
>  # Check if option is present in BUILDENV
>  #
>  #  usage : check_buildenv( $option, $expected_val )
>  # return : 0   - matches expected
>  #          1   - does not match expected
>  #          127 - not found
>  ##
>  check_buildenv() {
> -	in_opt_array "$1" ${BUILDENV[@]}
> -	case $? in
> -		0) # assert enabled
> -			[[ $2 = "y" ]]
> -			return ;;
> -		1) # assert disabled
> -			[[ $2 = "n" ]]
> -			return ;;
> -	esac
> -
> -	# not found
> -	return 127
> +	check_opt_array "$@" "${BUILDENV[@]}"
>  }
>  
> +
>  ##
>  # Checks to see if options are present in BUILDENV or PKGBUILD;
>  # PKGBUILD options always take precedence.
>  #
>  #  usage : check_buildoption( $option, $expected_val )
>  # return : 0   - matches expected
>  #          1   - does not match expected
>  #          127 - not found
>  ##
>  check_buildoption() {
> -	in_opt_array "$1" ${options[@]}
> -	case $? in
> -		0) # assert enabled
> -			[[ $2 = y ]]
> -			return ;;
> -		1) # assert disabled
> -			[[ $2 = n ]]
> -			return
> -	esac
> -
> -	in_opt_array "$1" ${BUILDENV[@]}
> -	case $? in
> -		0) # assert enabled
> -			[[ $2 = y ]]
> -			return ;;
> -		1) # assert disabled
> -			[[ $2 = n ]]
> -			return
> -	esac
> -
> -	# not found
> -	return 127
> +	check_opt_array "$@" "${options[@]}" "${BUILDENV[@]}"

These need switched.

>  }
>

Patch

diff --git a/scripts/libmakepkg/util/option.sh.in b/scripts/libmakepkg/util/option.sh.in
index 46e0568d..28357e27 100644
--- a/scripts/libmakepkg/util/option.sh.in
+++ b/scripts/libmakepkg/util/option.sh.in
@@ -48,95 +48,66 @@  in_opt_array() {
 }
 
 
+##
+#  usage : check_opt_array( $option, $expected_val, $haystack )
+# return : 0   - matches expected
+#          1   - does not match expected
+#          127 - not found
+##
+check_opt_array() {
+	local option=$1 expected=$2; shift 2
+
+	in_opt_array "$option" "$@"
+	case $? in
+		0) # assert enabled
+			[[ $expected = y ]]
+			return ;;
+		1) # assert disabled
+			[[ $expected = n ]]
+			return ;;
+	esac
+
+	# not found
+	return 127
+}
+
+
 ##
 # Checks to see if options are present in makepkg.conf or PKGBUILD;
 # PKGBUILD options always take precedence.
 #
 #  usage : check_option( $option, $expected_val )
 # return : 0   - matches expected
 #          1   - does not match expected
 #          127 - not found
 ##
 check_option() {
-	in_opt_array "$1" ${options[@]}
-	case $? in
-		0) # assert enabled
-			[[ $2 = y ]]
-			return ;;
-		1) # assert disabled
-			[[ $2 = n ]]
-			return
-	esac
-
-	# fall back to makepkg.conf options
-	in_opt_array "$1" ${OPTIONS[@]}
-	case $? in
-		0) # assert enabled
-			[[ $2 = y ]]
-			return ;;
-		1) # assert disabled
-			[[ $2 = n ]]
-			return
-	esac
-
-	# not found
-	return 127
+	check_opt_array "$@" "${options[@]}" "${OPTIONS[@]}"
 }
 
 
 ##
 # Check if option is present in BUILDENV
 #
 #  usage : check_buildenv( $option, $expected_val )
 # return : 0   - matches expected
 #          1   - does not match expected
 #          127 - not found
 ##
 check_buildenv() {
-	in_opt_array "$1" ${BUILDENV[@]}
-	case $? in
-		0) # assert enabled
-			[[ $2 = "y" ]]
-			return ;;
-		1) # assert disabled
-			[[ $2 = "n" ]]
-			return ;;
-	esac
-
-	# not found
-	return 127
+	check_opt_array "$@" "${BUILDENV[@]}"
 }
 
+
 ##
 # Checks to see if options are present in BUILDENV or PKGBUILD;
 # PKGBUILD options always take precedence.
 #
 #  usage : check_buildoption( $option, $expected_val )
 # return : 0   - matches expected
 #          1   - does not match expected
 #          127 - not found
 ##
 check_buildoption() {
-	in_opt_array "$1" ${options[@]}
-	case $? in
-		0) # assert enabled
-			[[ $2 = y ]]
-			return ;;
-		1) # assert disabled
-			[[ $2 = n ]]
-			return
-	esac
-
-	in_opt_array "$1" ${BUILDENV[@]}
-	case $? in
-		0) # assert enabled
-			[[ $2 = y ]]
-			return ;;
-		1) # assert disabled
-			[[ $2 = n ]]
-			return
-	esac
-
-	# not found
-	return 127
+	check_opt_array "$@" "${options[@]}" "${BUILDENV[@]}"
 }