/
legacy.go
69 lines (57 loc) · 1.65 KB
/
legacy.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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package config
import (
"io"
"io/ioutil"
"log"
"os"
"strings"
"github.com/influxdata/toml"
"github.com/influxdata/toml/ast"
)
// Set a logger for any configuration related information.
func SetLogger(l *log.Logger) {
toml.SetLogger(l)
}
// Decode unmarshals a string of TOML into a target configuration struct.
func Decode(tomlBlob string, target interface{}) error {
sreader := strings.NewReader(tomlBlob)
decoder := toml.NewDecoder(sreader)
err := decoder.Decode(target)
if err != nil {
return err
}
return nil
}
// DecodeFile loads a TOML configuration from a provided path and unmarshals it
// into a target configuration struct.
func DecodeFile(path string, target interface{}) error {
file, err := os.Open(path)
if err != nil {
return err
}
decoder := toml.NewDecoder(file)
err = decoder.Decode(target)
if err != nil {
return err
}
return nil
}
// ParseFile loads a TOML configuration from a provided path and returns the
// AST produced from the TOML parser. This function was originally provided by naoina/toml
func ParseFile(fpath string) (*ast.Table, error) {
contents, err := ioutil.ReadFile(fpath)
if err != nil {
return nil, err
}
return toml.Parse(contents)
}
// NewEncoder produces a struct capable of writing TOML-encoded data to the
// provided io.Writer. This function was originally provided by BurntSushi/toml
func NewEncoder(w io.Writer) *toml.Encoder {
return toml.NewEncoder(w)
}
// UnmarshalTable provides a mechanism to incrementally unmarshal an AST
// produced by ParseFile. This method was originally provided by naoina/toml
func UnmarshalTable(t *ast.Table, v interface{}) error {
return toml.UnmarshalTable(t, v)
}