Select Git revision
-
Jano Hendriks authoredJano Hendriks authored
key_reader.go 858 B
package string_utils
import (
"os"
"strings"
)
//KeyReader is an interface to read string "<key>":"<value>" pairs
//which is common to read from the environment
//it is abstracted so the same interface can be implemented for
//reading for example from REDIS and other sources
type KeyReader interface {
Keys(prefix string) []string
GetString(key string) (value string, ok bool)
}
func EnvironmentKeyReader() KeyReader {
return envKeyReader{}
}
type envKeyReader struct{}
func (envKeyReader) GetString(key string) (value string, ok bool) {
value = os.Getenv(key)
if value == "" {
return "", false
}
return value, true
}
func (envKeyReader) Keys(prefix string) []string {
keys := []string{}
for _, env := range os.Environ() {
if strings.HasPrefix(env, prefix) {
keys = append(keys, strings.SplitN(env, "=", 2)[0])
}
}
return keys
}