From 342ae5910e57df46d86c8f712d32a6f1856bbc38 Mon Sep 17 00:00:00 2001 From: Frederik Ring Date: Wed, 6 Jul 2022 13:16:32 +0200 Subject: [PATCH] Add env template helper (#121) --- cmd/backup/notifications.go | 2 ++ docs/NOTIFICATION-TEMPLATES.md | 3 ++- test/notifications/docker-compose.yml | 1 + test/notifications/notifications.tmpl | 2 +- test/notifications/run.sh | 2 +- 5 files changed, 7 insertions(+), 3 deletions(-) diff --git a/cmd/backup/notifications.go b/cmd/backup/notifications.go index 3311b8d..80af395 100644 --- a/cmd/backup/notifications.go +++ b/cmd/backup/notifications.go @@ -7,6 +7,7 @@ import ( "bytes" _ "embed" "fmt" + "os" "text/template" "time" @@ -82,6 +83,7 @@ var templateHelpers = template.FuncMap{ "formatBytesBin": func(bytes uint64) string { return formatBytes(bytes, false) }, + "env": os.Getenv, } // formatBytes converts an amount of bytes in a human-readable representation diff --git a/docs/NOTIFICATION-TEMPLATES.md b/docs/NOTIFICATION-TEMPLATES.md index 84d1da1..8af37af 100644 --- a/docs/NOTIFICATION-TEMPLATES.md +++ b/docs/NOTIFICATION-TEMPLATES.md @@ -32,8 +32,9 @@ Here is a list of all data passed to the template: ## Functions -Some formatting functions are also available: +Some formatting and helper functions are also available: * `formatTime`: formats a time object using [RFC3339](https://datatracker.ietf.org/doc/html/rfc3339) format (e.g. `2022-02-11T01:00:00Z`) * `formatBytesBin`: formats an amount of bytes using powers of 1024 (e.g. `7055258` bytes will be `6.7 MiB`) * `formatBytesDec`: formats an amount of bytes using powers of 1000 (e.g. `7055258` bytes will be `7.1 MB`) +* `env`: returns the value of the environment variable of the given key if set diff --git a/test/notifications/docker-compose.yml b/test/notifications/docker-compose.yml index a8981a6..dcda48a 100644 --- a/test/notifications/docker-compose.yml +++ b/test/notifications/docker-compose.yml @@ -10,6 +10,7 @@ services: BACKUP_PRUNING_PREFIX: test NOTIFICATION_LEVEL: info NOTIFICATION_URLS: ${NOTIFICATION_URLS} + EXTRA_VALUE: extra-value volumes: - ./local:/archive - app_data:/backup/app_data:ro diff --git a/test/notifications/notifications.tmpl b/test/notifications/notifications.tmpl index 1771da1..4080d8a 100644 --- a/test/notifications/notifications.tmpl +++ b/test/notifications/notifications.tmpl @@ -1,5 +1,5 @@ {{ define "title_success" -}} -Successful test run, yay! +Successful test run with {{ env "EXTRA_VALUE" }}, yay! {{- end }} {{ define "body_success" -}} diff --git a/test/notifications/run.sh b/test/notifications/run.sh index fe4498a..49dab08 100755 --- a/test/notifications/run.sh +++ b/test/notifications/run.sh @@ -37,7 +37,7 @@ pass "Correct number of notifications were sent when configured." MESSAGE_TITLE=$(curl -sSL http://admin:custom@localhost:8080/message | jq -r '.messages[0].title') MESSAGE_BODY=$(curl -sSL http://admin:custom@localhost:8080/message | jq -r '.messages[0].message') -if [ "$MESSAGE_TITLE" != "Successful test run, yay!" ]; then +if [ "$MESSAGE_TITLE" != "Successful test run with extra-value, yay!" ]; then fail "Unexpected notification title $MESSAGE_TITLE" fi pass "Custom notification title was used."