|
| 1 | +#!/usr/bin/env bash |
| 2 | + |
| 3 | +# WARNING: Please DO NOT edit this file! It is maintained in the Repository Template (https://github.com/NHSDigital/nhs-notify-repository-template). Raise a PR instead. |
| 4 | + |
| 5 | +set -euo pipefail |
| 6 | + |
| 7 | +# Terraform-docs command wrapper. It will run the command natively if terraform-docs is |
| 8 | +# installed, otherwise it will run it in a Docker container. |
| 9 | +# Run terraform-docs for generating Terraform module documentation code. |
| 10 | +# |
| 11 | +# Usage: |
| 12 | +# $ ./terraform-docs.sh [directory] |
| 13 | +# ============================================================================== |
| 14 | + |
| 15 | +function main() { |
| 16 | + |
| 17 | + cd "$(git rev-parse --show-toplevel)" |
| 18 | + |
| 19 | + local dir_to_document=${1:-.} |
| 20 | + |
| 21 | + if command -v terraform-docs > /dev/null 2>&1 && ! is-arg-true "${FORCE_USE_DOCKER:-false}"; then |
| 22 | + # shellcheck disable=SC2154 |
| 23 | + run-terraform-docs-natively "$dir_to_document" |
| 24 | + else |
| 25 | + run-terraform-docs-in-docker "$dir_to_document" |
| 26 | + fi |
| 27 | +} |
| 28 | + |
| 29 | +# Run terraform-docs on the specified directory. |
| 30 | +# Arguments: |
| 31 | +# $1 - Directory to document |
| 32 | +function run-terraform-docs-natively() { |
| 33 | + |
| 34 | + local dir_to_scan="$1" |
| 35 | + echo "Terraform-docs found locally, running natively" |
| 36 | + if [ -d "$dir_to_scan" ]; then |
| 37 | + echo "Running Terraform-docs on directory: $dir_to_scan" |
| 38 | + terraform-docs \ |
| 39 | + -c scripts/config/terraform-docs.yml \ |
| 40 | + --output-file README.md \ |
| 41 | + "$dir_to_scan" |
| 42 | + fi |
| 43 | +} |
| 44 | + |
| 45 | +function run-terraform-docs-in-docker() { |
| 46 | + |
| 47 | + # shellcheck disable=SC1091 |
| 48 | + source ./scripts/docker/docker.lib.sh |
| 49 | + local dir_to_scan="$1" |
| 50 | + |
| 51 | + # shellcheck disable=SC2155 |
| 52 | + local image=$(name=quay.io/terraform-docs/terraform-docs docker-get-image-version-and-pull) |
| 53 | + # shellcheck disable=SC2086 |
| 54 | + echo "Terraform-docs not found locally, running in Docker Container" |
| 55 | + echo "Running Terraform-docs on directory: $dir_to_scan" |
| 56 | + docker run --rm --platform linux/amd64 \ |
| 57 | + --volume "$PWD":/workdir \ |
| 58 | + --workdir /workdir \ |
| 59 | + "$image" \ |
| 60 | + -c scripts/config/terraform-docs.yml \ |
| 61 | + --output-file README.md \ |
| 62 | + "$dir_to_scan" |
| 63 | + |
| 64 | +} |
| 65 | +# ============================================================================== |
| 66 | + |
| 67 | +function is-arg-true() { |
| 68 | + |
| 69 | + if [[ "$1" =~ ^(true|yes|y|on|1|TRUE|YES|Y|ON)$ ]]; then |
| 70 | + return 0 |
| 71 | + else |
| 72 | + return 1 |
| 73 | + fi |
| 74 | +} |
| 75 | + |
| 76 | +# ============================================================================== |
| 77 | + |
| 78 | +is-arg-true "${VERBOSE:-false}" && set -x |
| 79 | + |
| 80 | +main "$@" |
| 81 | + |
| 82 | +exit 0 |
0 commit comments