mirror of
https://github.com/kemko/nomad.git
synced 2026-01-07 10:55:42 +03:00
Added config reader for booleans
This commit is contained in:
@@ -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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user