micro-config-file/options.go
Vasiliy Tolstov 8368768957
Some checks are pending
build / test (push) Waiting to run
build / lint (push) Waiting to run
codeql / analyze (go) (push) Waiting to run
add text transform support
Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
2024-04-17 16:49:21 +03:00

53 lines
1.0 KiB
Go

package file
import (
"io"
"os"
"regexp"
"go.unistack.org/micro/v4/options"
"golang.org/x/text/transform"
)
type pathKey struct{}
func Path(path string) options.Option {
return options.ContextOption(pathKey{}, path)
}
type readerKey struct{}
func Reader(r io.Reader) options.Option {
return options.ContextOption(readerKey{}, r)
}
type transformerKey struct{}
type TransformerFunc func(src []byte, index []int) []byte
func Transformer(t transform.Transformer) options.Option {
return options.ContextOption(transformerKey{}, t)
}
func NewEnvTransformer(rs string, trimLeft, trimRight int) (*EnvTransformer, error) {
re, err := regexp.Compile(rs)
if err != nil {
return nil, err
}
return &EnvTransformer{
Regexp: re,
TransformerFunc: func(src []byte, index []int) []byte {
var envKey string
if len(src) > index[1]-trimRight {
envKey = string(src[index[0]+trimLeft : index[1]-trimRight])
}
if envVal, ok := os.LookupEnv(envKey); ok {
return []byte(envVal)
}
return src[index[0]:index[1]]
},
}, nil
}