diff options
author | Wim <wim@42.be> | 2020-10-19 23:40:00 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-10-19 23:40:00 +0200 |
commit | 075a84427f6332aab707d283ad770d69f8816032 (patch) | |
tree | 0ff9f56a057919f3fe968e57f6f0b1c0d1f85078 /vendor/github.com/wiggin77/cfg/srcfile.go | |
parent | 950f2759bd2b20aa0bdedc3dc9a74d0dafb606d8 (diff) | |
download | matterbridge-msglm-075a84427f6332aab707d283ad770d69f8816032.tar.gz matterbridge-msglm-075a84427f6332aab707d283ad770d69f8816032.tar.bz2 matterbridge-msglm-075a84427f6332aab707d283ad770d69f8816032.zip |
Update vendor (#1265)
Diffstat (limited to 'vendor/github.com/wiggin77/cfg/srcfile.go')
-rw-r--r-- | vendor/github.com/wiggin77/cfg/srcfile.go | 63 |
1 files changed, 63 insertions, 0 deletions
diff --git a/vendor/github.com/wiggin77/cfg/srcfile.go b/vendor/github.com/wiggin77/cfg/srcfile.go new file mode 100644 index 00000000..f42c69fa --- /dev/null +++ b/vendor/github.com/wiggin77/cfg/srcfile.go @@ -0,0 +1,63 @@ +package cfg + +import ( + "os" + "time" + + "github.com/wiggin77/cfg/ini" +) + +// SrcFile is a configuration `Source` backed by a file containing +// name/value pairs or INI format. +type SrcFile struct { + AbstractSourceMonitor + ini ini.Ini + file *os.File +} + +// NewSrcFileFromFilespec creates a new SrcFile with the specified filespec. +func NewSrcFileFromFilespec(filespec string) (*SrcFile, error) { + file, err := os.Open(filespec) + if err != nil { + return nil, err + } + return NewSrcFile(file) +} + +// NewSrcFile creates a new SrcFile with the specified os.File. +func NewSrcFile(file *os.File) (*SrcFile, error) { + sf := &SrcFile{} + sf.freq = time.Minute + sf.file = file + if err := sf.ini.LoadFromFile(file); err != nil { + return nil, err + } + return sf, nil +} + +// GetProps fetches all the properties from a source and returns +// them as a map. +func (sf *SrcFile) GetProps() (map[string]string, error) { + lm, err := sf.GetLastModified() + if err != nil { + return nil, err + } + + // Check if we need to reload. + if sf.ini.GetLastModified() != lm { + if err := sf.ini.LoadFromFile(sf.file); err != nil { + return nil, err + } + } + return sf.ini.ToMap(), nil +} + +// GetLastModified returns the time of the latest modification to any +// property value within the source. +func (sf *SrcFile) GetLastModified() (time.Time, error) { + fi, err := sf.file.Stat() + if err != nil { + return time.Now(), err + } + return fi.ModTime(), nil +} |