diff options
author | Kevin Chabowski <kevin@kch42.de> | 2013-06-29 13:57:32 +0200 |
---|---|---|
committer | Kevin Chabowski <kevin@kch42.de> | 2013-06-29 13:57:32 +0200 |
commit | 98ac6348134f168168736ff8d7206c4a68645406 (patch) | |
tree | aac551c7439542e06af3bf8bb943180d4d56dc9d | |
parent | 2c6b44bfe3d13fa1dca01a982ed2cecdc671a513 (diff) | |
download | simpleconf-98ac6348134f168168736ff8d7206c4a68645406.tar.gz simpleconf-98ac6348134f168168736ff8d7206c4a68645406.tar.bz2 simpleconf-98ac6348134f168168736ff8d7206c4a68645406.zip |
Some convenience functions
-rw-r--r-- | simpleconf.go | 28 |
1 files changed, 28 insertions, 0 deletions
diff --git a/simpleconf.go b/simpleconf.go index 3dd6ef6..b204946 100644 --- a/simpleconf.go +++ b/simpleconf.go @@ -26,6 +26,7 @@ package simpleconf import ( "bufio" + "bytes" "errors" "fmt" "io" @@ -107,6 +108,17 @@ func Load(r io.Reader) (config Config, outerr error) { return } +// LoadByFilename behaves like Load, but will open the file for you. +func LoadByFilename(fn string) (Config, error) { + f, err := os.Open(fn) + if err != nil { + return nil, err + } + defer f.Close() + + return Load(f) +} + // Errors of the `Get...` functions. var ( NotFound = errors.New("Section or key not found.") @@ -210,3 +222,19 @@ func (c Config) GetFile(flag int, perm os.FileMode, section, key string) (*os.Fi func (c Config) GetFileReadonly(section, key string) (*os.File, error) { return c.GetFile(os.O_RDONLY, 0, section, key) } + +// GetFileContent reads the content of the file with the name in the config and returns the content. +func (c Config) GetFileContent(section, key string) ([]byte, error) { + f, err := c.GetFileReadonly(section, key) + if err != nil { + return nil, err + } + defer f.Close() + + buf := new(bytes.Buffer) + if _, err := io.Copy(buf, f); err != nil { + return nil, err + } + + return buf.Bytes(), nil +} |