Added config reader for booleans

This commit is contained in:
Chris Bednarski
2015-11-16 19:30:37 -08:00
parent 640337547a
commit f7784209f4

View File

@@ -1,7 +1,9 @@
package config
import (
"fmt"
"io"
"strconv"
"github.com/hashicorp/nomad/nomad/structs"
)
@@ -73,3 +75,26 @@ func (c *Config) ReadDefault(id string, defaultValue string) string {
}
return defaultValue
}
// ReadBool parses the specified option as a boolean.
func (c *Config) ReadBool(id string) (bool, error) {
val, ok := c.Options[id]
if !ok {
return false, nil
}
bval, err := strconv.ParseBool(val)
if err != nil {
return false, fmt.Errorf("Failed to parse %s as bool: %s", val, err)
}
return bval, nil
}
// ReadBoolDefault tries to parse the specified option as a boolean. If there is
// an error in parsing, the default option is returned.
func (c *Config) ReadBoolDefault(id string, defaultValue bool) bool {
val, err := c.ReadBool()
if err != nil {
return defaultValue
}
return val
}