fix: ignore empty config file in config validation (#561)

This commit is contained in:
Marc Brugger
2025-04-16 12:51:27 +02:00
committed by GitHub
parent 3bd093ceb9
commit bb1f2d02c5
3 changed files with 15 additions and 4 deletions
+7 -1
View File
@@ -2,6 +2,7 @@ package config
import (
_ "embed"
"fmt"
"os"
"strings"
@@ -17,19 +18,24 @@ var schemaData string
func validateSchema(cfgFile string) error {
// ignore if file not exists
if _, err := os.Stat(cfgFile); err != nil {
// Config file does not exist or is not readable - ignore it
//nolint:nilerr
return nil
}
// Load YAML file
yamlContent, err := os.ReadFile(cfgFile)
if err != nil {
return err
return fmt.Errorf("config file %q is invalid: %w", cfgFile, err)
}
return validateYAML(yamlContent)
}
func validateYAML(yamlContent []byte) error {
if yamlContent == nil || strings.TrimSpace(string(yamlContent)) == "" {
return nil
}
// Convert YAML to JSON
var yamlData any
err := yaml.Unmarshal(yamlContent, &yamlData)
+5
View File
@@ -36,5 +36,10 @@ var _ = Describe("Config", func() {
err = validateYAML(data)
Ω(err).ShouldNot(HaveOccurred())
})
It("validate config with empty file", func() {
var data []byte
err := validateYAML(data)
Ω(err).ShouldNot(HaveOccurred())
})
})
})
+3 -3
View File
@@ -496,7 +496,7 @@ func (mr *MockClientMockRecorder) SetSafeSearchConfig(settings any) *gomock.Call
}
// SetStatsConfig mocks base method.
func (m *MockClient) SetStatsConfig(sc *model.GetStatsConfigResponse) error {
func (m *MockClient) SetStatsConfig(sc *model.PutStatsConfigUpdateRequest) error {
m.ctrl.T.Helper()
ret := m.ctrl.Call(m, "SetStatsConfig", sc)
ret0, _ := ret[0].(error)
@@ -539,10 +539,10 @@ func (mr *MockClientMockRecorder) Stats() *gomock.Call {
}
// StatsConfig mocks base method.
func (m *MockClient) StatsConfig() (*model.GetStatsConfigResponse, error) {
func (m *MockClient) StatsConfig() (*model.PutStatsConfigUpdateRequest, error) {
m.ctrl.T.Helper()
ret := m.ctrl.Call(m, "StatsConfig")
ret0, _ := ret[0].(*model.GetStatsConfigResponse)
ret0, _ := ret[0].(*model.PutStatsConfigUpdateRequest)
ret1, _ := ret[1].(error)
return ret0, ret1
}