diff options
-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 +} |