Skip to content
Snippets Groups Projects
Commit 8577f251 authored by Jan Semmelink's avatar Jan Semmelink
Browse files

Added key_reader to string_utils

parent e904183b
Branches
Tags
No related merge requests found
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
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment