fix: be compliant with golanci linter
All checks were successful
Lint Markdown files / Run markdown linter (push) Successful in 13s
Lint Golang files / Run golang CI linter (stable, ubuntu-latest-amd64) (push) Successful in 30s
Lint Golang files / Run golang CI linter (stable, ubuntu-latest-arm64) (push) Successful in 1m38s

This commit is contained in:
2025-08-12 16:28:40 +02:00
parent d2cf678b97
commit 2f17abedb9
5 changed files with 50 additions and 21 deletions

View File

@ -46,11 +46,12 @@ type YAMLConfig struct {
// Read reads the config struct from a file. The decoding format will be determined by the file extension like
// `xml` or `yaml`.
func ReadConfig(name string) (*domain.Config, error) {
// #nosec G304
f, err := os.Open(name)
if err != nil {
return nil, err
}
defer f.Close()
defer func() { _ = f.Close() }()
switch {
case strings.HasSuffix(name, "xml"):
@ -58,7 +59,7 @@ func ReadConfig(name string) (*domain.Config, error) {
case strings.HasSuffix(name, "yml") || strings.HasSuffix(name, "yaml"):
return readYAMLConfig(f)
default:
return nil, fmt.Errorf("Unsupported file extension")
return nil, fmt.Errorf("unsupported file extension")
}
}
@ -97,8 +98,8 @@ func readYAMLConfig(r io.Reader) (*domain.Config, error) {
}
func WatchConfig(ctx context.Context, name string) (<-chan *domain.Config, <-chan error) {
configChannel := make(chan *domain.Config, 0)
errorChannel := make(chan error, 0)
configChannel := make(chan *domain.Config)
errorChannel := make(chan error)
go func() {
wait := time.Second * 3
@ -110,14 +111,18 @@ func WatchConfig(ctx context.Context, name string) (<-chan *domain.Config, <-cha
errorChannel <- err
return
}
watcher.Add(name)
err = watcher.Add(name)
if err != nil {
errorChannel <- err
return
}
for {
select {
case <-ctx.Done():
close(configChannel)
close(errorChannel)
break
return
case event, open := <-watcher.Events:
if !open {
errorChannel <- fmt.Errorf("FSWatcher closed channel: %w", err)
@ -145,11 +150,12 @@ func WatchConfig(ctx context.Context, name string) (<-chan *domain.Config, <-cha
// WriteConfig writes the config struct into the file. The encoding format will be determined by the file extension like
// `xml` or `yaml`.
func WriteConfig(name string, config *domain.Config) error {
// #nosec G304
f, err := os.Create(name)
if err != nil {
return err
}
defer f.Close()
defer func() { _ = f.Close() }()
switch {
case strings.HasSuffix(name, "xml"):
@ -157,13 +163,13 @@ func WriteConfig(name string, config *domain.Config) error {
case strings.HasSuffix(name, "yml") || strings.HasSuffix(name, "yaml"):
return writeYAMLConfig(f, config)
default:
return fmt.Errorf("Unsupported file extension")
return fmt.Errorf("unsupported file extension")
}
}
func writeXMLConfig(w io.Writer, config *domain.Config) error {
xmlEncoder := xml.NewEncoder(w)
defer xmlEncoder.Close()
defer func() { _ = xmlEncoder.Close() }()
xmlConfig := &XMLConfig{
APIToken: config.API.Token,
@ -180,7 +186,7 @@ func writeXMLConfig(w io.Writer, config *domain.Config) error {
func writeYAMLConfig(w io.Writer, config *domain.Config) error {
yamlEncoder := yaml.NewEncoder(w)
defer yamlEncoder.Close()
defer func() { _ = yamlEncoder.Close() }()
yamlConfig := &YAMLConfig{
Auth: YAMLConfigAuth{

View File

@ -24,6 +24,7 @@ func TestReadWriteConfig_XML(t *testing.T) {
t.Cleanup(func() { _ = os.RemoveAll(tmpDir) })
expectedXMLConfigName := filepath.Join(tmpDir, "expected_config.xml")
// #nosec G304
f, err := os.Create(expectedXMLConfigName)
require.NoError(err)
@ -38,6 +39,7 @@ func TestReadWriteConfig_XML(t *testing.T) {
err = config.WriteConfig(actualXMLConfigName, actualConfig)
require.NoError(err)
// #nosec G304
b, err := os.ReadFile(actualXMLConfigName)
require.NoError(err)
require.Equal(expectedXMLConfig, string(b))
@ -51,6 +53,7 @@ func TestReadWriteConfig_YAML(t *testing.T) {
t.Cleanup(func() { _ = os.RemoveAll(tmpDir) })
expectedYAMLConfigName := filepath.Join(tmpDir, "expected_config.yaml")
// #nosec G304
f, err := os.Create(expectedYAMLConfigName)
require.NoError(err)
@ -65,6 +68,7 @@ func TestReadWriteConfig_YAML(t *testing.T) {
err = config.WriteConfig(actualYAMLConfigName, actualConfig)
require.NoError(err)
// #nosec G304
b, err := os.ReadFile(actualYAMLConfigName)
require.NoError(err)
require.Equal(expectedYAMLConfig, string(b))