Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .golangci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,8 @@ linters:

varnamelen:
max-distance: 10
ignore-decls:
- c *Config[T]

whitespace:
multi-if: true
Expand Down
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,14 @@
SPDX-License-Identifier: MPL-2.0
-->

Release 0.10.0
==============

- added functional options for construction
- added `WithValue` option that generates values accessible
via `.Values.valName` inside of configuration templates
- update dependencies

Release 0.9.4
=============

Expand Down
60 changes: 58 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,62 @@ func main() {
}
```

#### Using Custom Values

So far, the cases involved the convenience wrappers provided by *templig*. Beneath that layer there is a standard
functional options pattern to generate a *templig* configuration. All options start with the `With` preix.

So far, it was not possible for a programmer to provide own values to the configuration other than defining custom
environment variables. To address this shortcoming, custom values are introduced, that allow the configuration to
get that information via the `.Values` variable.

Having a templated configuration file like this one:

```yaml
id: 23
name: Interesting Name
pass: {{ .Values.pass | required "password value required" | quote }}
```

One can see the templating code between the double curly braces `{{` and `}}`.
The following program is essentially the same as in the [Simple Case](#simple-case).
It just adds the `pass` field to the configuration:

```go
package main

import (
"fmt"
"strings"

"github.com/AlphaOne1/templig"
)

// Config is the configuration structure
type Config struct {
ID int `yaml:"id"`
Name string `yaml:"name"`
Pass string `yaml:"pass"`
}

// main will read and display the configuration
func main() {
c, confErr := templig.New[Config](
templig.WithFile[Config]("my_config.yaml"),
templig.WithValue[Config]("pass", "secret"))

fmt.Printf("read errors: %v", confErr)

if confErr == nil {
fmt.Printf("ID: %v\n", c.Get().ID)
fmt.Printf("Name: %v\n", c.Get().Name)
fmt.Printf("Pass: %v\n", strings.Repeat("*", len(c.Get().Pass)))
}
}
```

It should be noted, that using the `New` method with the functional options provides also the means to intermix file
and io.Reader inputs freely.

### Validation

Expand Down Expand Up @@ -461,9 +517,9 @@ An example usage can be found [here](examples/templating/env).

The regular expression used to identify secrets to hide can be changed globally setting `templig.SecretRE` to a
different value. It also can be set for each `Config` instance using the `SetSecretRE` method. To hide, e.g., also
tokens, one could use the following (with `SecretDefaultRE` containing the original regular expression text):
identifications, one could use the following (with `SecretDefaultRE` containing the original regular expression text):

```go
c, _ := templig.FromFile[Config]("my_config.yaml")
c.SetSecretRE(regexp.MustCompile(templig.SecretDefaultRE + "|token"))
c.SetSecretRE(regexp.MustCompile(templig.SecretDefaultRE + "|identification"))
```
Loading
Loading