golangci-lint-fix.sh 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. #!/bin/bash
  2. set -e
  3. if [ ! -f "go.work" ]; then
  4. echo "go.work file not found in current directory"
  5. exit 1
  6. fi
  7. echo "Found go.work file, parsing directories..."
  8. directories=()
  9. in_use_block=false
  10. while IFS= read -r line; do
  11. line=$(echo "$line" | sed 's/^[[:space:]]*//;s/[[:space:]]*$//')
  12. if [[ -z "$line" || "$line" =~ ^// ]]; then
  13. continue
  14. fi
  15. if [[ "$line" =~ ^use[[:space:]]*\( ]]; then
  16. in_use_block=true
  17. continue
  18. fi
  19. if [[ "$in_use_block" == true && "$line" =~ ^\) ]]; then
  20. in_use_block=false
  21. continue
  22. fi
  23. if [[ "$line" =~ ^use[[:space:]]+ ]]; then
  24. dir=$(echo "$line" | sed 's/^use[[:space:]]*//;s/"//g')
  25. directories+=("$dir")
  26. continue
  27. fi
  28. if [[ "$in_use_block" == true ]]; then
  29. dir=$(echo "$line" | sed 's/"//g')
  30. directories+=("$dir")
  31. fi
  32. done <go.work
  33. if [ ${#directories[@]} -eq 0 ]; then
  34. echo "No directories found in go.work file"
  35. exit 0
  36. fi
  37. echo "Found ${#directories[@]} directories to run golangci-lint --fix:"
  38. for dir in "${directories[@]}"; do
  39. echo " - $dir"
  40. done
  41. echo
  42. has_error=false
  43. for dir in "${directories[@]}"; do
  44. echo "Processing directory: $dir"
  45. if [ ! -d "$dir" ]; then
  46. echo "Directory '$dir' does not exist"
  47. exit 1
  48. fi
  49. if [ ! -f "$dir/go.mod" ]; then
  50. echo "No go.mod found in '$dir', skipping..."
  51. exit 1
  52. fi
  53. # --fix will ignore some issues, so we run it twice
  54. if (cd "$dir" && golangci-lint run --path-mode abs --fix && golangci-lint run --path-mode abs); then
  55. echo "Successfully fixed lint issues in '$dir'"
  56. else
  57. echo "Failed to fix lint issues in '$dir'"
  58. has_error=true
  59. fi
  60. echo
  61. done
  62. go work sync
  63. if [ "$has_error" = true ]; then
  64. exit 1
  65. fi