Skip to content

Commit

Permalink
replacer: Implement file.* global replacements
Browse files Browse the repository at this point in the history
  • Loading branch information
francislavoie committed Mar 27, 2023
1 parent 1aef807 commit 950024d
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 additions & 0 deletions replacer.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ import (
"strconv"
"strings"
"time"

"go.uber.org/zap"
)

// NewReplacer returns a new Replacer.
Expand Down Expand Up @@ -300,6 +302,26 @@ func globalDefaultReplacements(key string) (any, bool) {
return os.Getenv(key[len(envPrefix):]), true
}

// check files
// TODO: We may want to cache the file contents in case
// this is used in a hot path in a config. But for now,
// we'll just read the file every time, the kernel will
// tend to cache the file contents for us.
const filePrefix = "file."
if strings.HasPrefix(key, filePrefix) {
filename := key[len(filePrefix):]
body, err := os.ReadFile(filename)
if err != nil {
wd, _ := os.Getwd()
Log().Error("placeholder: failed to read file",
zap.String("file", filename),
zap.String("wd", wd),
zap.Error(err))
return nil, true
}
return body, true
}

switch key {
case "system.hostname":
// OK if there is an error; just return empty string
Expand Down

0 comments on commit 950024d

Please sign in to comment.