Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 38 additions & 9 deletions tools/setup-qwiklabs.sh
Original file line number Diff line number Diff line change
Expand Up @@ -17,24 +17,52 @@ while :; do
shift
done

if [ -z ${HACK} ] || [ -z ${SLUG} ]; then
echo "Usage: ${0} --hack=<string> --slug=<string>" 1>&2; exit 1
fi
HACK="${HACK:-$1}"

if [[ ${SLUG} =~ ^ghacks[0-9]{3}$ ]]; then
SLUG=${SLUG}-${HACK}
if [ -z "${HACK}" ]; then
echo "Usage: ${0} <hack> [--slug=<string>]" 1>&2; exit 1
fi

if [[ ! ${SLUG} =~ ^ghacks[0-9]{3}-${HACK}$ ]]; then
echo "Slug '${SLUG}' has to match pattern 'ghacks[0-9]{3}-${HACK}'" 1>&2; exit 1
fi
# Sanitize in case a path is passed
HACK=$(basename "${HACK}")

HACK_DIR="${SRC_DIR}/hacks/${HACK}"

if [ ! -d ${HACK_DIR} ] || [ ! -d ${DST_DIR} ]; then
if [ ! -d "${HACK_DIR}" ] || [ ! -d "${DST_DIR}" ]; then
echo "${HACK_DIR} and/or ${DST_DIR} missing, are you in the right directory?" 1>&2; exit 1
fi

# If slug is not provided, find it or generate it (for new hacks)
if [ -z "${SLUG}" ]; then
# Check if there is an existing directory matching ghacks[0-9]{3}-${HACK}
EXISTING_DIR=$(ls "${DST_DIR}/labs" | grep -E "ghacks[0-9]{3}-${HACK}" | head -n 1)

if [ -n "${EXISTING_DIR}" ]; then
SLUG="${EXISTING_DIR}"
echo "Found existing directory for hack '${HACK}': ${SLUG}"
else
# Detect the latest ghacks number in the target repository and increment it
LATEST_NUM=$(ls "${DST_DIR}/labs" | grep -E "ghacks[0-9]{3}-*" | \
sed -E 's/^ghacks([0-9]{3})-.*/\1/' | sort -n | tail -n 1)
if [ -z "${LATEST_NUM}" ]; then
NEXT_NUM=1
else
NEXT_NUM=$((10#${LATEST_NUM} + 1))
fi
SLUG=$(printf "ghacks%03d-${HACK}" ${NEXT_NUM})
echo "Detected next slug: ${SLUG}"
fi
else
if [[ ${SLUG} =~ ^ghacks[0-9]{3}$ ]]; then
SLUG=${SLUG}-${HACK}
fi

if [[ ! ${SLUG} =~ ^ghacks[0-9]{3}-${HACK}$ ]]; then
echo "Slug '${SLUG}' has to match pattern 'ghacks[0-9]{3}-${HACK}'" 1>&2; exit 1
fi
fi


LAB_DIR="${DST_DIR}/labs/${SLUG}"
mkdir -p ${LAB_DIR}

Expand All @@ -58,6 +86,7 @@ BLOCK=`cat<<EOF
## Lab resources for the participants

<ql-code-block bash templated noWrap>
Latest instructions: https://ghacks.dev/hacks/${HACK}
$OUTPUTS
</ql-code-block>
EOF
Expand Down
Loading