Skip to content

Commit

Permalink
feat: Possibility to use custom env variables in templates (#165)
Browse files Browse the repository at this point in the history
  • Loading branch information
tarampampam authored Jan 29, 2023
1 parent 252618a commit 1ec17ca
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 0 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions internal/tpl/render.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"time"

"github.com/pkg/errors"

"github.com/tarampampam/error-pages/internal/version"
)

Expand All @@ -30,6 +31,7 @@ var tplFnMap = template.FuncMap{ //nolint:gochecknoglobals

return 0
},
"env": os.Getenv,
}

var ErrClosed = errors.New("closed")
Expand Down
12 changes: 12 additions & 0 deletions internal/tpl/render_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,25 @@ 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"
)

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
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 1ec17ca

Please sign in to comment.