Beispiel #1
0
// ProcessConfig replaces references of environment varialbes for the given data
// Support variable syntax: $varname, ${varname}
func ProcessConfig(data []byte, e *env.Env, escapeChar rune) ([]byte, error) {
	var result []byte
	var sc scanner.Scanner
	sc.Init(bytes.NewReader(data))

DONE:
	for {
		switch ch := sc.Peek(); ch {
		default:
			result = append(result, byte(sc.Next()))
		case scanner.EOF:
			break DONE
		case escapeChar:
			curr, next := sc.Next(), sc.Peek()
			if next != '$' {
				result = append(result, byte(curr))
			}

			if next != scanner.EOF {
				result = append(result, byte(sc.Next()))
			}

		case '$':
			name, err := parseVariable(&sc)

			if err != nil {
				pos := sc.Pos()
				return result, fmt.Errorf(`parseError:%d:%d: %v %q`, pos.Line, pos.Offset, err, name)
			}
			result = append(result, e.Get(string(name))...)
		}
	}
	return result, nil
}
Beispiel #2
0
func addConfigToEnv(keyPrefix string, conf map[string]interface{}, ev *env.Env) {
	for k, v := range conf {
		key := keyPrefix + k
		switch t := v.(type) {
		case string:
			ev.Set(key, t)
		case int64:
			ev.Set(key, strconv.FormatInt(t, 10))
		case float64:
			ev.Set(key, strconv.FormatFloat(t, 'f', -1, 64))
		case map[string]interface{}:
			addConfigToEnv(key+"_", t, ev)
		}
	}
}