gen-api-v1-docs.sh 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. #!/bin/bash
  2. # This script generates API documentation using swaggo/swag
  3. # For more details, check the docs:
  4. # * https://usememos.com/docs/contribution/development
  5. # * https://github.com/usememos/memos/blob/main/docs/api/documentation.md
  6. # Requirements:
  7. # * go
  8. # swag is configured via gen-api-v1-docs.cfg file.
  9. # Usage:
  10. # chmod +x ./scripts/gen-api-v1-docs.sh
  11. # ./scripts/gen-api-v1-docs.sh
  12. find_repo_root() {
  13. # Usage: find_repo_root <file_at_root> <dir1> <dir2> ...
  14. local looking_for="${1:-".gitignore"}"
  15. shift
  16. local default_dirs=("." "../")
  17. local dirs=("${@:-${default_dirs[@]}}")
  18. for dir in "${dirs[@]}"; do
  19. if [ -f "$dir/$looking_for" ]; then
  20. echo $(realpath "$dir")
  21. return
  22. fi
  23. done
  24. }
  25. find_binary() {
  26. # Usage: find_binary <binary> <dir1> <dir2> ...
  27. local looking_for="$1"
  28. shift
  29. local default_dirs=(".")
  30. local binary=$(command -v $looking_for)
  31. if [ ! -z "$binary" ]; then
  32. echo "$binary"
  33. return
  34. fi
  35. local dirs=("${@:-${default_dirs[@]}}")
  36. for dir in "${dirs[@]}"; do
  37. if [ -f "$dir/$looking_for" ]; then
  38. echo $(realpath "$dir")/$looking_for
  39. return
  40. fi
  41. done
  42. }
  43. repo_root=$(find_repo_root)
  44. if [ -z "$repo_root" ]; then
  45. echo -e "\033[0;31mRepository root not found! Exiting.\033[0m"
  46. exit 1
  47. else
  48. echo -e "Repository root: \033[0;34m$repo_root\033[0m"
  49. fi
  50. cd $repo_root
  51. echo "Parsing gen-api-v1-docs.cfg..."
  52. source "$repo_root/scripts/gen-api-v1-docs.cfg"
  53. echo -e "API directories: \033[0;34m$SWAG_API_DIRS\033[0m"
  54. echo -e "Output directory: \033[0;34m$SWAG_OUTPUT\033[0m"
  55. echo -e "General info: \033[0;34m$SWAG_GENERAL_INFO\033[0m"
  56. if [ -z "$SWAG_API_DIRS" ]; then
  57. echo -e "\033[0;31mAPI directories not set! Exiting.\033[0m"
  58. exit 1
  59. fi
  60. swag=$(find_binary swag "$HOME/go/bin" "$GOPATH/bin")
  61. if [ -z "$swag" ]; then
  62. echo "Swag is not installed. Installing..."
  63. go install github.com/swaggo/swag/cmd/swag@latest
  64. swag=$(find_binary swag "$HOME/go/bin" "$GOPATH/bin")
  65. fi
  66. if [ -z "$swag" ]; then
  67. echo -e "\033[0;31mSwag binary not found! Exiting.\033[0m"
  68. exit 1
  69. fi
  70. echo -e "Swag binary: \033[0;34m$swag\033[0m"
  71. general_info_path=$(dirname "$SWAG_GENERAL_INFO")
  72. if [ ! -d "$general_info_path" ]; then
  73. echo -e "\033[0;31mGeneral info directory does not exist!\033[0m"
  74. exit 1
  75. fi
  76. echo -e "\e[35mFormatting comments via \`swag fmt --dir "$general_info_path,$SWAG_API_DIRS"\`...\e[0m"
  77. $swag fmt --dir "$general_info_path,$SWAG_API_DIRS"
  78. # This is just in case "swag fmt" do something non-conforming to "go fmt"
  79. go_fmt_dirs=$(echo $general_info_path $SWAG_API_DIRS | tr "," " ")
  80. echo -e "\e[35mFormatting code via \`go fmt $go_fmt_dirs\`...\e[0m"
  81. go fmt $go_fmt_dirs
  82. echo -e "\e[35mGenerating Swagger API documentation...\e[0m"
  83. $swag init --output "$SWAG_OUTPUT" --outputTypes "$SWAG_OUTPUT_TYPES" --generalInfo "$SWAG_GENERAL_INFO" --dir "./,$SWAG_API_DIRS"
  84. if [ $? -ne 0 ]; then
  85. echo -e "\033[0;31mFailed to generate Swagger API documentation!\033[0m"
  86. exit 1
  87. fi
  88. echo -e "\033[0;32mSwagger API documentation updated!\033[0m"