Skip to content

Commit

Permalink
yaml.v3: handle empty files
Browse files Browse the repository at this point in the history
  • Loading branch information
mmetc committed Mar 4, 2024
1 parent 881b9de commit cbee069
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 5 deletions.
8 changes: 6 additions & 2 deletions pkg/csconfig/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,9 @@ func (o *OnlineApiClientCfg) Load() error {

err = dec.Decode(o.Credentials)
if err != nil {
return fmt.Errorf("failed unmarshaling api server credentials configuration file '%s': %w", o.CredentialsFilePath, err)
if !errors.Is(err, io.EOF) {
return fmt.Errorf("failed unmarshaling api server credentials configuration file '%s': %w", o.CredentialsFilePath, err)
}

Check warning on line 103 in pkg/csconfig/api.go

View check run for this annotation

Codecov / codecov/patch

pkg/csconfig/api.go#L102-L103

Added lines #L102 - L103 were not covered by tests
}

switch {
Expand Down Expand Up @@ -129,7 +131,9 @@ func (l *LocalApiClientCfg) Load() error {

err = dec.Decode(&l.Credentials)
if err != nil {
return fmt.Errorf("failed unmarshaling api client credential configuration file '%s': %w", l.CredentialsFilePath, err)
if !errors.Is(err, io.EOF) {
return fmt.Errorf("failed unmarshaling api client credential configuration file '%s': %w", l.CredentialsFilePath, err)
}

Check warning on line 136 in pkg/csconfig/api.go

View check run for this annotation

Codecov / codecov/patch

pkg/csconfig/api.go#L135-L136

Added lines #L135 - L136 were not covered by tests
}

if l.Credentials == nil || l.Credentials.URL == "" {
Expand Down
8 changes: 6 additions & 2 deletions pkg/csconfig/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
package csconfig

import (
"errors"
"fmt"
"io"
"os"
"path/filepath"
"strings"
Expand Down Expand Up @@ -63,8 +65,10 @@ func NewConfig(configFile string, disableAgent bool, disableAPI bool, inCli bool

err = dec.Decode(&cfg)
if err != nil {
// this is actually the "merged" yaml
return nil, "", fmt.Errorf("%s: %w", configFile, err)
if !errors.Is(err, io.EOF) {
// this is actually the "merged" yaml
return nil, "", fmt.Errorf("%s: %w", configFile, err)
}
}

if cfg.Prometheus == nil {
Expand Down
6 changes: 5 additions & 1 deletion pkg/csconfig/simulation.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ package csconfig

import (
"bytes"
"errors"
"fmt"
"io"
"path/filepath"

"gopkg.in/yaml.v3"
Expand Down Expand Up @@ -44,7 +46,9 @@ func (c *Config) LoadSimulation() error {
dec := yaml.NewDecoder(bytes.NewReader(rcfg))
dec.KnownFields(true)
if err := dec.Decode(&simCfg); err != nil {
return fmt.Errorf("while unmarshaling simulation file '%s' : %s", c.ConfigPaths.SimulationFilePath, err)
if !errors.Is(err, io.EOF) {
return fmt.Errorf("while unmarshaling simulation file '%s' : %s", c.ConfigPaths.SimulationFilePath, err)
}

Check warning on line 51 in pkg/csconfig/simulation.go

View check run for this annotation

Codecov / codecov/patch

pkg/csconfig/simulation.go#L49-L51

Added lines #L49 - L51 were not covered by tests
}
if simCfg.Simulation == nil {
simCfg.Simulation = new(bool)
Expand Down

0 comments on commit cbee069

Please sign in to comment.