gostd 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. #!/usr/bin/env bash
  2. ############################ GLOBAL VARIABLES
  3. regex=' '
  4. branch="master"
  5. max_length=150
  6. REGEX_SUFFIX_GO=".+\.go$"
  7. ############################ FUNCTIONS
  8. msg() {
  9. printf '%b' "$1" >&2
  10. }
  11. die() {
  12. msg "\33[31m[✘]\33[0m ${1}${2}"
  13. exit 1
  14. }
  15. succ() {
  16. msg "\33[34m[√]\33[0m ${1}${2}"
  17. }
  18. gostd() {
  19. local branch=$1
  20. local reg4exclude=$2
  21. local max_length=$3
  22. for file in `git diff $branch --name-only`
  23. do
  24. if ! [[ $file =~ $REGEX_SUFFIX_GO ]] || [[ $file =~ $reg4exclude ]]; then
  25. continue
  26. fi
  27. error=`go fmt $file 2>&1`
  28. if ! [ $? -eq 0 ]; then
  29. die "go fmt $file:" "$error"
  30. fi
  31. succ "$file\n"
  32. grep -n -E --color=always ".{$max_length}" $file | awk '{ printf ("%4s %s\n", "", $0) }'
  33. done
  34. }
  35. get_options() {
  36. while getopts "b:e:hl:" opts
  37. do
  38. case $opts in
  39. b)
  40. branch=$OPTARG
  41. ;;
  42. e)
  43. regex=$OPTARG
  44. ;;
  45. h)
  46. usage
  47. exit 0
  48. ;;
  49. l)
  50. max_length=$OPTARG
  51. ;;
  52. \?)
  53. usage
  54. exit 1
  55. ;;
  56. esac
  57. done
  58. }
  59. usage () {
  60. cat << _EOC_
  61. Usage:
  62. gostd [options]
  63. Options:
  64. -b <branch/commit> Specify the git diff branch or commit.
  65. (default: master)
  66. -e <regex> Regex for excluding file or directory.
  67. -h Print this usage.
  68. -l <length> Show files that exceed the limit line length.
  69. (default: 150)
  70. Examples:
  71. gostd
  72. gostd -b master -l 100
  73. gostd -b 59d532a -e weed/pb -l 100
  74. _EOC_
  75. }
  76. main() {
  77. get_options "$@"
  78. gostd "$branch" "$regex" "$max_length"
  79. }
  80. ############################ MAIN()
  81. main "$@"