Skip to content

Commit 4f639af

Browse files
committed
feat(lint) applied shellcheck and added a makefile
1 parent c26fb7e commit 4f639af

7 files changed

+174
-118
lines changed

.shellcheckrc

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
disable=SC2181

Makefile

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
install:
2+
mkdir -p ~/.local/bin
3+
ln -sf $$(pwd)/pongo.sh ~/.local/bin/pongo
4+
if [ ! "$$(command -v pongo)" = "$$(command -v ~/.local/bin/pongo)" ]; then \
5+
echo "\033[0;33m[INFO] please add ~/.local/bin/ to your system path \033[0m"; \
6+
echo "\033[0;33m[INFO] export PATH=~/.local/bin/:"'$$'"PATH\033[0m"; \
7+
fi
8+
9+
lint:
10+
bash -c 'shopt -s globstar nullglob; shellcheck **/*.{sh,ksh,bash}'

assets/add_version.sh

+22-19
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ DRY_RUN=$3
77
LOCAL_PATH=$(dirname "$(realpath "$0")")/..
88

99
# load variables
10-
source ${LOCAL_PATH}/assets/set_variables.sh
10+
# shellcheck source=./assets/set_variables.sh
11+
source "${LOCAL_PATH}/assets/set_variables.sh"
1112

1213

