From 1ec17caa1d11e3e03635a7ed522360c09a21eba8 Mon Sep 17 00:00:00 2001 From: Paramtamtam <7326800+tarampampam@users.noreply.github.com> Date: Sun, 29 Jan 2023 15:25:38 +0400 Subject: [PATCH] feat: Possibility to use custom env variables in templates (#165) --- CHANGELOG.md | 6 ++++++ internal/tpl/render.go | 2 ++ internal/tpl/render_test.go | 12 ++++++++++++ 3 files changed, 20 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 20b0e89..df8b688 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,6 +12,12 @@ The format is based on [Keep a Changelog][keepachangelog] and this project adher - `--config-file` flag is not global anymore (use `error-pages (serve|build) --config-file ...` instead of `error-pages --config-file ... (serve|build) ...`) [#163] - Flags `--verbose`, `--debug` and `--log-json` are deprecated, use `--log-level` and `--log-format` instead [#163] +### Added + +- Possibility to use custom env variables in templates [#164], [#165] + +[#164]:https://github.com/tarampampam/error-pages/issues/164 +[#165]:https://github.com/tarampampam/error-pages/pull/165 [#163]:https://github.com/tarampampam/error-pages/pull/163 ## v2.19.0 diff --git a/internal/tpl/render.go b/internal/tpl/render.go index 7694393..cc050cd 100644 --- a/internal/tpl/render.go +++ b/internal/tpl/render.go @@ -10,6 +10,7 @@ import ( "time" "github.com/pkg/errors" + "github.com/tarampampam/error-pages/internal/version" ) @@ -30,6 +31,7 @@ var tplFnMap = template.FuncMap{ //nolint:gochecknoglobals return 0 }, + "env": os.Getenv, } var ErrClosed = errors.New("closed") diff --git a/internal/tpl/render_test.go b/internal/tpl/render_test.go index 9ddf44a..0b1d4fa 100644 --- a/internal/tpl/render_test.go +++ b/internal/tpl/render_test.go @@ -2,11 +2,14 @@ package tpl_test import ( "math/rand" + "os" "strconv" "sync" "testing" "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" + "github.com/tarampampam/error-pages/internal/tpl" ) @@ -14,6 +17,10 @@ func Test_Render(t *testing.T) { renderer := tpl.NewTemplateRenderer() defer func() { _ = renderer.Close() }() + require.NoError(t, os.Setenv("TEST_ENV_VAR", "unit-test")) + + defer func() { require.NoError(t, os.Unsetenv("TEST_ENV_VAR")) }() + for name, tt := range map[string]struct { giveContent string giveProps tpl.Properties @@ -67,6 +74,11 @@ func Test_Render(t *testing.T) { giveProps: tpl.Properties{L10nDisabled: true}, wantContent: "Y", }, + + "env": { + giveContent: `{{ env "TEST_ENV_VAR" }}`, + wantContent: "unit-test", + }, } { t.Run(name, func(t *testing.T) { content, err := renderer.Render([]byte(tt.giveContent), tt.giveProps)