github.com/yandex/pandora@v0.5.32/lib/confutil/property_var_resolver.go (about)

     1  package confutil
     2  
     3  import (
     4  	"bufio"
     5  	"fmt"
     6  	"os"
     7  	"strings"
     8  )
     9  
    10  // Resolve custom tag token with property variable value. Allow read from properties file
    11  // for example: secret: '${property: /etc/datasources/secret.properties#tvm_secret}'
    12  var PropertyTagResolver TagResolver = propertyTokenResolver
    13  
    14  func propertyTokenResolver(in string) (string, error) {
    15  	split := strings.SplitN(in, "#", 2)
    16  	filename, property := split[0], split[1]
    17  	file, err := os.Open(filename)
    18  	if err != nil {
    19  		return "", fmt.Errorf("cannot open file: '%v'", filename)
    20  	}
    21  
    22  	defer file.Close()
    23  	scanner := bufio.NewScanner(file)
    24  	for scanner.Scan() {
    25  		line := scanner.Text()
    26  		if strings.Contains(line, "=") {
    27  			kv := strings.SplitN(line, "=", 2)
    28  			if kv[0] == property {
    29  				return kv[1], nil
    30  			}
    31  		}
    32  	}
    33  
    34  	return "", fmt.Errorf("no such property '%v', in file '%v'", property, filename)
    35  }