update-go-mod.sh 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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 update:"
  38. for dir in "${directories[@]}"; do
  39. echo " - $dir"
  40. done
  41. echo
  42. for dir in "${directories[@]}"; do
  43. echo "Processing directory: $dir"
  44. if [ ! -d "$dir" ]; then
  45. echo "Directory '$dir' does not exist"
  46. exit 1
  47. fi
  48. if [ ! -f "$dir/go.mod" ]; then
  49. echo "No go.mod found in '$dir', skipping..."
  50. exit 1
  51. fi
  52. if (cd "$dir" && go get -u && go mod tidy); then
  53. echo "Successfully updated dependencies in '$dir'"
  54. else
  55. echo "Failed to update dependencies in '$dir'"
  56. exit 1
  57. fi
  58. echo
  59. done
  60. go work sync