Skip to content

Commit

Permalink
Merge branch 'main' into authz-opa-sdk
Browse files Browse the repository at this point in the history
  • Loading branch information
markphelps authored Jun 19, 2024
2 parents 0f12ff7 + 1804b13 commit cd1b7e8
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 1 deletion.
9 changes: 9 additions & 0 deletions .all-contributorsrc
Original file line number Diff line number Diff line change
Expand Up @@ -448,6 +448,15 @@
"contributions": [
"code"
]
},
{
"login": "mbezhanov",
"name": "Marin Bezhanov",
"avatar_url": "https://avatars.githubusercontent.com/u/785542?v=4",
"profile": "https://www.linkedin.com/in/mbezhanov",
"contributions": [
"code"
]
}
],
"contributorsPerLine": 7,
Expand Down
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -397,6 +397,9 @@ Thanks goes to these wonderful people ([emoji key](https://allcontributors.org/d
<td align="center" valign="top" width="14.28%"><a href="https://github.com/kyryl-perepelytsia"><img src="https://avatars.githubusercontent.com/u/46731109?v=4?s=100" width="100px;" alt="Kyryl Perepelytsia"/><br /><sub><b>Kyryl Perepelytsia</b></sub></a><br /><a href="https://github.com/flipt-io/flipt/commits?author=kyryl-perepelytsia" title="Code">💻</a></td>
<td align="center" valign="top" width="14.28%"><a href="http://pythonhacker24.github.io"><img src="https://avatars.githubusercontent.com/u/72488360?v=4?s=100" width="100px;" alt="Aditya Patil"/><br /><sub><b>Aditya Patil</b></sub></a><br /><a href="https://github.com/flipt-io/flipt/commits?author=PythonHacker24" title="Code">💻</a></td>
</tr>
<tr>
<td align="center" valign="top" width="14.28%"><a href="https://www.linkedin.com/in/mbezhanov"><img src="https://avatars.githubusercontent.com/u/785542?v=4?s=100" width="100px;" alt="Marin Bezhanov"/><br /><sub><b>Marin Bezhanov</b></sub></a><br /><a href="https://github.com/flipt-io/flipt/commits?author=mbezhanov" title="Code">💻</a></td>
</tr>
</tbody>
</table>

Expand Down
24 changes: 23 additions & 1 deletion internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"os"
"path/filepath"
"reflect"
"regexp"
"slices"
"strings"
"time"
Expand All @@ -27,10 +28,12 @@ const (
)

var (
_ validator = (*Config)(nil)
_ validator = (*Config)(nil)
envsubst = regexp.MustCompile(`^\${([a-zA-Z_]+[a-zA-Z0-9_]*)}$`)
)

var DecodeHooks = []mapstructure.DecodeHookFunc{
stringToEnvsubstHookFunc(),
mapstructure.StringToTimeDurationHookFunc(),
stringToSliceHookFunc(),
stringToEnumHookFunc(stringToCacheBackend),
Expand Down Expand Up @@ -475,6 +478,25 @@ func experimentalFieldSkipHookFunc(types ...reflect.Type) mapstructure.DecodeHoo
}
}

// stringToEnvsubstHookFunc returns a DecodeHookFunc that substitutes
// `${VARIABLE}` strings with their matching environment variables.
func stringToEnvsubstHookFunc() mapstructure.DecodeHookFunc {
return func(
f reflect.Type,
t reflect.Type,
data interface{}) (interface{}, error) {
if f.Kind() != reflect.String || f != reflect.TypeOf("") {
return data, nil
}
str := data.(string)
if !envsubst.MatchString(str) {
return data, nil
}
key := envsubst.ReplaceAllString(str, `$1`)
return os.Getenv(key), nil
}
}

// stringToSliceHookFunc returns a DecodeHookFunc that converts
// string to []string by splitting using strings.Fields().
func stringToSliceHookFunc() mapstructure.DecodeHookFunc {
Expand Down
14 changes: 14 additions & 0 deletions internal/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,20 @@ func TestLoad(t *testing.T) {
return cfg
},
},
{
name: "environment variable substitution",
path: "./testdata/envsubst.yml",
envOverrides: map[string]string{
"HTTP_PORT": "18080",
"LOG_FORMAT": "json",
},
expected: func() *Config {
cfg := Default()
cfg.Log.Encoding = "json"
cfg.Server.HTTPPort = 18080
return cfg
},
},
{
name: "deprecated tracing jaeger",
path: "./testdata/deprecated/tracing_jaeger.yml",
Expand Down
4 changes: 4 additions & 0 deletions internal/config/testdata/envsubst.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
server:
http_port: ${HTTP_PORT}
log:
encoding: ${LOG_FORMAT}

0 comments on commit cd1b7e8

Please sign in to comment.