Skip to content

Instantly share code, notes, and snippets.

@Noxsios
Created March 22, 2024 19:21
Show Gist options
  • Save Noxsios/c54763c84e9216972688eba0c9df2569 to your computer and use it in GitHub Desktop.
Save Noxsios/c54763c84e9216972688eba0c9df2569 to your computer and use it in GitHub Desktop.
#!/bin/bash
# Directory containing the Go files
DIRECTORY="."
# Array of paths to exclude from the check
EXCLUDE_PATHS=(
"./path/to/directory/exclude1"
"./path/to/directory/exclude2"
)
# Function to check if a path is in the EXCLUDE_PATHS array
is_excluded() {
local path="$1"
for exclude in "${EXCLUDE_PATHS[@]}"; do
if [[ "$path" == "$exclude"* ]]; then
return 0 # 0 means true/success in shell script
fi
done
return 1 # 1 means false/failure in shell script
}
# Flag to track if any file meets the condition
found=0
# Use process substitution to avoid subshell issue with the 'found' variable
while IFS= read -r file; do
if is_excluded "$file"; then
echo "Skipping excluded file: $file"
continue
fi
# Use `head` to grab the first two lines and compare them directly
firstLine=$(head -n 1 "$file")
secondLine=$(head -n 2 "$file" | tail -n 1)
# Check if the lines do not match the specified strings
if [[ "$firstLine" != "// SPDX-License-Identifier: Apache-2.0" || "$secondLine" != "// SPDX-FileCopyrightText: 2024-Present Defense Unicorns" ]]; then
echo "Non-compliant file found: $file"
found=1
fi
done < <(find "$DIRECTORY" -type f -name "*.go")
# If any file met the condition, exit with status 1
if [ "$found" -eq 1 ]; then
exit 1
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment