github.com/yankunsam/loki/v2@v2.6.3-0.20220817130409-389df5235c27/pkg/ruler/storage/instance/marshal.go (about)

     1  // This directory was copied and adapted from https://github.com/grafana/agent/tree/main/pkg/metrics.
     2  // We cannot vendor the agent in since the agent vendors loki in, which would cause a cyclic dependency.
     3  // NOTE: many changes have been made to the original code for our use-case.
     4  package instance
     5  
     6  import (
     7  	"bytes"
     8  	"io"
     9  
    10  	"gopkg.in/yaml.v2"
    11  )
    12  
    13  // UnmarshalConfig unmarshals an instance config from a reader based on a
    14  // provided content type.
    15  func UnmarshalConfig(r io.Reader) (*Config, error) {
    16  	var cfg Config
    17  	dec := yaml.NewDecoder(r)
    18  	dec.SetStrict(true)
    19  	err := dec.Decode(&cfg)
    20  	return &cfg, err
    21  }
    22  
    23  // MarshalConfig marshals an instance config based on a provided content type.
    24  func MarshalConfig(c *Config, scrubSecrets bool) ([]byte, error) {
    25  	var buf bytes.Buffer
    26  	err := MarshalConfigToWriter(c, &buf, scrubSecrets)
    27  	return buf.Bytes(), err
    28  }
    29  
    30  // MarshalConfigToWriter marshals a config to an io.Writer.
    31  func MarshalConfigToWriter(c *Config, w io.Writer, scrubSecrets bool) error {
    32  	enc := yaml.NewEncoder(w)
    33  
    34  	type plain Config
    35  	return enc.Encode((*plain)(c))
    36  }