parent
55c201b36c
commit
5cc5aa419e
|
@ -1,7 +1,7 @@
|
|||
# gosimpleconf
|
||||
|
||||
```sh
|
||||
go get gitea.wisellama.rocks/Wisellama/gosimpleconf@v0.0.3
|
||||
go get gitea.wisellama.rocks/Wisellama/gosimpleconf@v0.0.4
|
||||
```
|
||||
|
||||
This is a small library for parsing super simple configuration files.
|
||||
|
@ -25,6 +25,9 @@ url = www.wisellama.rocks
|
|||
number_of_widgets = 1337
|
||||
pi = 3.141592653589793238462643383
|
||||
environment = production
|
||||
# Quotes are optional
|
||||
name = Dude guy
|
||||
description = "Just some dude it was"
|
||||
```
|
||||
|
||||
Then to load that config file into a map:
|
||||
|
|
|
@ -46,7 +46,7 @@ func ParseFlags(configMap ConfigMap, flagMap FlagMap) ConfigMap {
|
|||
|
||||
for k, v := range flagMap {
|
||||
if v != nil && len(*v) > 0 {
|
||||
configMap[k] = *v
|
||||
configMap[k] = trimToken(*v)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -57,7 +57,7 @@ func ParseFlags(configMap ConfigMap, flagMap FlagMap) ConfigMap {
|
|||
func Bool(value string) bool {
|
||||
v, err := strconv.ParseBool(value)
|
||||
if err != nil {
|
||||
log.Printf("error parsing bool %v - %v", v, err)
|
||||
log.Printf("error parsing bool %v - %v\n", v, err)
|
||||
v = false
|
||||
}
|
||||
|
||||
|
@ -87,8 +87,9 @@ func parseFile(config io.Reader) (ConfigMap, error) {
|
|||
return nil, fmt.Errorf("failed to parse config line: %v", trimmed)
|
||||
}
|
||||
|
||||
key := strings.TrimSpace(split[0])
|
||||
value := strings.TrimSpace(split[1])
|
||||
key := trimToken(split[0])
|
||||
value := trimToken(split[1])
|
||||
|
||||
parsedMap[key] = value
|
||||
}
|
||||
|
||||
|
@ -98,3 +99,14 @@ func parseFile(config io.Reader) (ConfigMap, error) {
|
|||
|
||||
return parsedMap, nil
|
||||
}
|
||||
|
||||
func trimToken(value string) string {
|
||||
// Trim off whitespace
|
||||
v := strings.TrimSpace(value)
|
||||
|
||||
// Trim off any surrounding quotes, everything is a string anyway
|
||||
v = strings.TrimPrefix(v, "\"")
|
||||
v = strings.TrimSuffix(v, "\"")
|
||||
|
||||
return v
|
||||
}
|
||||
|
|
|
@ -102,6 +102,31 @@ immaempty=
|
|||
validateMap(t, configMap, expectedMap)
|
||||
}
|
||||
|
||||
func TestParseWithExtraQuotes(t *testing.T) {
|
||||
var err error
|
||||
configFileStr := `
|
||||
some.name = "This is in quotes"
|
||||
extra.quotes = ""This will keep the quotes""
|
||||
internal.quotes = "Some "thing" is here"
|
||||
"This should work too" = shrug I guess
|
||||
`
|
||||
|
||||
configFile := strings.NewReader(configFileStr)
|
||||
|
||||
expectedMap := make(ConfigMap)
|
||||
expectedMap["some.name"] = "This is in quotes"
|
||||
expectedMap["extra.quotes"] = "\"This will keep the quotes\""
|
||||
expectedMap["internal.quotes"] = "Some \"thing\" is here"
|
||||
expectedMap["This should work too"] = "shrug I guess"
|
||||
|
||||
configMap, err := parseFile(configFile)
|
||||
if err != nil {
|
||||
t.Errorf("failed while parsing the file")
|
||||
}
|
||||
|
||||
validateMap(t, configMap, expectedMap)
|
||||
}
|
||||
|
||||
func validateMap(t *testing.T, given map[string]string, expected map[string]string) {
|
||||
if given == nil {
|
||||
t.Errorf("given map was nil")
|
||||
|
|
Loading…
Reference in New Issue