Skip to content

Commit e69648b

Browse files
committed
Document environment variables
1 parent 7eadb50 commit e69648b

File tree

4 files changed

+86
-2
lines changed

4 files changed

+86
-2
lines changed

COMMANDS.md

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ Change into the [cypher](./cypher/) directory e.g. with `cd cypher` and then exe
115115
Change into the [scripts](./scripts/) directory e.g. with `cd scripts` and then execute the script [generateScriptReference.sh](./scripts/generateScriptReference.sh) with the following command:
116116
117117
```script
118-
./../scripts/generateScriptReference.sh
118+
./generateScriptReference.sh
119119
```
120120
121121
### Update Markdown Reference
@@ -125,11 +125,18 @@ Change into the [results](./results/) directory e.g. with `cd results` and then
125125
👉**Note:** This script is automatically triggered at the end of [copyReportsIntoResults.sh](./scripts/copyReportsIntoResults.sh)
126126
which is included in the pipeline [code-reports.yml](.github/workflows/code-reports.yml) and doesn't need to be executed manually normally.
127127

128-
129128
```script
130129
./../scripts/generateScriptReference.sh
131130
```
132131

132+
### Update Environment Variable Reference
133+
134+
Change into the [scripts](./scripts/) directory e.g. with `cd scripts` and then execute the script [generateEnvironmentVariablesReference.sh](./scripts/generateEnvironmentVariablesReference.sh) with the following command:
135+
136+
```script
137+
./generateEnvironmentVariablesReference.sh
138+
```
139+
133140
## Manual Setup
134141

135142
The manual setup is only documented for completeness. It isn't needed since the analysis also covers download, installation and configuration of all needed tools.

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,10 @@ The [Code Reports Pipeline](./.github/workflows/code-reports.yml) utilizes [GitH
9494

9595
[COMMANDS.md](./COMMANDS.md) contains further details on commands and how to do a manual setup.
9696

97+
## 🛠 Environment Variable Reference
98+
99+
[ENVIRONMENT_VARIABLES.md](./scripts/ENVIRONMENT_VARIABLES.md) contains all environment variables that are used in the scripts including default values and description. It can updated as described in [Update Environment Variable Reference](./COMMANDS.md#update-environment-variable-reference) of the [Commands Reference](./COMMANDS.md).
100+
97101
## 🤔 Questions & Answers
98102

99103
- How can i run an analysis locally?
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
#!/usr/bin/env bash
2+
3+
# Extracts the environment variable declarations including default values from a script file and appends it to a markdown file as table columns.
4+
5+
# Markdown file name
6+
markdownFile="ENVIRONMENT_VARIABLES.md"
7+
8+
# Get the file to search for environment variables from the first (and only) command line option
9+
filePath="$1"
10+
11+
# Check if the given file exists
12+
if [ -z "${filePath}" ] ; then
13+
echo "findEnvironmentVariables: Please provide a file name."
14+
exit 0
15+
fi
16+
17+
# Check if the given file exists
18+
if [ ! -f "./${filePath}" ] ; then
19+
echo "findEnvironmentVariables: File ${filePath} doesn't exist."
20+
exit 0
21+
fi
22+
23+
# Create the markdown file if it doesn't exist with the header of the table
24+
if [ ! -f "./${markdownFile}" ] ; then
25+
echo "findEnvironmentVariables: Creating ${markdownFile}..."
26+
{
27+
echo "# Environment Variables Reference"
28+
echo ""
29+
echo "This document serves as a reference for all environment variables that are supported by the script files."
30+
echo "It provides a table listing each environment variable, its default value and its corresponding description provided as a inline comment."
31+
echo ""
32+
echo "| Environment Variable Name | Default | Description |"
33+
echo "| ----------------------------------- | ----------------------------------- | ------------------------------------------------------ |"
34+
} > ${markdownFile}
35+
fi
36+
37+
# Regular expression that extracts the environment variable name (1st group), its default value (2nd group) and its description (3rd group)
38+
regular_expression='^[[:space:]]*([A-Za-z_][A-Za-z0-9_]*)=\${[A-Za-z_][A-Za-z0-9_]*:-"([^"]*)"}[[:space:]]*#*[[:space:]]*(.*)'
39+
40+
environmentVariables=$(\
41+
# Use grep to find lines with the pattern you specified
42+
grep -E "${regular_expression}" "$filePath" |
43+
# Use sed to extract the variable name and default value
44+
sed -E "s/${regular_expression}/\1;\2;\3/" |
45+
# Use awk to format the output as requested
46+
awk -F ';' '{ printf "%-37s | %-35s | %s |\n", $1, $2, $3 }'\
47+
)
48+
49+
# Iterate over each environment variable line by line
50+
while IFS= read -r environmentVariable; do
51+
# Append the previously found environment variable to the markdown file if it isn't contained there yet.
52+
if ! grep -q "${environmentVariable}" "$markdownFile"; then
53+
echo "${environmentVariable}" >> ${markdownFile}
54+
fi
55+
done <<< "$environmentVariables"
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#!/usr/bin/env bash
2+
3+
# Runs "appendEnvironmentVariable.sh" for every script file in the current directory and its sub directories.
4+
5+
# Requires appendEnvironmentVariable.sh
6+
7+
## Get this "scripts" directory if not already set
8+
# Even if $BASH_SOURCE is made for Bourne-like shells it is also supported by others and therefore here the preferred solution.
9+
# CDPATH reduces the scope of the cd command to potentially prevent unintended directory changes.
10+
# This way non-standard tools like readlink aren't needed.
11+
SCRIPTS_DIR=${SCRIPTS_DIR:-$( CDPATH=. cd -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd -P )}
12+
echo "generateEnvironmentVariablesReference: SCRIPTS_DIR=${SCRIPTS_DIR}"
13+
14+
# Loop through all script files in the current directory
15+
find . -type f -name "*.sh" | sort | while read -r scriptFile; do
16+
echo "generateEnvironmentVariablesReference: Searching for environment variables in ${scriptFile}"
17+
source "${SCRIPTS_DIR}/appendEnvironmentVariables.sh" "${scriptFile}"
18+
done

0 commit comments

Comments
 (0)