fix: ignore empty config file in config validation (#561)
This commit is contained in:
@@ -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)
|
||||
|
||||
@@ -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())
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user