Skip to content

Commit

Permalink
Optimize loop
Browse files Browse the repository at this point in the history
  • Loading branch information
Isengo1989 committed Dec 7, 2023
1 parent 356c33b commit c0cb06f
Showing 1 changed file with 18 additions and 9 deletions.
27 changes: 18 additions & 9 deletions .github/scripts/file-watcher.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@

# Set the path to the snippets folder
WATCHER_PATH=$1
echo 0 > error.txt
COUNTER=0
ERROR_FOUND=0


# Find all files in the snippets folder
find $WATCHER_PATH -type f -name '*.md' -o -name '*.yaml' -print0 | while IFS= read -r -d '' file; do

while IFS= read -r -d '' file; do
# Check if the file contains the string "WATCHER_URL" and is therefore a watcher file
cat "$file" | grep 'WATCHER_URL' >> /dev/null || continue
((COUNTER++))

# Get the JSON from the first line of the file
HEAD=$(cat "$file" | grep 'WATCHER_URL')
Expand All @@ -17,7 +19,14 @@ find $WATCHER_PATH -type f -name '*.md' -o -name '*.yaml' -print0 | while IFS= r
# Get the values from the JSON
WATCHER_URL=$(echo $JSON | jq -r '.WATCHER_URL')
WATCHER_HASH=$(echo $JSON | jq -r '.WATCHER_HASH')
WATCHER_CONTAINS=$(echo $JSON | jq -r '.WATCHER_CONTAINS')
WATCHER_CONTAINS=$(echo $JSON | jq -r '.WATCHER_CONTAINS')

echo '#####'
echo $file
echo $WATCHER_URL
echo $WATCHER_HASH
echo $WATCHER_CONTAINS
echo '#####'

# Get the hash of the URL
EVAL_HASH="curl -sl $WATCHER_URL | md5sum | cut -d ' ' -f 1"
Expand All @@ -33,7 +42,7 @@ find $WATCHER_PATH -type f -name '*.md' -o -name '*.yaml' -print0 | while IFS= r

# Check if $WATCHER_CONTAINS contains the string "null" and is therefore empty or not set
if [[ $WATCHER_CONTAINS == "null" ]]; then
echo 1 > error.txt
ERROR_FOUND=1
echo "Sourcefile: https://github.com/shopware/docs/blob/main/$file"
fi
# Check if $WATCHER_CONTAINS contains the string $WATCHER_CONTAINS and if not call the Slack webhook
Expand All @@ -42,18 +51,18 @@ find $WATCHER_PATH -type f -name '*.md' -o -name '*.yaml' -print0 | while IFS= r
else
echo "String not found! Please check $WATCHER_URL"
echo "Sourcefile: https://github.com/shopware/docs/blob/main/$file"
echo 1 > error.txt
ERROR_FOUND=1
fi
fi
done
done < <(find $WATCHER_PATH -type f -regex '.*\.\(md\|yaml\)$' -print0)

ERROR_FOUND=$(cat error.txt)
echo "Found $COUNTER files with a watcher tag."

if [ "$ERROR_FOUND" -eq 1 ]
then
echo "Error found, exiting..."
exit 1
else
echo "$ERROR_FOUND No error found, exiting..."
echo "No error found, exiting..."
exit 0
fi

0 comments on commit c0cb06f

Please sign in to comment.