#!/usr/bin/env bash
#####################
# Checks that every chart directory is named exactly like the `name:` field of
# its Chart.yaml, for the layout <root>/<chartDir>/Chart.yaml at any depth.
#
# The two must agree because helm names the packaged archive after the `name:`
# field, while the pipeline builds the path and the .tgz filename from the
# directory name - a mismatch only shows up as a missing file at `helm push`.
#
# Usage: ./validate-chart-names.sh [root ...] (default root: .)
# Prints every mismatch and exits 1; exits 0 when everything agrees.
#####################
set -uo pipefail
roots=("$@")
[[ "${#roots[@]}" -gt 0 ]] || roots=(".")
# Top-level `name:` of a Chart.yaml. Only column-0 keys are considered, so the
# indented `- name:` entries under `dependencies:` are ignored. Strips CR (the
# charts are checked out with CRLF), inline comments and trailing blanks.
chart_name() {
awk '
/^name:/ {
sub(/^name:[ \t]*/, "")
sub(/\r$/, "")
sub(/[ \t]+#.*$/, "")
sub(/[ \t]+$/, "")
print
exit
}
' "$1"
}
checked=0
bad=0
for root in "${roots[@]}"; do
if [[ ! -d "$root" ]]; then
echo "##[error] no such directory: $root" >&2
bad=$((bad + 1))
continue
fi
while IFS= read -r -d '' chartFile; do
checked=$((checked + 1))
dirName="$(basename "$(dirname "$chartFile")")"
name="$(chart_name "$chartFile")"
name="${name%\"}"; name="${name#\"}"
name="${name%\'}"; name="${name#\'}"
if [[ -z "$name" ]]; then
echo "NO NAME $chartFile"
echo " no top-level 'name:' field"
bad=$((bad + 1))
elif [[ "$name" != "$dirName" ]]; then
echo "MISMATCH $chartFile"
echo " directory: $dirName"
echo " name: $name"
bad=$((bad + 1))
fi
done < <(find "$root" -type f -name Chart.yaml -print0)
done
echo "checked $checked Chart.yaml file(s)"
if [[ "$bad" -ne 0 ]]; then
echo "##[error] $bad chart(s) whose directory and name: disagree"
exit 1
fi
echo "all chart directories match their name: field"