/ scripts / quick-code-scan.sh
quick-code-scan.sh
  1  #!/usr/bin/env bash
  2  # quick-code-scan.sh
  3  #
  4  # This script performs a quick scan of the code structure and compares it with
  5  # the memory bank documentation to identify potential misalignments.
  6  #
  7  # Usage: ./scripts/quick-code-scan.sh [options]
  8  #
  9  # Options:
 10  #   --verbose        Show detailed output
 11  #   --output=FILE    Write report to FILE
 12  #
 13  # Example: ./scripts/quick-code-scan.sh --verbose --output=code-scan-report.md
 14  
 15  set -e
 16  
 17  # Default options
 18  VERBOSE=false
 19  OUTPUT="code-scan-report.md"
 20  
 21  # Parse options
 22  for arg in "$@"; do
 23      case $arg in
 24          --verbose)
 25              VERBOSE=true
 26              shift
 27              ;;
 28          --output=*)
 29              OUTPUT="${arg#*=}"
 30              shift
 31              ;;
 32          *)
 33              echo "Unknown option: $arg"
 34              exit 1
 35              ;;
 36      esac
 37  done
 38  
 39  # Colors for output
 40  RED='\033[0;31m'
 41  GREEN='\033[0;32m'
 42  YELLOW='\033[0;33m'
 43  BLUE='\033[0;34m'
 44  NC='\033[0m' # No Color
 45  
 46  echo -e "${BLUE}Starting quick code scan...${NC}"
 47  
 48  # Create temporary directory for analysis
 49  TEMP_DIR=$(mktemp -d)
 50  trap 'rm -rf "$TEMP_DIR"' EXIT
 51  
 52  # Function to log verbose output
 53  function log_verbose() {
 54      if [[ "$VERBOSE" == "true" ]]; then
 55          echo -e "$1"
 56      fi
 57  }
 58  
 59  # Function to extract package names from Go files
 60  function extract_packages() {
 61      find . -name "*.go" -type f | xargs grep -l "^package " | while read -r file; do
 62          package=$(grep "^package " "$file" | head -1 | awk '{print $2}')
 63          echo "$(dirname "$file") $package"
 64      done | sort | uniq
 65  }
 66  
 67  # Function to extract interface definitions from Go files
 68  function extract_interfaces() {
 69      find . -name "*.go" -type f | xargs grep -l "type .* interface {" | while read -r file; do
 70          grep -n "type .* interface {" "$file" | while read -r line; do
 71              line_num=$(echo "$line" | cut -d: -f1)
 72              interface_name=$(echo "$line" | cut -d: -f2- | sed -E 's/type ([^ ]+) interface \{.*/\1/')
 73              echo "$file:$line_num $interface_name"
 74          done
 75      done | sort
 76  }
 77  
 78  # Function to extract struct definitions from Go files
 79  function extract_structs() {
 80      find . -name "*.go" -type f | xargs grep -l "type .* struct {" | while read -r file; do
 81          grep -n "type .* struct {" "$file" | while read -r line; do
 82              line_num=$(echo "$line" | cut -d: -f1)
 83              struct_name=$(echo "$line" | cut -d: -f2- | sed -E 's/type ([^ ]+) struct \{.*/\1/')
 84              echo "$file:$line_num $struct_name"
 85          done
 86      done | sort
 87  }
 88  
 89  # Function to extract function definitions from Go files
 90  function extract_functions() {
 91      find . -name "*.go" -type f | xargs grep -l "^func " | while read -r file; do
 92          grep -n "^func " "$file" | while read -r line; do
 93              line_num=$(echo "$line" | cut -d: -f1)
 94              func_name=$(echo "$line" | cut -d: -f2- | sed -E 's/func ([^(]+).*/\1/' | tr -d ' ')
 95              echo "$file:$line_num $func_name"
 96          done
 97      done | sort
 98  }
 99  
