Add env template helper (#121)

This commit is contained in:
Frederik Ring 2022-07-06 13:16:32 +02:00 committed by GitHub
parent c2a8cc92fc
commit 342ae5910e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 7 additions and 3 deletions

View File

@ -7,6 +7,7 @@ import (
"bytes" "bytes"
_ "embed" _ "embed"
"fmt" "fmt"
"os"
"text/template" "text/template"
"time" "time"
@ -82,6 +83,7 @@ var templateHelpers = template.FuncMap{
"formatBytesBin": func(bytes uint64) string { "formatBytesBin": func(bytes uint64) string {
return formatBytes(bytes, false) return formatBytes(bytes, false)
}, },
"env": os.Getenv,
} }
// formatBytes converts an amount of bytes in a human-readable representation // formatBytes converts an amount of bytes in a human-readable representation

View File

@ -32,8 +32,9 @@ Here is a list of all data passed to the template:
## Functions ## 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`) * `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`) * `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`) * `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

View File

@ -10,6 +10,7 @@ services:
BACKUP_PRUNING_PREFIX: test BACKUP_PRUNING_PREFIX: test
NOTIFICATION_LEVEL: info NOTIFICATION_LEVEL: info
NOTIFICATION_URLS: ${NOTIFICATION_URLS} NOTIFICATION_URLS: ${NOTIFICATION_URLS}
EXTRA_VALUE: extra-value
volumes: volumes:
- ./local:/archive - ./local:/archive
- app_data:/backup/app_data:ro - app_data:/backup/app_data:ro

View File

@ -1,5 +1,5 @@
{{ define "title_success" -}} {{ define "title_success" -}}
Successful test run, yay! Successful test run with {{ env "EXTRA_VALUE" }}, yay!
{{- end }} {{- end }}
{{ define "body_success" -}} {{ define "body_success" -}}

View File

@ -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_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') 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" fail "Unexpected notification title $MESSAGE_TITLE"
fi fi
pass "Custom notification title was used." pass "Custom notification title was used."