Skip to main content

web/caddyfmt: exclude Cog templates; print only formatted files

ID
2935542
date
2026-04-18 11:33:00+00:00
author
Alex Chan <alex@alexwlchan.net>
parent
55a6c72
message
web/caddyfmt: exclude Cog templates; print only formatted files
changed files
1 file, 18 additions, 4 deletions

Changed files

web/caddyfmt (199) → web/caddyfmt (603)

diff --git a/web/caddyfmt b/web/caddyfmt
index 69b9129..b50c3dc 100755
--- a/web/caddyfmt
+++ b/web/caddyfmt
@@ -1,9 +1,23 @@
 #!/usr/bin/env bash
+# Look for any Caddyfiles in the current directory, and use `caddy fmt`
+# to format them.
+#
+# This skips any Caddyfiles that contain `[[[cog`, which is a sign that
+# they use Cog for templates: https://alexwlchan.net/2026/cog-in-my-caddy/
 
 set -o errexit
 set -o nounset
 
-find . \
-    -name Caddyfile \
-    -o -name '*.Caddyfile' ! -name 'redirects.Caddyfile' | \
-    xargs -I '{}' --verbose caddy fmt --overwrite '{}'
+format_caddyfile() {
+  caddy fmt "$1" >/dev/null 2>&1
+  if (( $? != 0 )); then
+    echo "$1"
+    caddy fmt --overwrite "$1"
+  fi
+}
+
+export -f format_caddyfile
+
+find . \( -name Caddyfile -o -name '*.Caddyfile' \) -print0 \
+  | xargs -0 grep -L '\[\[\[cog' \
+  | xargs -I '{}' bash -c 'format_caddyfile "$1"' _ {}