forked from Panfactum/stack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenerate-tf-docs.sh
More file actions
executable file
·110 lines (92 loc) · 3.99 KB
/
generate-tf-docs.sh
File metadata and controls
executable file
·110 lines (92 loc) · 3.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#!/usr/bin/env bash
# Purpose: Uses terraform-docs to create the markdown documentation
# for each terraform module for the public website
DOCS_VERSION_DIR="$REPO_ROOT/packages/website/src/content/docs/main"
OUTPUT_DIR="$DOCS_VERSION_DIR/modules"
# Initialize an empty JSON object with a `modules` array
JSON=$(jq -n '{modules: []}')
# Function to clean only auto-generated content (reference folders)
function clean_autogenerated_content() {
local module_dir="$1"
if [ -d "$module_dir/reference" ]; then
rm -rf "$module_dir/reference"
fi
}
function skip_injected_variables() {
awk '
{
lines[NR] = $0
}
/#injected/ {
for (i = NR-2; i <= NR; i++)
delete lines[i]
for (i = NR; i <= NR+4; i++)
toSkip[i] = 1
}
END {
for (i = 1; i <= NR; i++)
if (i in lines && !(i in toSkip))
print lines[i]
}
'
}
function add_provider_links() {
sed -E 's@- (helm|kubernetes|aws|time|local|vault|time|random|tls|archive) \((.*)\)@- [\1](https://registry.terraform.io/providers/hashicorp/\1/\2/docs) (\2)@g' |
sed -E 's@- kubectl \((.*)\)@- [kubectl](https://registry.terraform.io/providers/alekc/kubectl/\1/docs) (\1)@g' |
sed -E 's@- authentik \((.*)\)@- [authentik](https://registry.terraform.io/providers/goauthentik/authentik/\1/docs) (\1)@g' |
sed -E 's@- pf \((.*)\)@- [pf](https://registry.terraform.io/providers/panfactum/pf/\1/docs) (\1)@g' |
sed -E 's@- mongodbatlas \((.*)\)@- [mongodbatlas](https://registry.terraform.io/providers/panfactum/mongodbatlas/\1/docs) (\1)@g'
}
function rename_provider_header() {
sed -E 's@## Requirements@## Providers@g' |
sed -E 's@The following requirements are needed by this module@The following providers are needed by this module@g' |
sed -E 's@<a name="requirement_(.*)"></a>@@g'
}
function remove_version_header() {
sed '/^Version:$/d'
}
function add_header() {
sed -E "1iimport ModuleHeader from \"@/components/markdown/ModuleHeader.astro\";\n" |
sed -E "6i<ModuleHeader name=\"$1\" sourceHref=\"https://github.com/Panfactum/stack/tree/__PANFACTUM_VERSION_MAIN__/packages/infrastructure/$1\" status=\"$2\" type=\"$3\"/>"
}
# Loop through each directory in the script's directory
for d in "$TERRAFORM_MODULES_DIR"/*; do
if [ -d "$d" ]; then
# Extract the name of the directory
MODULE=$(basename "$d")
# Ready the config
TYPE=$(yq -r '.type' "$d/config.yaml")
STATUS=$(yq -r '.status' "$d/config.yaml")
GROUP=$(yq -r '.group' "$d/config.yaml")
# Don't generate documentation for utility modules
if [[ $TYPE == "utility" ]]; then
continue
fi
# Append the directory name to the modules array in the JSON object
JSON=$(jq --arg module "$MODULE" --arg group "$GROUP" --arg type "$TYPE" '.modules += [{"module": $module, "type": $type, "group": $group}]' <<<"$JSON")
# Make the docs in new flat structure with reference subfolder
MODULE_DIR="$OUTPUT_DIR/$MODULE"
DOCS_DIR="$MODULE_DIR/reference"
# Clean existing auto-generated content only
clean_autogenerated_content "$MODULE_DIR"
# Create the reference directory
mkdir -p "$DOCS_DIR"
# Generate the reference documentation
terraform-docs -c "$TERRAFORM_MODULES_DIR/.terraform-docs.yml" "$d" |
add_provider_links |
remove_version_header |
rename_provider_header |
add_header "$MODULE" "$STATUS" "$TYPE" |
skip_injected_variables \
>"$DOCS_DIR/index.mdx"
# Copy only image files from the doc_images directory if it exists
if [ -d "$d/doc_images" ]; then
mkdir -p "$DOCS_DIR/doc_images"
find "$d/doc_images" -maxdepth 1 -type f \( -iname "*.png" -o -iname "*.jpg" -o -iname "*.jpeg" -o -iname "*.gif" -o -iname "*.svg" -o -iname "*.webp" \) -exec cp {} "$DOCS_DIR/doc_images/" \;
fi
fi
done
echo "$JSON" >"$DOCS_VERSION_DIR/modules.json"
# Enhance the modules.json with filesystem content structure
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
DOCS_VERSION_DIR="$DOCS_VERSION_DIR" node "$SCRIPT_DIR/enhance-modules-json.js"