gofmt.sh 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. #!/usr/bin/env bash
  2. # Licensed to the LF AI & Data foundation under one
  3. # or more contributor license agreements. See the NOTICE file
  4. # distributed with this work for additional information
  5. # regarding copyright ownership. The ASF licenses this file
  6. # to you under the Apache License, Version 2.0 (the
  7. # "License"); you may not use this file except in compliance
  8. # with the License. You may obtain a copy of the License at
  9. #
  10. # http://www.apache.org/licenses/LICENSE-2.0
  11. #
  12. # Unless required by applicable law or agreed to in writing, software
  13. # distributed under the License is distributed on an "AS IS" BASIS,
  14. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. # See the License for the specific language governing permissions and
  16. # limitations under the License.
  17. ## green to echo
  18. function green(){
  19. echo -e "\033[32m $1 \033[0m"
  20. }
  21. ## Error
  22. function bred(){
  23. echo -e "\033[31m\033[01m $1 \033[0m"
  24. }
  25. files=()
  26. files_need_gofmt=()
  27. if [[ $# -ne 0 ]]; then
  28. for arg in "$@"; do
  29. if [ -f "$arg" ];then
  30. files+=("$arg")
  31. fi
  32. if [ -d "$arg" ];then
  33. for file in `find $arg -type f | grep "\.go$" | grep -v -e mock -e proto`; do
  34. files+=("$file")
  35. done
  36. fi
  37. done
  38. fi
  39. # Check for files that fail gofmt.
  40. if [[ "${#files[@]}" -ne 0 ]]; then
  41. for file in "${files[@]}"; do
  42. diff="$(gofmt -s -d ${file} 2>&1)"
  43. if [[ -n "$diff" ]]; then
  44. files_need_gofmt+=("${file}")
  45. fi
  46. done
  47. fi
  48. if [[ "${#files_need_gofmt[@]}" -ne 0 ]]; then
  49. bred "ERROR!"
  50. for file in "${files_need_gofmt[@]}"; do
  51. gofmt -s -d ${file} 2>&1
  52. done
  53. echo ""
  54. bred "Some files have not been gofmt'd. To fix these errors, "
  55. bred "copy and paste the following:"
  56. for file in "${files_need_gofmt[@]}"; do
  57. bred " gofmt -s -w ${file}"
  58. done
  59. exit 1
  60. else
  61. green "OK"
  62. exit 0
  63. fi