feat: Possibility to use custom env variables in templates (#165)

This commit is contained in:
Paramtamtam 2023-01-29 15:25:38 +04:00 committed by GitHub
parent 252618a975
commit 1ec17caa1d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 20 additions and 0 deletions

View File

@ -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] - `--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] - 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 [#163]:https://github.com/tarampampam/error-pages/pull/163
## v2.19.0 ## v2.19.0

View File

@ -10,6 +10,7 @@ import (
"time" "time"
"github.com/pkg/errors" "github.com/pkg/errors"
"github.com/tarampampam/error-pages/internal/version" "github.com/tarampampam/error-pages/internal/version"
) )
@ -30,6 +31,7 @@ var tplFnMap = template.FuncMap{ //nolint:gochecknoglobals
return 0 return 0
}, },
"env": os.Getenv,
} }
var ErrClosed = errors.New("closed") var ErrClosed = errors.New("closed")

View File

@ -2,11 +2,14 @@ package tpl_test
import ( import (
"math/rand" "math/rand"
"os"
"strconv" "strconv"
"sync" "sync"
"testing" "testing"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/tarampampam/error-pages/internal/tpl" "github.com/tarampampam/error-pages/internal/tpl"
) )
@ -14,6 +17,10 @@ func Test_Render(t *testing.T) {
renderer := tpl.NewTemplateRenderer() renderer := tpl.NewTemplateRenderer()
defer func() { _ = renderer.Close() }() 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 { for name, tt := range map[string]struct {
giveContent string giveContent string
giveProps tpl.Properties giveProps tpl.Properties
@ -67,6 +74,11 @@ func Test_Render(t *testing.T) {
giveProps: tpl.Properties{L10nDisabled: true}, giveProps: tpl.Properties{L10nDisabled: true},
wantContent: "Y", wantContent: "Y",
}, },
"env": {
giveContent: `{{ env "TEST_ENV_VAR" }}`,
wantContent: "unit-test",
},
} { } {
t.Run(name, func(t *testing.T) { t.Run(name, func(t *testing.T) {
content, err := renderer.Render([]byte(tt.giveContent), tt.giveProps) content, err := renderer.Render([]byte(tt.giveContent), tt.giveProps)