From 98ac6348134f168168736ff8d7206c4a68645406 Mon Sep 17 00:00:00 2001 From: Kevin Chabowski Date: Sat, 29 Jun 2013 13:57:32 +0200 Subject: Some convenience functions --- simpleconf.go | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) 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 +} -- cgit v1.2.3-54-g00ecf