-
Notifications
You must be signed in to change notification settings - Fork 487
/
marshal.go
50 lines (43 loc) · 1.29 KB
/
marshal.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
package instance
import (
"bytes"
"io"
config_util "github.com/prometheus/common/config"
"gopkg.in/yaml.v2"
)
// UnmarshalConfig unmarshals an instance config from a reader based on a
// provided content type.
func UnmarshalConfig(r io.Reader) (*Config, error) {
var cfg Config
dec := yaml.NewDecoder(r)
dec.SetStrict(true)
err := dec.Decode(&cfg)
return &cfg, err
}
// MarshalConfig marshals an instance config based on a provided content type.
func MarshalConfig(c *Config, scrubSecrets bool) ([]byte, error) {
var buf bytes.Buffer
err := MarshalConfigToWriter(c, &buf, scrubSecrets)
return buf.Bytes(), err
}
// MarshalConfigToWriter marshals a config to an io.Writer.
func MarshalConfigToWriter(c *Config, w io.Writer, scrubSecrets bool) error {
enc := yaml.NewEncoder(w)
// If we're not sanitizing the marshaled config, we want to add in an
// encoding hook to ignore how Secrets marshal (i.e., scrubbing the value
// and replacing it with <secret>).
if !scrubSecrets {
enc.SetHook(func(in interface{}) (ok bool, out interface{}, err error) {
switch v := in.(type) {
case config_util.Secret:
return true, string(v), nil
case *config_util.URL:
return true, v.String(), nil
default:
return false, nil, nil
}
})
}
type plain Config
return enc.Encode((*plain)(c))
}