100  # Function to extract build tags from Go files
101  function extract_build_tags() {
102      find . -name "*.go" -type f | xargs grep -l "^//go:build" | while read -r file; do
103          grep -n "^//go:build" "$file" | while read -r line; do
104              line_num=$(echo "$line" | cut -d: -f1)
105              build_tags=$(echo "$line" | cut -d: -f2- | sed -E 's/\/\/go:build (.*)/\1/')
106              echo "$file:$line_num $build_tags"
107          done
108      done | sort
109  }
110  
111  # Function to extract import paths from Go files
112  function extract_imports() {
113      find . -name "*.go" -type f | xargs grep -l "^import (" | while read -r file; do
114          awk '/^import \(/{flag=1;next}/\)/{flag=0}flag' "$file" | grep -v "^$" | sed 's/^[ \t]*//' | sort | uniq | while read -r import; do
115              echo "$file $import"
116          done
117      done | sort
118  }
119  
120  # Function to extract command-line flags from Go files
121  function extract_flags() {
122      find . -name "*.go" -type f | xargs grep -l "flag\\." | while read -r file; do
123          grep -n "flag\\." "$file" | grep -E "String|Int|Bool|Float|Duration" | while read -r line; do
124              line_num=$(echo "$line" | cut -d: -f1)
125              flag_def=$(echo "$line" | cut -d: -f2- | sed -E 's/.*flag\.([^(]+)\("([^"]+)".*/\1 \2/')
126              echo "$file:$line_num $flag_def"
127          done
128      done | sort
129  }
130  
131  # Function to extract test files
132  function extract_tests() {
133      find . -name "*_test.go" -type f | sort
134  }
135  
136  # Function to extract build scripts
137  function extract_build_scripts() {
138      find . -name "*.sh" -type f | grep -E "build|install|package|deploy" | sort
139  }
140  
141  # Function to extract documentation files
142  function extract_docs() {
143      find . -name "*.md" -type f | grep -v "memory-bank" | sort
144  }
145  
146  # Function to extract memory bank files
147  function extract_memory_bank() {
148      find memory-bank -name "*.md" -type f | sort
149  }
150  
151  # Function to check if a term is mentioned in the memory bank
152  function check_term_in_memory_bank() {
153      term="$1"
154      grep -l "$term" memory-bank/*.md | wc -l
155  }
156  
157  # Extract code structure
158  echo -e "${BLUE}Extracting code structure...${NC}"
159  extract_packages > "$TEMP_DIR/packages.txt"
160  extract_interfaces > "$TEMP_DIR/interfaces.txt"
161  extract_structs > "$TEMP_DIR/structs.txt"
162  extract_functions > "$TEMP_DIR/functions.txt"
163  extract_build_tags > "$TEMP_DIR/build_tags.txt"
164  extract_imports > "$TEMP_DIR/imports.txt"
165  extract_flags > "$TEMP_DIR/flags.txt"
166  extract_tests > "$TEMP_DIR/tests.txt"
167  extract_build_scripts > "$TEMP_DIR/build_scripts.txt"
168  extract_docs > "$TEMP_DIR/docs.txt"
169  extract_memory_bank > "$TEMP_DIR/memory_bank.txt"
170  
171  # Count items
172  PACKAGE_COUNT=$(wc -l < "$TEMP_DIR/packages.txt")
173  INTERFACE_COUNT=$(wc -l < "$TEMP_DIR/interfaces.txt")
174  STRUCT_COUNT=$(wc -l < "$TEMP_DIR/structs.txt")
175  FUNCTION_COUNT=$(wc -l < "$TEMP_DIR/functions.txt")
176  BUILD_TAG_COUNT=$(wc -l < "$TEMP_DIR/build_tags.txt")
177  IMPORT_COUNT=$(wc -l < "$TEMP_DIR/imports.txt")
178  FLAG_COUNT=$(wc -l < "$TEMP_DIR/flags.txt")
179  TEST_COUNT=$(wc -l < "$TEMP_DIR/tests.txt")
180  BUILD_SCRIPT_COUNT=$(wc -l < "$TEMP_DIR/build_scripts.txt")
181  DOC_COUNT=$(wc -l < "$TEMP_DIR/docs.txt")
182  MEMORY_BANK_COUNT=$(wc -l < "$TEMP_DIR/memory_bank.txt")
183  
184  echo -e "${GREEN}Found:${NC}"
185  echo -e "  $PACKAGE_COUNT packages"
186  echo -e "  $INTERFACE_COUNT interfaces"
187  echo -e "  $STRUCT_COUNT structs"
188  echo -e "  $FUNCTION_COUNT functions"
189  echo -e "  $BUILD_TAG_COUNT build tags"
190  echo -e "  $IMPORT_COUNT imports"
191  echo -e "  $FLAG_COUNT command-line flags"
192  echo -e "  $TEST_COUNT test files"
193  echo -e "  $BUILD_SCRIPT_COUNT build scripts"
194  echo -e "  $DOC_COUNT documentation files"
195  echo -e "  $MEMORY_BANK_COUNT memory bank files"
196  
197  # Check for key components in memory bank
198  echo -e "${BLUE}Checking for key components in memory bank...${NC}"
199  
200  # List of key components to check
201  KEY_COMPONENTS=(
202      "S3Provider"
203      "WebDAVProvider"
204      "SFTPProvider"
205      "TPM"
206      "QuantumResistant"
207      "KeyRotation"
208      "Revocation"
209      "FUSE"
210      "CLI"
211      "Daemon"
212      "Encryption"
213      "Metrics"
214      "Cache"
215      "Compression"
216      "Versioning"
217  )
218  
219  # Check each component
220  for component in "${KEY_COMPONENTS[@]}"; do
221      count=$(check_term_in_memory_bank "$component")
222      if [[ "$count" -eq 0 ]]; then
223          echo -e "${RED}  $component: Not found in memory bank${NC}"
224      else
225          echo -e "${GREEN}  $component: Found in $count memory bank files${NC}"
226      fi
227  done
228  
229  # Check for build-related files
230  echo -e "${BLUE}Checking for build-related files...${NC}"
231  BUILD_FILES=(
232      "Makefile"
233      "go.mod"
234      "go.sum"
235      ".gitignore"
236      ".clinerules"
237  )
238  
239  for file in "${BUILD_FILES[@]}"; do
240      if [[ -f "$file" ]]; then
241          echo -e "${GREEN}  $file: Found${NC}"
242      else
243          echo -e "${RED}  $file: Not found${NC}"
244      fi
245  done
246  
247  # Check for essential directories
248  echo -e "${BLUE}Checking for essential directories...${NC}"
249  ESSENTIAL_DIRS=(
250      "cmd"
251      "internal"
252      "scripts"
253      "memory-bank"
254      "docs"
255  )
256  
257  for dir in "${ESSENTIAL_DIRS[@]}"; do
258      if [[ -d "$dir" ]]; then
259          echo -e "${GREEN}  $dir/: Found${NC}"
260      else
261          echo -e "${RED}  $dir/: Not found${NC}"
262      fi
263  done
264  
265  # Generate report
266  echo -e "${BLUE}Generating report...${NC}"
267  
268  cat > "$OUTPUT" << EOF
269  # Code Scan Report
270  
271  ## Overview
272  
273  This report provides a quick scan of the code structure and compares it with the memory bank documentation to identify potential misalignments.
274  
275  ## Code Structure
276  
277  - **Packages**: $PACKAGE_COUNT
278  - **Interfaces**: $INTERFACE_COUNT
279  - **Structs**: $STRUCT_COUNT
280  - **Functions**: $FUNCTION_COUNT
281  - **Build Tags**: $BUILD_TAG_COUNT
282  - **Imports**: $IMPORT_COUNT
283  - **Command-line Flags**: $FLAG_COUNT
284  - **Test Files**: $TEST_COUNT
285  - **Build Scripts**: $BUILD_SCRIPT_COUNT
286  - **Documentation Files**: $DOC_COUNT
287  - **Memory Bank Files**: $MEMORY_BANK_COUNT
288  
289  ## Key Components
290  
291  EOF
292  
293  for component in "${KEY_COMPONENTS[@]}"; do
294      count=$(check_term_in_memory_bank "$component")
295      if [[ "$count" -eq 0 ]]; then
296          echo "- **$component**: ❌ Not found in memory bank" >> "$OUTPUT"
297      else
298          echo "- **$component**: ✅ Found in $count memory bank files" >> "$OUTPUT"
299      fi
300  done
301  
302  cat >> "$OUTPUT" << EOF
303  
304  ## Build-Related Files
305  
306  EOF
307  
308  for file in "${BUILD_FILES[@]}"; do
309      if [[ -f "$file" ]]; then
310          echo "- **$file**: ✅ Found" >> "$OUTPUT"
311      else
312          echo "- **$file**: ❌ Not found" >> "$OUTPUT"
313      fi
314  done
315  
316  cat >> "$OUTPUT" << EOF
317  
318  ## Essential Directories
319  
320  EOF
321  
322  for dir in "${ESSENTIAL_DIRS[@]}"; do
323      if [[ -d "$dir" ]]; then
324          echo "- **$dir/**: ✅ Found" >> "$OUTPUT"
325      else
326          echo "- **$dir/**: ❌ Not found" >> "$OUTPUT"
327      fi
328  done
329  
330  cat >> "$OUTPUT" << EOF
331  
332  ## Files Needed for Building
333  
334  The following files and directories are essential for building the project:
335  
336  1. **Build Configuration**:
337     - \`go.mod\`: Go module definition
338     - \`go.sum\`: Go module checksums
339     - \`Makefile\`: Build targets and rules
340  
341  2. **Source Code**:
342     - \`cmd/\`: Command-line executables
343     - \`internal/\`: Internal packages
344     - \`pkg/\`: Public packages (if any)
345  
346  3. **Build Scripts**:
347     - \`scripts/\`: Build and utility scripts
348  
349  4. **Documentation**:
350     - \`memory-bank/\`: Project documentation
351     - \`docs/\`: User documentation
352     - \`.clinerules\`: Project intelligence
353  
354  5. **Version Control**:
355     - \`.git/\`: Git repository
356     - \`.gitignore\`: Git ignore rules
357  
358  ## Files That Can Be Cleaned
359  
360  The following files and directories may be candidates for cleaning:
361  
362  1. **Backup Files**:
363     - \`*.bak\`: Backup files
364     - \`*.old\`: Old versions of files
365     - \`*~\`: Temporary files
366  
367  2. **Generated Files**:
368     - \`bin/\`: Binary files (can be rebuilt)
369     - \`*.exe\`: Executable files
370     - \`*.o\`: Object files
371     - \`*.a\`: Archive files
372  
373  3. **Temporary Directories**:
374     - \`temp/\`: Temporary files
375     - \`tmp/\`: Temporary files
376     - \`backup*/\`: Backup directories
377  
378  4. **Duplicate Code**:
379     - Files with \`consolidated\` in the name that have been moved to the reference directory
380  
381  ## Recommendations
382  
383  1. **Memory Bank Updates**:
384     - Review and update documentation for components not well-covered
385     - Ensure all key components are documented
386  
387  2. **Code Cleanup**:
388     - Remove backup and temporary files
389     - Move unused code to the reference directory
390  
391  3. **Build Process**:
392     - Verify that all build scripts work correctly
393     - Document the build process in the memory bank
394  
395  4. **Testing**:
396     - Ensure all tests pass
397     - Add tests for components without test coverage
398  EOF
399  
400  echo -e "${GREEN}Report generated: $OUTPUT${NC}"
401  echo -e "${BLUE}Quick code scan complete.${NC}"