Like Prometheus, but for logs.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
loki/pkg/cfg/files.go

72 lines
1.3 KiB

package cfg
import (
"encoding/json"
"flag"
"io/ioutil"
"github.com/pkg/errors"
"gopkg.in/yaml.v2"
)
// JSON returns a Source that opens the supplied `.json` file and loads it.
func JSON(f *string) Source {
return func(dst interface{}) error {
if f == nil {
return nil
}
j, err := ioutil.ReadFile(*f)
if err != nil {
return err
}
err = dJSON(j)(dst)
return errors.Wrap(err, *f)
}
}
// dJSON returns a JSON source and allows dependency injection
func dJSON(y []byte) Source {
return func(dst interface{}) error {
return json.Unmarshal(y, dst)
}
}
// YAML returns a Source that opens the supplied `.yaml` file and loads it.
func YAML(f *string) Source {
return func(dst interface{}) error {
if f == nil {
return nil
}
y, err := ioutil.ReadFile(*f)
if err != nil {
return err
}
err = dYAML(y)(dst)
return errors.Wrap(err, *f)
}
}
// dYAML returns a YAML source and allows dependency injection
func dYAML(y []byte) Source {
return func(dst interface{}) error {
return yaml.Unmarshal(y, dst)
}
}
// YAMLFlag defines a `config.file` flag and loads this file
func YAMLFlag(name, value, help string) Source {
return func(dst interface{}) error {
f := flag.String(name, value, help)
flag.Parse()
if *f == "" {
f = nil
}
return YAML(f)(dst)
}
}