diff --git a/config.go b/config.go index b32890d..392e20d 100644 --- a/config.go +++ b/config.go @@ -2,6 +2,7 @@ package main import ( "fmt" + "io" "os" "time" @@ -42,20 +43,29 @@ type bouncerConfig struct { PrometheusConfig PrometheusConfig `yaml:"prometheus"` } -func NewConfig(configPath string) (*bouncerConfig, error) { +// mergedConfig() returns the byte content of the patched configuration file (with .yaml.local). +func mergedConfig(configPath string) ([]byte, error) { + patcher := yamlpatch.NewPatcher(configPath, ".local") + data, err := patcher.MergedPatchContent() + if err != nil { + return nil, err + } + return data, nil +} + +func newConfig(reader io.Reader) (*bouncerConfig, error) { var LogOutput *lumberjack.Logger //io.Writer config := &bouncerConfig{} - patcher := yamlpatch.NewPatcher(configPath, ".local") - fcontent, err := patcher.MergedPatchContent() + fcontent, err := io.ReadAll(reader) if err != nil { return &bouncerConfig{}, err } err = yaml.Unmarshal(fcontent, &config) if err != nil { - return &bouncerConfig{}, fmt.Errorf("failed to unmarshal %s : %v", configPath, err) + return &bouncerConfig{}, fmt.Errorf("failed to unmarshal: %w", err) } if config.BinPath == "" { diff --git a/custom_test.go b/custom_test.go index ecb5fd8..cb86891 100644 --- a/custom_test.go +++ b/custom_test.go @@ -2,7 +2,6 @@ package main import ( "fmt" - "io/ioutil" "os" "reflect" "strings" @@ -33,7 +32,7 @@ type parsedLine struct { } func parseFile(path string) []parsedLine { - dat, err := ioutil.ReadFile(binaryOutputFile) + dat, err := os.ReadFile(binaryOutputFile) parsedLines := make([]parsedLine, 0) if err != nil { panic(err) diff --git a/go.sum b/go.sum index b877c34..728c7d0 100644 --- a/go.sum +++ b/go.sum @@ -180,7 +180,6 @@ github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA github.com/census-instrumentation/opencensus-proto v0.3.0/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU= github.com/certifi/gocertifi v0.0.0-20191021191039-0944d244cd40/go.mod h1:sGbDF6GwGcLpkNXPUTkMRoywsNa/ol15pxFe6ERfguA= github.com/certifi/gocertifi v0.0.0-20200922220541-2c3bb06c6054/go.mod h1:sGbDF6GwGcLpkNXPUTkMRoywsNa/ol15pxFe6ERfguA= -github.com/cespare/xxhash v1.1.0 h1:a6HrQnmkObjyL+Gs60czilIUGqrzKutQD6XZog3p+ko= github.com/cespare/xxhash v1.1.0/go.mod h1:XrSqR1VqqWfGrhpAt58auRo0WTKS1nRRg3ghfAqPWnc= github.com/cespare/xxhash/v2 v2.1.1/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs= github.com/cespare/xxhash/v2 v2.1.2 h1:YRXhKfTDauu4ajMg1TPgFO5jnlC2HCbmLXMcTG5cbYE= diff --git a/main.go b/main.go index 770c7e5..2a37a0d 100644 --- a/main.go +++ b/main.go @@ -1,6 +1,7 @@ package main import ( + "bytes" "context" "flag" "fmt" @@ -87,7 +88,12 @@ func main() { }, }) - config, err := NewConfig(*configPath) + configBytes, err := mergedConfig(*configPath) + if err != nil { + log.Fatalf("unable to read config file: %s", err) + } + + config, err := newConfig(bytes.NewReader(configBytes)) if err != nil { log.Fatalf("unable to load configuration: %s", err) }