generate_commands_markdown.sh 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. #!/usr/bin/env bash
  2. set -euo pipefail
  3. PROJECT_PATH="."
  4. OUTPUT_FILE="COMMANDS.md"
  5. declare -A VISITED
  6. # ----------------------------
  7. # Helpers
  8. # ----------------------------
  9. strip_colors() {
  10. sed -r "s/\x1B\[[0-9;]*[mK]//g"
  11. }
  12. run_help() {
  13. # $@ contains the parts, e.g., "switches" "summary" "list"
  14. echo "Running: dotnet run --project ./RackPeek -- \"$@\"" >&2
  15. local output
  16. if ! output=$(dotnet run --project ./RackPeek -- "$@" 2>&1 | strip_colors); then
  17. echo "WARNING: command failed: dotnet run --project ./RackPeek -- $*" >&2
  18. echo "$output" >&2
  19. return 1
  20. fi
  21. echo "$output"
  22. }
  23. # Extracts commands from help output
  24. get_commands() {
  25. local help_output="$1"
  26. echo "$help_output" | awk '
  27. BEGIN { in_commands = 0 }
  28. /^COMMANDS:/ { in_commands = 1; next }
  29. in_commands {
  30. if ($0 ~ /^[[:space:]]{4}[a-zA-Z0-9-]+[[:space:]]+/) {
  31. print $1
  32. next
  33. }
  34. if ($0 !~ /^[[:space:]]*$/) {
  35. exit
  36. }
  37. }
  38. '
  39. }
  40. # ----------------------------
  41. # Recursion
  42. # ----------------------------
  43. generate_help_recursive() {
  44. local current_path=("$@")
  45. # 1. Create a "Map Key" for the VISITED hash (must not be empty)
  46. local flat_cmd="${current_path[*]}"
  47. local map_key="${flat_cmd:-root}"
  48. # 2. Create a "Display Header" for the Markdown file
  49. # If empty, just "rpk". If not empty, "rpk <commands>"
  50. local display_header
  51. if [[ -z "$flat_cmd" ]]; then
  52. display_header="rpk"
  53. else
  54. display_header="rpk $flat_cmd"
  55. fi
  56. # Prevent infinite loops using the Map Key
  57. if [[ -n "${VISITED["$map_key"]:-}" ]]; then
  58. return
  59. fi
  60. VISITED["$map_key"]=1
  61. # Run help
  62. local help_output
  63. if ! help_output=$(run_help "${current_path[@]}" --help); then
  64. echo "Skipping: $map_key (help failed)" >&2
  65. return
  66. fi
  67. # Append to Markdown file using the Display Header
  68. {
  69. echo "## \`${display_header}\`"
  70. echo '```'
  71. echo "$help_output"
  72. echo '```'
  73. echo ""
  74. } >> "$OUTPUT_FILE"
  75. # Extract subcommands
  76. local commands
  77. mapfile -t commands < <(get_commands "$help_output")
  78. for cmd in "${commands[@]}"; do
  79. echo "Recursing into: ${display_header} ${cmd}" >&2
  80. generate_help_recursive "${current_path[@]}" "$cmd"
  81. done
  82. }
  83. # ----------------------------
  84. # Main
  85. # ----------------------------
  86. {
  87. echo "# CLI Commands"
  88. echo ""
  89. } > "$OUTPUT_FILE"
  90. generate_help_recursive
  91. echo "Generated $OUTPUT_FILE successfully."