2023-02-02 10:55:24 +01:00
|
|
|
package config
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
|
|
|
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
"gopkg.in/yaml.v3"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Config definition
|
|
|
|
type Config struct {
|
2023-03-07 23:10:42 +01:00
|
|
|
Logger LoggerConfig `yaml:"logger"`
|
2023-10-13 12:30:52 +02:00
|
|
|
Sentry SentryConfig `yaml:"sentry"`
|
2023-03-07 23:10:42 +01:00
|
|
|
Server ServerConfig `yaml:"server"`
|
|
|
|
Agent AgentConfig `yaml:"agent"`
|
2023-02-02 10:55:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewFromFile retrieves the configuration from the given file
|
|
|
|
func NewFromFile(path string) (*Config, error) {
|
|
|
|
config := NewDefault()
|
|
|
|
|
|
|
|
data, err := ioutil.ReadFile(path)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrapf(err, "could not read file '%s'", path)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := yaml.Unmarshal(data, config); err != nil {
|
|
|
|
return nil, errors.Wrapf(err, "could not unmarshal configuration")
|
|
|
|
}
|
|
|
|
|
|
|
|
return config, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewDumpDefault dump the new default configuration
|
|
|
|
func NewDumpDefault() *Config {
|
|
|
|
config := NewDefault()
|
|
|
|
|
|
|
|
return config
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewDefault return new default configuration
|
|
|
|
func NewDefault() *Config {
|
|
|
|
return &Config{
|
2023-03-07 23:10:42 +01:00
|
|
|
Logger: NewDefaultLoggerConfig(),
|
|
|
|
Agent: NewDefaultAgentConfig(),
|
|
|
|
Server: NewDefaultServerConfig(),
|
2023-10-13 12:30:52 +02:00
|
|
|
Sentry: NewDefaultSentryConfig(),
|
2023-02-02 10:55:24 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Dump the given configuration in the given writer
|
|
|
|
func Dump(config *Config, w io.Writer) error {
|
|
|
|
data, err := yaml.Marshal(config)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "could not dump config")
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err := w.Write(data); err != nil {
|
|
|
|
return errors.WithStack(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|