github.com/annwntech/go-micro/v2@v2.9.5/config/reader/preprocessor.go (about)

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