-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathloader_toml.go
43 lines (36 loc) · 1.09 KB
/
loader_toml.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
// Copyright The ActForGood Authors.
// Use of this source code is governed by an MIT-style
// license that can be found in the LICENSE file or at
// https://github.com/actforgood/xconf/blob/main/LICENSE.
package xconf
import (
"io"
"os"
"github.com/pelletier/go-toml/v2"
)
// TOMLFileLoader loads TOML configuration from a file.
// The location of TOML content based file is given as parameter.
func TOMLFileLoader(filePath string) Loader {
return LoaderFunc(func() (map[string]any, error) {
f, err := os.Open(filePath)
if err != nil {
return nil, err
}
defer f.Close()
return TOMLReaderLoader(f).Load()
})
}
// TOMLReaderLoader loads TOML configuration from an [io.Reader].
func TOMLReaderLoader(reader io.Reader) Loader {
return LoaderFunc(func() (map[string]any, error) {
if seekReader, ok := reader.(io.Seeker); ok {
_, _ = seekReader.Seek(0, io.SeekStart) // move to the beginning in case of a re-load needed.
}
var configMap map[string]any
dec := toml.NewDecoder(reader)
if err := dec.Decode(&configMap); err != nil {
return nil, err
}
return configMap, nil
})
}