Skip to content

Commit b22398a

Browse files
authored
fix: Check for user-provided build_opts (#71)
* fix: Check for user-provided `build_opts` * chore: Simplify checking for build_opts * chore: Some style fix * chore: Further explain how to use `check_build_opts` function
1 parent 0658135 commit b22398a

1 file changed

Lines changed: 24 additions & 13 deletions

File tree

build_opts_check.sh

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,32 @@ set -euo pipefail
55
# SQUASH_INPUT_VALUE
66
# BUILD_OPTS
77

8-
if [ "$SQUASH_INPUT_VALUE" != "true" ]; then
9-
if grep -qE '(-B)|(--build-driver)' <<< "$BUILD_OPTS"; then
10-
echo 'Cannot provide --build-driver in build_opts while squash is set to true.'
11-
exit 1
12-
fi
8+
# check_build_opts "option1" "option2" "error_message"
9+
# If you have only 1 option to provide, provide '---' string as 2nd placeholder option
10+
# check_build_opts "option1" --- "error_message"
11+
check_build_opts() {
12+
local option1="${1}"
13+
local option2="${2}"
14+
local error_message="${3}"
1315

14-
if grep -qE '(-s)|(--squash)' <<< "$BUILD_OPTS"; then
15-
echo 'Cannot provide --squash in build_opts while squash is set to true.'
16-
exit 1
17-
fi
18-
fi
16+
if [[ "${2}" == "---" ]]; then
17+
if [[ -n "$(awk '/(^|\s)('${option1}')($|\s)/' <<< "${BUILD_OPTS}")" ]]; then
18+
echo "${error_message}"
19+
exit 1
20+
fi
21+
elif [[ -n "${2}" ]]; then
22+
if [[ -n "$(awk '/(^|\s)('${option1}'|'${option2}')($|\s)/' <<< "${BUILD_OPTS}")" ]]; then
23+
echo "${error_message}"
24+
exit 1
25+
fi
26+
fi
27+
}
1928

20-
if grep -qE '(-p)|(--push)' <<< "$BUILD_OPTS"; then
21-
echo 'Please do not add --push to build_opts, as the action already provides that argument.'
22-
exit 1
29+
if [[ "${SQUASH_INPUT_VALUE}" != "true" ]]; then
30+
check_build_opts "-B" "--build-driver" "Cannot provide '--build-driver' in build_opts while 'squash' is set to true."
31+
check_build_opts "-s" "--squash" "Cannot provide '--squash' in build_opts while 'squash' is set to true."
2332
fi
2433

34+
check_build_opts "-p" "--push" "Please do not add '--push' to build_opts, as the action already provides that argument."
35+
2536
exit 0

0 commit comments

Comments
 (0)