Skip to content
This repository was archived by the owner on Oct 10, 2025. It is now read-only.

Commit 3d5c1ee

Browse files
committed
refactor: [#10] simplify lint.sh to use tools' built-in file discovery
- yamllint: Use 'yamllint .' instead of manual find + per-file processing - shellcheck: Use glob patterns to find files, process all at once - markdownlint: Use 'markdownlint **/*.md' glob pattern - Removed complex template file handling logic - Simplified error handling with single pass/fail per tool - Reduced code complexity by ~100 lines while maintaining functionality - All linting tools now leverage their own optimized file discovery
1 parent a4a5e5f commit 3d5c1ee

File tree

1 file changed

+43
-88
lines changed

1 file changed

+43
-88
lines changed

scripts/lint.sh

Lines changed: 43 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -45,67 +45,24 @@ run_yamllint() {
4545
return 1
4646
fi
4747

48-
# Find all YAML files, excluding .git directory and .terraform directories
49-
yaml_files=$(find . -name "*.yml" -o -name "*.yaml" | grep -v ".git" | grep -v ".terraform" | sort)
50-
51-
if [ -z "$yaml_files" ]; then
52-
print_status "WARNING" "No YAML files found"
53-
return 0
54-
fi
55-
5648
# Use yamllint config if it exists
57-
yamllint_args=()
5849
if [ -f ".yamllint-ci.yml" ]; then
59-
yamllint_args=("-c" ".yamllint-ci.yml")
60-
fi
61-
62-
local failed=0
63-
for file in $yaml_files; do
64-
# Skip template files that need variable substitution
65-
if [[ "$file" == *.tpl ]]; then
66-
# For template files, create a temporary file with dummy values
67-
if [[ "$file" == *"user-data"* ]]; then
68-
temp_file="/tmp/$(basename "$file" .tpl)"
69-
sed "s/\\\${ssh_public_key}/ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAACAQC/" "$file" >"$temp_file"
70-
if [ ${#yamllint_args[@]} -gt 0 ]; then
71-
if yamllint "${yamllint_args[@]}" "$temp_file"; then
72-
print_status "SUCCESS" "yamllint passed for $file"
73-
else
74-
print_status "ERROR" "yamllint failed for $file"
75-
failed=1
76-
fi
77-
else
78-
if yamllint "$temp_file"; then
79-
print_status "SUCCESS" "yamllint passed for $file"
80-
else
81-
print_status "ERROR" "yamllint failed for $file"
82-
failed=1
83-
fi
84-
fi
85-
rm -f "$temp_file"
86-
else
87-
print_status "WARNING" "Skipping template file $file (needs variable substitution)"
88-
fi
50+
if yamllint -c .yamllint-ci.yml .; then
51+
print_status "SUCCESS" "yamllint passed"
52+
return 0
8953
else
90-
if [ ${#yamllint_args[@]} -gt 0 ]; then
91-
if yamllint "${yamllint_args[@]}" "$file"; then
92-
print_status "SUCCESS" "yamllint passed for $file"
93-
else
94-
print_status "ERROR" "yamllint failed for $file"
95-
failed=1
96-
fi
97-
else
98-
if yamllint "$file"; then
99-
print_status "SUCCESS" "yamllint passed for $file"
100-
else
101-
print_status "ERROR" "yamllint failed for $file"
102-
failed=1
103-
fi
104-
fi
54+
print_status "ERROR" "yamllint failed"
55+
return 1
10556
fi
106-
done
107-
108-
return $failed
57+
else
58+
if yamllint .; then
59+
print_status "SUCCESS" "yamllint passed"
60+
return 0
61+
else
62+
print_status "ERROR" "yamllint failed"
63+
return 1
64+
fi
65+
fi
10966
}
11067

11168
# Function to run ShellCheck
@@ -117,25 +74,33 @@ run_shellcheck() {
11774
return 1
11875
fi
11976

120-
# Find all shell scripts, excluding .git directory and .terraform directories
121-
shell_files=$(find . -name "*.sh" -o -name "*.bash" | grep -v ".git" | grep -v ".terraform" | sort)
77+
# Use glob pattern to find shell scripts, excluding .git and .terraform directories
78+
# Enable globstar for ** patterns
79+
shopt -s globstar nullglob
80+
81+
# Find shell scripts with common extensions
82+
shell_files=()
83+
for pattern in "**/*.sh" "**/*.bash"; do
84+
for file in $pattern; do
85+
# Skip files in .git and .terraform directories
86+
if [[ "$file" != *".git"* && "$file" != *".terraform"* ]]; then
87+
shell_files+=("$file")
88+
fi
89+
done
90+
done
12291

123-
if [ -z "$shell_files" ]; then
92+
if [ ${#shell_files[@]} -eq 0 ]; then
12493
print_status "WARNING" "No shell scripts found"
12594
return 0
12695
fi
12796

128-
local failed=0
129-
for file in $shell_files; do
130-
if shellcheck "$file"; then
131-
print_status "SUCCESS" "shellcheck passed for $file"
132-
else
133-
print_status "ERROR" "shellcheck failed for $file"
134-
failed=1
135-
fi
136-
done
137-
138-
return $failed
97+
if shellcheck "${shell_files[@]}"; then
98+
print_status "SUCCESS" "shellcheck passed"
99+
return 0
100+
else
101+
print_status "ERROR" "shellcheck failed"
102+
return 1
103+
fi
139104
}
140105

141106
# Function to run markdownlint
@@ -147,25 +112,15 @@ run_markdownlint() {
147112
return 1
148113
fi
149114

150-
# Find all markdown files, excluding .git directory and .terraform directories
151-
markdown_files=$(find . -name "*.md" | grep -v ".git" | grep -v ".terraform" | sort)
152-
153-
if [ -z "$markdown_files" ]; then
154-
print_status "WARNING" "No Markdown files found"
115+
# Use markdownlint with glob pattern to find markdown files
116+
# markdownlint can handle glob patterns and will exclude .git directories by default
117+
if markdownlint "**/*.md"; then
118+
print_status "SUCCESS" "markdownlint passed"
155119
return 0
120+
else
121+
print_status "ERROR" "markdownlint failed"
122+
return 1
156123
fi
157-
158-
local failed=0
159-
for file in $markdown_files; do
160-
if markdownlint "$file"; then
161-
print_status "SUCCESS" "markdownlint passed for $file"
162-
else
163-
print_status "ERROR" "markdownlint failed for $file"
164-
failed=1
165-
fi
166-
done
167-
168-
return $failed
169124
}
170125

171126
# Main function

0 commit comments

Comments
 (0)