Skip to content
This repository has been archived by the owner on Feb 15, 2024. It is now read-only.

Commit

Permalink
errcheck: Explicitly check file close return vals
Browse files Browse the repository at this point in the history
- Wrap deferred file close with error check/logging
- Add explicit file close with error check/logging
- Emit func name for both cases to aid in troubleshooting

refs GH-104
  • Loading branch information
atc0005 committed Jul 22, 2020
1 parent ed2cee6 commit f2aafe9
Showing 1 changed file with 22 additions and 1 deletion.
23 changes: 22 additions & 1 deletion config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (

"github.com/alexflint/go-arg"
"github.com/apex/log"
"github.com/atc0005/brick/internal/caller"
"github.com/pelletier/go-toml"
)

Expand Down Expand Up @@ -180,6 +181,8 @@ func MessageTrailer(format BrandingFormat) string {
// one or more configuration sources.
func NewConfig() (*Config, error) {

myFuncName := caller.GetFuncName()

config := Config{}

// Bundle the returned `*.arg.Parser` for later use One potential use:
Expand All @@ -204,7 +207,15 @@ func NewConfig() (*Config, error) {
if err != nil {
return nil, fmt.Errorf("unable to open config file: %v", err)
}
defer fh.Close()
defer func() {
if err := fh.Close(); err != nil {
log.Errorf(
"%s: failed to close file %q: %s",
myFuncName,
err.Error(),
)
}
}()
log.Debugf("Config file %q opened", config.ConfigFile())

log.Debugf("Attempting to load config file %q", config.ConfigFile())
Expand All @@ -213,6 +224,16 @@ func NewConfig() (*Config, error) {
"error loading config file %q: %v", config.ConfigFile(), err)
}
log.Debugf("Config file %q successfully loaded", config.ConfigFile())

// explicitly close file, bail if failure occurs
if err := fh.Close(); err != nil {
return nil, fmt.Errorf(
"%s: failed to close file %q: %w",
myFuncName,
config.ConfigFile(),
err,
)
}
}

// Apply initial logging settings based on user-supplied settings
Expand Down

0 comments on commit f2aafe9

Please sign in to comment.