(new): rich message support
(fix): runtime reliability (tests): regression coverage (doc): v1.1 release notes
This commit is contained in:
+15
-2
@@ -127,8 +127,18 @@ func (codec BotOptsFileJSONCodec) Save(filename string, opts *BotOpts) error {
|
||||
return SaveBotOptsFile(codec, filename, opts)
|
||||
}
|
||||
|
||||
// EscapeEnv escapes an environment value for use inside a JSON string.
|
||||
func (codec BotOptsFileJSONCodec) EscapeEnv(s string) string {
|
||||
data, _ := json.Marshal(s)
|
||||
return string(data[1 : len(data)-1])
|
||||
}
|
||||
|
||||
var envParameterRegex = regexp.MustCompile(`\{\{\s*(\w+)\s*\}\}`)
|
||||
|
||||
type botOptsFileEnvEscaper interface {
|
||||
EscapeEnv(string) string
|
||||
}
|
||||
|
||||
// BotOptsFileCodec decodes and encodes BotOpts file formats.
|
||||
type BotOptsFileCodec interface {
|
||||
FromBytes([]byte) (*BotOpts, error)
|
||||
@@ -148,7 +158,7 @@ func LoadBotOptsFile(codec BotOptsFileCodec, filename string) (*BotOpts, error)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
data = expandEnvPlaceholdersInFile(data)
|
||||
data = expandEnvPlaceholdersInFile(codec, data)
|
||||
return codec.FromBytes(data)
|
||||
}
|
||||
|
||||
@@ -165,7 +175,7 @@ func SaveBotOptsFile(codec BotOptsFileCodec, filename string, opts *BotOpts) err
|
||||
return nil
|
||||
}
|
||||
|
||||
func expandEnvPlaceholdersInFile(data []byte) []byte {
|
||||
func expandEnvPlaceholdersInFile(codec BotOptsFileCodec, data []byte) []byte {
|
||||
return envParameterRegex.ReplaceAllFunc(data, func(match []byte) []byte {
|
||||
group := envParameterRegex.FindSubmatch(match)
|
||||
if len(group) != 2 {
|
||||
@@ -173,6 +183,9 @@ func expandEnvPlaceholdersInFile(data []byte) []byte {
|
||||
}
|
||||
key := group[1]
|
||||
value := os.Getenv(string(key))
|
||||
if escaper, ok := codec.(botOptsFileEnvEscaper); ok {
|
||||
value = escaper.EscapeEnv(value)
|
||||
}
|
||||
return []byte(value)
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user