ing
This commit is contained in:
parent
e413bd0bf6
commit
d7879b0149
|
@ -81,6 +81,7 @@ type Configurator interface {
|
||||||
UnmarshalKey(key string, rawVal interface{}) error
|
UnmarshalKey(key string, rawVal interface{}) error
|
||||||
Unmarshal(rawVal interface{}) error
|
Unmarshal(rawVal interface{}) error
|
||||||
UnmarshalExact(rawVal interface{}) error
|
UnmarshalExact(rawVal interface{}) error
|
||||||
|
Marshal(configType string) ([]byte, error)
|
||||||
BindPFlags(flags *pflag.FlagSet) error
|
BindPFlags(flags *pflag.FlagSet) error
|
||||||
BindPFlag(key string, flag *pflag.Flag) error
|
BindPFlag(key string, flag *pflag.Flag) error
|
||||||
BindFlagValues(flags FlagValueSet) (err error)
|
BindFlagValues(flags FlagValueSet) (err error)
|
||||||
|
@ -594,6 +595,13 @@ func (c *configurator) searchMap(source map[string]interface{}, path []string) i
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Marshal marshals the config to []byte.
|
||||||
|
func Marshal(configType string) ([]byte, error) { return _c.Marshal(configType) }
|
||||||
|
func (c *configurator) Marshal(configType string) ([]byte, error) {
|
||||||
|
|
||||||
|
return marshallConfig(c.config, configType)
|
||||||
|
}
|
||||||
|
|
||||||
// UnmarshalKey takes a single key and unmarshals it into a Struct.
|
// UnmarshalKey takes a single key and unmarshals it into a Struct.
|
||||||
func UnmarshalKey(key string, rawVal interface{}) error { return _c.UnmarshalKey(key, rawVal) }
|
func UnmarshalKey(key string, rawVal interface{}) error { return _c.UnmarshalKey(key, rawVal) }
|
||||||
func (c *configurator) UnmarshalKey(key string, rawVal interface{}) error {
|
func (c *configurator) UnmarshalKey(key string, rawVal interface{}) error {
|
||||||
|
|
37
util.go
37
util.go
|
@ -28,6 +28,16 @@ func (pe ConfigParseError) Error() string {
|
||||||
return fmt.Sprintf("While parsing config: %s", pe.err.Error())
|
return fmt.Sprintf("While parsing config: %s", pe.err.Error())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ConfigMarshalError denotes failing to marshal configuration.
|
||||||
|
type ConfigMarshalError struct {
|
||||||
|
err error
|
||||||
|
}
|
||||||
|
|
||||||
|
// Error returns the formatted marshal error.
|
||||||
|
func (me ConfigMarshalError) Error() string {
|
||||||
|
return fmt.Sprintf("While marshaling config: %s", me.err.Error())
|
||||||
|
}
|
||||||
|
|
||||||
func absPathify(inPath string) (string, error) {
|
func absPathify(inPath string) (string, error) {
|
||||||
if strings.HasPrefix(inPath, "$HOME") {
|
if strings.HasPrefix(inPath, "$HOME") {
|
||||||
inPath = userHomeDir() + inPath[5:]
|
inPath = userHomeDir() + inPath[5:]
|
||||||
|
@ -81,6 +91,33 @@ func exists(path string) (bool, error) {
|
||||||
return false, err
|
return false, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func marshallConfig(c map[string]interface{}, configType string) ([]byte, error) {
|
||||||
|
var (
|
||||||
|
buf []byte
|
||||||
|
err error
|
||||||
|
)
|
||||||
|
|
||||||
|
switch strings.ToLower(configType) {
|
||||||
|
case "yaml", "yml":
|
||||||
|
if buf, err = yaml.Marshal(c); err != nil {
|
||||||
|
return nil, ConfigMarshalError{err}
|
||||||
|
}
|
||||||
|
|
||||||
|
case "json":
|
||||||
|
if buf, err = json.Marshal(c); err != nil {
|
||||||
|
return nil, ConfigMarshalError{err}
|
||||||
|
}
|
||||||
|
|
||||||
|
case "toml":
|
||||||
|
if buf, err = toml.Marshal(c); err != nil {
|
||||||
|
return nil, ConfigMarshalError{err}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
return buf, nil
|
||||||
|
}
|
||||||
|
|
||||||
func unmarshallConfigReader(in io.Reader, c map[string]interface{}, configType string) error {
|
func unmarshallConfigReader(in io.Reader, c map[string]interface{}, configType string) error {
|
||||||
buf := new(bytes.Buffer)
|
buf := new(bytes.Buffer)
|
||||||
buf.ReadFrom(in)
|
buf.ReadFrom(in)
|
||||||
|
|
Loading…
Reference in New Issue
Block a user