|
1 | 1 | #!/bin/bash
|
2 | 2 |
|
| 3 | +# CLI output colors |
| 4 | +RED='\033[0;31m' |
| 5 | +YELLOW='\033[1;33m' |
| 6 | +NC='\033[0m' # No Color |
| 7 | + |
3 | 8 | errors=()
|
| 9 | +warnings=() |
| 10 | + |
| 11 | +# Check if Terraform is installed |
| 12 | +if ! command -v terraform >/dev/null 2>&1; then |
| 13 | + echo -e "${RED}Error:${NC} Terraform is not installed or not in PATH" |
| 14 | + exit 1 |
| 15 | +fi |
4 | 16 |
|
5 | 17 | check_readme_format() {
|
6 | 18 | local readme_path="$1"
|
| 19 | + |
7 | 20 | if [[ ! -f "$readme_path" ]]; then
|
8 | 21 | errors+=("README.md not found at $readme_path")
|
9 | 22 | return 1
|
10 | 23 | fi
|
11 | 24 |
|
12 |
| - if ! grep -q "^---" "$readme_path"; then |
13 |
| - errors+=("Missing '---' at the beginning of the README.md at $readme_path") |
14 |
| - fi |
15 |
| - if ! grep -q "name: " "$readme_path"; then |
16 |
| - errors+=("Missing 'name' in README.md at $readme_path") |
| 25 | + # Check for valid YAML front matter |
| 26 | + if ! awk '/^---/{f=1; next} /^---$/{if(f){exit 0}} END{exit 1}' "$readme_path"; then |
| 27 | + errors+=("README.md at $readme_path does not have a valid YAML front matter block (start and end with '---')") |
| 28 | + return 1 |
17 | 29 | fi
|
18 |
| - if ! grep -q "supportedPlatforms:" "$readme_path"; then |
19 |
| - errors+=("Missing 'supportedPlatforms' in README.md at $readme_path") |
| 30 | + |
| 31 | + # Extract YAML block |
| 32 | + local yaml_header |
| 33 | + yaml_header=$(awk '/^---/{f=1; next} /^---$/{f=0} f' "$readme_path") |
| 34 | + |
| 35 | + # Check for required fields |
| 36 | + if ! grep -q "name:" <<< "$yaml_header"; then |
| 37 | + errors+=("Missing 'name' in YAML header of README.md at $readme_path") |
20 | 38 | fi
|
21 |
| - if ! grep -q "description:" "$readme_path"; then |
22 |
| - errors+=("Missing 'description' in README.md at $readme_path") |
| 39 | + if ! grep -q "supportedPlatforms:" <<< "$yaml_header"; then |
| 40 | + errors+=("Missing 'supportedPlatforms' in YAML header of README.md at $readme_path") |
23 | 41 | fi
|
24 |
| - if ! grep -q "^---$" "$readme_path"; then |
25 |
| - errors+=("Missing ending '---' in README.md at $readme_path") |
| 42 | + if ! grep -q "description:" <<< "$yaml_header"; then |
| 43 | + errors+=("Missing 'description' in YAML header of README.md at $readme_path") |
26 | 44 | fi
|
| 45 | + |
27 | 46 | return 0
|
28 | 47 | }
|
29 | 48 |
|
30 | 49 | check_png_naming() {
|
31 | 50 | local png_path="$1"
|
32 |
| - local png_name=$(basename "$png_path") |
| 51 | + local png_name |
| 52 | + png_name=$(basename "$png_path") |
| 53 | + |
33 | 54 | if [[ "$png_name" != "logo.png" ]]; then
|
34 |
| - errors+=("Warning: PNG file '$png_name' should be named 'logo.png' to be importable in meshStack") |
| 55 | + warnings+=("PNG file '$png_name' should be named 'logo.png' to be importable in meshStack (path: $png_path)") |
35 | 56 | fi
|
36 | 57 | }
|
37 | 58 |
|
38 | 59 | check_terraform_files() {
|
39 | 60 | local buildingblock_path="$1"
|
40 |
| - local tf_files=("main.tf" "variables.tf" "outputs.tf") |
41 | 61 |
|
42 |
| - for tf_file in "${tf_files[@]}"; do |
| 62 | + # Check if any .tf files exist |
| 63 | + if ! find "$buildingblock_path" -maxdepth 1 -name '*.tf' | grep -q .; then |
| 64 | + errors+=("No Terraform (.tf) files found in $buildingblock_path") |
| 65 | + return 1 |
| 66 | + fi |
| 67 | + |
| 68 | + # Optional: Check for recommended files |
| 69 | + local required_tf_files=("main.tf" "variables.tf" "outputs.tf" "provider.tf" "versions.tf") |
| 70 | + for tf_file in "${required_tf_files[@]}"; do |
43 | 71 | if [[ ! -f "$buildingblock_path/$tf_file" ]]; then
|
44 |
| - errors+=("Error: '$tf_file' not found in $buildingblock_path") |
| 72 | + warnings+=("Recommended file '$tf_file' is missing in $buildingblock_path") |
45 | 73 | fi
|
46 | 74 | done
|
47 |
| - return 0 |
| 75 | + |
| 76 | + # Validate Terraform configuration |
| 77 | + pushd "$buildingblock_path" > /dev/null || return 1 |
| 78 | + |
| 79 | + if ! terraform init -backend=false -input=false > /dev/null 2>&1; then |
| 80 | + errors+=("Terraform init failed in $buildingblock_path") |
| 81 | + elif ! terraform validate > /dev/null 2>&1; then |
| 82 | + errors+=("Terraform validate failed in $buildingblock_path") |
| 83 | + fi |
| 84 | + |
| 85 | + popd > /dev/null || return 1 |
48 | 86 | }
|
49 | 87 |
|
50 |
| -# Ensure it is called from the repo root |
51 |
| -cd "$(dirname "$0")/.." |
| 88 | +# Ensure the script is run from repo root |
| 89 | +cd "$(dirname "$0")/.." || exit 1 |
52 | 90 | modules_path="modules"
|
53 | 91 |
|
54 | 92 | if [[ ! -d "$modules_path" ]]; then
|
55 |
| - echo "Error: Modules folder not found at $modules_path" |
| 93 | + echo -e "${RED}Error:${NC} Modules folder not found at $modules_path" |
56 | 94 | exit 1
|
57 | 95 | fi
|
58 | 96 |
|
59 | 97 | modules_glob="$modules_path/*/*/buildingblock"
|
60 | 98 |
|
61 |
| -for readme_file in $(find $modules_glob -name 'README.md'); do |
| 99 | +# Check all README.md files |
| 100 | +find $modules_glob -name 'README.md' -print0 | while IFS= read -r -d '' readme_file; do |
62 | 101 | check_readme_format "$readme_file"
|
63 | 102 | done
|
64 | 103 |
|
65 |
| -for png_file in $(find $modules_glob -name '*.png'); do |
| 104 | +# Check all PNG files |
| 105 | +find $modules_glob -name '*.png' -print0 | while IFS= read -r -d '' png_file; do |
66 | 106 | check_png_naming "$png_file"
|
67 | 107 | done
|
68 | 108 |
|
69 |
| -for buildingblock_dir in $(find $modules_glob -type d -name 'buildingblock'); do |
| 109 | +# Check all Terraform buildingblock directories |
| 110 | +find $modules_glob -type d -name 'buildingblock' -print0 | while IFS= read -r -d '' buildingblock_dir; do |
70 | 111 | check_terraform_files "$buildingblock_dir"
|
71 | 112 | done
|
72 | 113 |
|
| 114 | +# Print results |
| 115 | +echo "" |
73 | 116 | echo "Number of errors: ${#errors[@]}"
|
| 117 | +echo "Number of warnings: ${#warnings[@]}" |
| 118 | +echo "" |
| 119 | + |
74 | 120 | if [[ ${#errors[@]} -gt 0 ]]; then
|
75 |
| - echo "Errors found:" |
| 121 | + echo -e "${RED}Errors found:${NC}" |
76 | 122 | for error in "${errors[@]}"; do
|
77 |
| - echo "- $error" |
| 123 | + echo -e "- $error" |
78 | 124 | done
|
| 125 | + echo "" |
79 | 126 | exit 1
|
| 127 | +elif [[ ${#warnings[@]} -gt 0 ]]; then |
| 128 | + echo -e "${YELLOW}Warnings found:${NC}" |
| 129 | + for warning in "${warnings[@]}"; do |
| 130 | + echo -e "- $warning" |
| 131 | + done |
| 132 | + echo "" |
| 133 | + exit 0 |
80 | 134 | else
|
81 |
| - echo "All checks passed successfully." |
| 135 | + echo "✅ All checks passed successfully." |
82 | 136 | exit 0
|
83 | 137 | fi
|
0 commit comments