1314
function usage {
@@ -56,7 +57,7 @@ msg "Version to add: $ADD_VERSION"
5657

5758
#TODO: here check we're in a Pongo git repo, and on 'master' branch
5859

59-
if $(version_exists $ADD_VERSION); then
60+
if version_exists "$ADD_VERSION"; then
6061
err "Version '$ADD_VERSION' is already available"
6162
exit 1
6263
fi
@@ -68,26 +69,27 @@ if [[ ! -f $VERSIONS_FILE ]]; then
6869
fi
6970

7071
# add the version to the file
71-
echo "$ADD_VERSION" >> $VERSIONS_FILE
72-
sort --version-sort $VERSIONS_FILE > ${VERSIONS_FILE}_tmp
72+
echo "$ADD_VERSION" >> "$VERSIONS_FILE"
73+
sort --version-sort "$VERSIONS_FILE" > "${VERSIONS_FILE}_tmp"
7374

74-
if [[ ! -f ${VERSIONS_FILE}_tmp ]]; then
75+
if [[ ! -f "${VERSIONS_FILE}_tmp" ]]; then
7576
err "Failed to add and sort the new versions file"
7677
exit 1
7778
fi
7879

79-
mv ${VERSIONS_FILE}_tmp $VERSIONS_FILE
80+
mv "${VERSIONS_FILE}_tmp" "$VERSIONS_FILE"
8081

8182
# reload variables to add the new version to our array
82-
source ${LOCAL_PATH}/assets/set_variables.sh
83+
# shellcheck source=./assets/set_variables.sh
84+
source "${LOCAL_PATH}/assets/set_variables.sh"
8385

8486
# add the first commit with just the added version
85-
pushd ${LOCAL_PATH} > /dev/null
87+
pushd "${LOCAL_PATH}" > /dev/null || { echo "Failure to enter $LOCAL_PATH"; exit 1; }
8688
PREVIOUS_BRANCH=$(git rev-parse --abbrev-ref HEAD)
8789
BRANCH_NAME=add-version-${ADD_VERSION}
88-
git checkout -b ${BRANCH_NAME}
90+
git checkout -b "${BRANCH_NAME}"
8991

90-
git add $VERSIONS_FILE
92+
git add "$VERSIONS_FILE"
9193

9294
if [[ "$CODE_BASE" == "CE" ]]; then
9395
git commit --message="feat(version) added Kong open source version $ADD_VERSION"
@@ -96,14 +98,15 @@ else
9698
fi
9799

98100
# add the artifacts and the second commit
99-
source ${LOCAL_PATH}/assets/update_versions.sh
101+
# shellcheck source=./assets/update_versions.sh
102+
source "${LOCAL_PATH}/assets/update_versions.sh"
100103
update_artifacts
101104

102105
if [[ ! "$?" == "99" ]]; then
103106
warn "Nothing was updated? nothing to commit. Please check the version"
104107
warn "you have added to be a valid one."
105-
git checkout $PREVIOUS_BRANCH &> /dev/null
106-
git branch -D $BRANCH_NAME &> /dev/null
108+
git checkout "$PREVIOUS_BRANCH" &> /dev/null
109+
git branch -D "$BRANCH_NAME" &> /dev/null
107110
exit 1
108111
fi
109112

@@ -117,9 +120,9 @@ fi
117120

118121
# push to remote and create a PR
119122
if [[ "$DRY_RUN" == "" ]]; then
120-
git push --set-upstream origin $BRANCH_NAME
123+
git push --set-upstream origin "$BRANCH_NAME"
121124
if [[ ! $? -eq 0 ]]; then
122-
git checkout $PREVIOUS_BRANCH &> /dev/null
125+
git checkout "$PREVIOUS_BRANCH" &> /dev/null
123126
err "Failed to push the branch '$BRANCH_NAME' to the remote git repo"
124127
fi
125128
else
@@ -129,8 +132,8 @@ msg "Now creating a Github pull-request:"
129132
if [[ "$DRY_RUN" == "" ]]; then
130133
hub pull-request --no-edit
131134
if [[ ! $? -eq 0 ]]; then
132-
git checkout $PREVIOUS_BRANCH &> /dev/null
133-
git branch -D $BRANCH_NAME &> /dev/null
135+
git checkout "$PREVIOUS_BRANCH" &> /dev/null
136+
git branch -D "$BRANCH_NAME" &> /dev/null
134137
err "Failed to create a PR from the '$BRANCH_NAME' branch"
135138
fi
136139
else
@@ -141,8 +144,8 @@ msg "Success! A new branch '$BRANCH_NAME' was pushed to the repo (and a PR creat
141144
msg "with the following commits:"
142145
git log --oneline -2
143146
echo
144-
git checkout $PREVIOUS_BRANCH &> /dev/null
145-
git branch -D $BRANCH_NAME &> /dev/null
147+
git checkout "$PREVIOUS_BRANCH" &> /dev/null
148+
git branch -D "$BRANCH_NAME" &> /dev/null
146149
msg "done. Goto https://github.com/kong/kong-pongo/pulls to checkout the new PR."
147150
if [[ ! "$DRY_RUN" == "" ]]; then
148151
warn "test-run completed, nothing was written! re-run without 'test' to push the changes."

assets/set_variables.sh

+22-11
Original file line numberDiff line numberDiff line change
@@ -7,23 +7,24 @@ NIGHTLY_CE=nightly
77
NIGHTLY_EE=nightly-ee
88

99
function err {
10-
>&2 echo -e "\033[0;31m[pongo-ERROR] $@\033[0m"
10+
>&2 echo -e "\033[0;31m[pongo-ERROR] $*\033[0m"
1111
exit 1
1212
}
1313

1414

1515
function warn {
16-
>&2 echo -e "\033[0;33m[pongo-WARN] $@\033[0m"
16+
>&2 echo -e "\033[0;33m[pongo-WARN] $*\033[0m"
1717
}
1818

1919

2020
function msg {
21-
>&2 echo -e "\033[0;36m[pongo-INFO] $@\033[0m"
21+
>&2 echo -e "\033[0;36m[pongo-INFO] $*\033[0m"
2222
}
2323

2424

2525
# read config from Pongo RC file
2626
if [[ -f $PONGORC_FILE ]]; then
27+
# shellcheck disable=SC2016 # expansion in single quotes: a false positive
2728
IFS=$'\r\n' GLOBIGNORE='*' command eval 'PONGORC_ARGS=($(cat $PONGORC_FILE))'
2829
fi
2930
#echo ".pongorc content: ${PONGORC_ARGS[@]}"
@@ -33,6 +34,7 @@ fi
3334
if [[ ! -f $LOCAL_PATH/assets/kong_EE_versions.ver ]]; then
3435
err "$LOCAL_PATH/assets/kong_EE_versions.ver file is missing!"
3536
fi
37+
# shellcheck disable=SC2016 # expansion in single quotes: a false positive
3638
IFS=$'\r\n' GLOBIGNORE='*' command eval 'KONG_EE_VERSIONS=($(cat $LOCAL_PATH/assets/kong_EE_versions.ver))'
3739
#echo "Current list: ${KONG_EE_VERSIONS[@]}"
3840

@@ -41,25 +43,31 @@ IFS=$'\r\n' GLOBIGNORE='*' command eval 'KONG_EE_VERSIONS=($(cat $LOCAL_PATH/as
4143
if [[ ! -f $LOCAL_PATH/assets/kong_CE_versions.ver ]]; then
4244
err "$LOCAL_PATH/assets/kong_CE_versions.ver file is missing!"
4345
fi
46+
# shellcheck disable=SC2016 # expansion in single quotes: a false positive
4447
IFS=$'\r\n' GLOBIGNORE='*' command eval 'KONG_CE_VERSIONS=($(cat $LOCAL_PATH/assets/kong_CE_versions.ver))'
4548
#echo "Current list: ${KONG_CE_VERSIONS[@]}"
4649

4750

48-
# Create a new array with all versions combined
51+
# Create a new array with all versions combined (wrap in function for local vars)
4952
KONG_VERSIONS=()
50-
for VERSION in ${KONG_EE_VERSIONS[*]}; do
51-
KONG_VERSIONS+=("$VERSION")
52-
done;
53-
for VERSION in ${KONG_CE_VERSIONS[*]}; do
54-
KONG_VERSIONS+=("$VERSION")
55-
done;
53+
function create_all_versions_array {
54+
local VERSION
55+
for VERSION in ${KONG_EE_VERSIONS[*]}; do
56+
KONG_VERSIONS+=("$VERSION")
57+
done;
58+
for VERSION in ${KONG_CE_VERSIONS[*]}; do
59+
KONG_VERSIONS+=("$VERSION")
60+
done;
61+
}
62+
create_all_versions_array
5663

5764
# take the last one as the default
65+
# shellcheck disable=SC2034 # Unused variable: this script is 'sourced', so a false positive
5866
KONG_DEFAULT_VERSION="${KONG_CE_VERSIONS[ ${#KONG_CE_VERSIONS[@]}-1 ]}"
5967

60-
6168
function is_enterprise {
6269
local check_version=$1
70+
local VERSION
6371
for VERSION in ${KONG_EE_VERSIONS[*]} $NIGHTLY_EE; do
6472
if [[ "$VERSION" == "$check_version" ]]; then
6573
return 0
@@ -70,6 +78,7 @@ function is_enterprise {
7078

7179
function is_nightly {
7280
local check_version=$1
81+
local VERSION
7382
for VERSION in $NIGHTLY_CE $NIGHTLY_EE; do
7483
if [[ "$VERSION" == "$check_version" ]]; then
7584
return 0
@@ -80,6 +89,7 @@ function is_nightly {
8089

8190
function version_exists {
8291
local version=$1
92+
local entry
8393
for entry in ${KONG_VERSIONS[*]} $NIGHTLY_CE $NIGHTLY_EE ; do
8494
if [[ "$version" == "$entry" ]]; then
8595
return 0
@@ -92,6 +102,7 @@ function version_exists {
92102
function resolve_version {
93103
if [[ "${KONG_VERSION: -1}" == "x" ]]; then
94104
local new_version=$KONG_VERSION
105+
local entry
95106
for entry in ${KONG_VERSIONS[*]}; do
96107
if [[ "${KONG_VERSION:0:${#KONG_VERSION}-1}" == "${entry:0:${#entry}-1}" ]]; then
97108
# keep replacing, last one wins

assets/test_plugin_entrypoint.sh

+6-5
Original file line numberDiff line numberDiff line change
@@ -62,21 +62,22 @@ elif [ -f /kong-plugin/.pongo-setup.sh ]; then
6262
else
6363
pongo_setup=none
6464
fi
65-
if [ "$pongo_setup" == "none" ]; then
65+
if [ "$pongo_setup" = "none" ]; then
6666
# if there is a rockspec, then install it first, so we get any required
6767
# dependencies installed before testing
6868
find /kong-plugin -maxdepth 1 -type f -name '*.rockspec' -exec luarocks install --only-deps {} \;
6969
else
7070
old_entry_pwd=$(pwd)
71-
cd /kong-plugin
72-
source $pongo_setup
73-
cd $old_entry_pwd
71+
cd /kong-plugin || { echo "Failure to enter /kong-plugin"; exit 1; }
72+
# shellcheck source=/dev/null # not checking this since it is user provided
73+
. $pongo_setup
74+
cd "$old_entry_pwd" || { echo "Failure to enter $old_entry_pwd"; exit 1; }
7475
unset old_entry_pwd
7576
fi
7677
unset pongo_setup
7778

7879

79-
if [ ! "$SUPPRESS_KONG_VERSION" == "true" ]; then
80+
if [ ! "$SUPPRESS_KONG_VERSION" = "true" ]; then
8081
echo "Kong version: $(kong version)"
8182
fi
8283

0 commit comments

Comments
 (0)