diff options
| author | Kyren223 <kyren223@proton.me> | 2025-08-04 10:59:08 +0300 |
|---|---|---|
| committer | Kyren223 <kyren223@proton.me> | 2025-08-04 10:59:08 +0300 |
| commit | 558755e027fc9173c02af293fb9ca4bb3e0f9f8f (patch) | |
| tree | 7dbc3e08b81c2b91428f30154f2a50ddec519314 /tools | |
| parent | 5f78dd182d46e722244cd91adfc7461980ebdda2 (diff) | |
Updated copyright script and added it to CI
Diffstat (limited to 'tools')
| -rwxr-xr-x | tools/check-ci.sh | 1 | ||||
| -rwxr-xr-x | tools/copyright.sh | 60 |
2 files changed, 44 insertions, 17 deletions
diff --git a/tools/check-ci.sh b/tools/check-ci.sh index 2baee8a..7c2701b 100755 --- a/tools/check-ci.sh +++ b/tools/check-ci.sh @@ -4,6 +4,7 @@ set -e trap 'echo "❌ Check failed"; exit 1' ERR +./tools/copyright.sh --check test -z "$(go fmt ./...)" staticcheck ./... go test --cover ./... diff --git a/tools/copyright.sh b/tools/copyright.sh index cc3a612..0c6a517 100755 --- a/tools/copyright.sh +++ b/tools/copyright.sh @@ -1,9 +1,12 @@ #!/usr/bin/env bash # AGPLv3 header template -START_YEAR=2025 + +# Get current year +CURRENT_YEAR=$(date +%Y) + HEADER="// Eko: A terminal based social media platform -// Copyright (C) $START_YEAR Kyren223 +// Copyright (C) $CURRENT_YEAR Kyren223 // // This program is free software: you can redistribute it and/or modify // it under the terms of the GNU Affero General Public License as published @@ -21,17 +24,20 @@ HEADER="// Eko: A terminal based social media platform # List of paths to exclude (files or directories) EXCLUDE_PATHS=("internal/data" "tools/test_rate_limit.go") +MODIFIED=false +CHECK_MODE=false +if [[ "$1" == "--check" ]]; then + CHECK_MODE=true +fi + # Check if .go files exist recursively if ! find . -type f -name "*.go" | grep -q .; then echo "No .go files found in the current directory or subdirectories." exit 1 fi -# Get current year -CURRENT_YEAR=$(date +%Y) - # Apply header to all .go files recursively, excluding specified paths -find . -type f -name "*.go" | while IFS= read -r file; do +while IFS= read -r file; do # Check if file or its parent directories are in EXCLUDE_PATHS skip=false for exclude in "${EXCLUDE_PATHS[@]}"; do @@ -42,20 +48,40 @@ find . -type f -name "*.go" | while IFS= read -r file; do done if [ "$skip" = true ]; then - echo "Skipped $file (in excluded path)" + if [ $CHECK_MODE = false ]; then + echo "Skipped $file (in excluded path)" + fi continue fi - # Skip if file already has Copyright in the first 10 lines - if ! head -n 10 "$file" | grep -q "Copyright"; then - # Prepend header with a single newline, preserving original content - { echo "$HEADER"; echo; cat "$file"; } > "$file.tmp" && mv "$file.tmp" "$file" - echo "Added license header to $file" - else - # Update year in the header if needed and current year differs - if [ "$CURRENT_YEAR" != "$START_YEAR" ] && head -n 10 "$file" | grep -q "Copyright (C) $START_YEAR"; then - sed -i "1,10s/Copyright (C) $START_YEAR/Copyright (C) $START_YEAR-$CURRENT_YEAR/" "$file" + head_block=$(head -n 10 "$file") + copyright_line=$(echo "$head_block" | grep -E 'Copyright \(C\) [0-9]{4}') + + if [[ -z "$copyright_line" ]]; then + MODIFIED=true + if $CHECK_MODE; then + echo "Missing copyright: $file" + else + { echo "$HEADER"; echo; cat "$file"; } > "$file.tmp" && mv "$file.tmp" "$file" + echo "Added license header to $file" + fi + continue + fi + + # Extract the starting year + start_year=$(echo "$copyright_line" | sed -E 's/.*Copyright \(C\) ([0-9]{4})(-[0-9]{4})?.*/\1/') + + if [[ "$start_year" != "$CURRENT_YEAR" ]]; then + MODIFIED=true + if $CHECK_MODE; then + echo "Needs update: $file" + else + sed -i "1,10s/Copyright (C) $start_year/Copyright (C) $start_year-$CURRENT_YEAR/" "$file" echo "Updated year in $file" fi fi -done +done < <(find . -type f -name "*.go") + +if $MODIFIED; then + exit 1 +fi |
