preprocessor.go 416 B

1234567891011121314151617181920212223
  1. package reader
  2. import (
  3. "os"
  4. "regexp"
  5. )
  6. func ReplaceEnvVars(raw []byte) ([]byte, error) {
  7. re := regexp.MustCompile(`\$\{([A-Za-z0-9_]+)\}`)
  8. if re.Match(raw) {
  9. dataS := string(raw)
  10. res := re.ReplaceAllStringFunc(dataS, replaceEnvVars)
  11. return []byte(res), nil
  12. } else {
  13. return raw, nil
  14. }
  15. }
  16. func replaceEnvVars(element string) string {
  17. v := element[2 : len(element)-1]
  18. el := os.Getenv(v)
  19. return el
  20. }