Last active
September 17, 2018 04:35
-
-
Save saidaspen/7683098fbd02bd2595f0abcd5641ad2d to your computer and use it in GitHub Desktop.
Did configuration - Seems overly complex!
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Config holds the properties and values used for configuration | |
type Config struct { | |
dateFormat string | |
} | |
// DefaultConfig is the set of configuration values used if none other is given. | |
var DefaultConfig = Config{"2006-01-02"} | |
// FromFile returns the configuration to use for did | |
func FromFile(path string) (c Config, e error) { | |
c = DefaultConfig | |
if lines, err := File2lines(path); err == nil { | |
for _, line := range lines { | |
prop, val, propErr := propertyOf(line) | |
if propErr == nil && prop == "dateformat" && ValidDateFormat(val) { | |
format, formatErr := ToGoDateFormat(val) | |
if formatErr == nil { | |
c.dateFormat = format | |
} | |
} else if propErr != nil { | |
e = propErr | |
} | |
} | |
} else if err != nil { | |
e = err | |
} | |
return | |
} | |
func propertyOf(str string) (key, val string, err error) { | |
split := strings.Split(strings.TrimSpace(strings.TrimRight(str, "\n")), "=") | |
if len(split) == 2 { | |
return split[0], split[1], nil | |
} | |
return "", "", fmt.Errorf(fmt.Sprintf("String '%s' cannot be read as property.", str)) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment