Skip to content

Commit

Permalink
add better error logging and file / directory check (#232)
Browse files Browse the repository at this point in the history
  • Loading branch information
matoszz authored Nov 21, 2024
1 parent a484a5b commit f585d62
Showing 1 changed file with 21 additions and 9 deletions.
30 changes: 21 additions & 9 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package config

import (
"crypto/tls"
"os"

"strings"
"time"

Expand All @@ -10,17 +12,16 @@ import (
"github.com/knadh/koanf/providers/file"
"github.com/knadh/koanf/v2"
"github.com/mcuadros/go-defaults"
"github.com/rs/zerolog/log"

"github.com/theopenlane/beacon/otelx"
"github.com/theopenlane/emailtemplates"
"github.com/theopenlane/entx"
"github.com/theopenlane/iam/fgax"
"github.com/theopenlane/riverboat/pkg/riverqueue"

"github.com/theopenlane/iam/totp"

"github.com/theopenlane/iam/sessions"
"github.com/theopenlane/iam/tokens"

"github.com/theopenlane/iam/totp"
"github.com/theopenlane/riverboat/pkg/riverqueue"
"github.com/theopenlane/utils/cache"

"github.com/theopenlane/core/internal/ent/entconfig"
Expand All @@ -35,10 +36,6 @@ import (
"github.com/theopenlane/core/pkg/objects"
)

var (
DefaultConfigFilePath = "./config/.config.yaml"
)

// Config contains the configuration for the core server
type Config struct {
// RefreshInterval determines how often to reload the config
Expand Down Expand Up @@ -141,6 +138,10 @@ type PondPool struct {
MaxWorkers int `json:"maxWorkers" koanf:"maxWorkers" default:"100"`
}

var (
DefaultConfigFilePath = "./config/.config.yaml"
)

// Load is responsible for loading the configuration from a YAML file and environment variables.
// If the `cfgFile` is empty or nil, it sets the default configuration file path.
// Config settings are taken from default values, then from the config file, and finally from environment
Expand All @@ -152,17 +153,26 @@ func Load(cfgFile *string) (*Config, error) {
*cfgFile = DefaultConfigFilePath
}

if _, err := os.Stat(*cfgFile); err != nil {
if os.IsNotExist(err) {
log.Error().Err(err).Msg("config file not found")
return nil, err
}
}

// load defaults
conf := &Config{}
defaults.SetDefaults(conf)

// parse yaml config
if err := k.Load(file.Provider(*cfgFile), yaml.Parser()); err != nil {
log.Error().Err(err).Msg("failed to load config file - ensure the .config.yaml is present and valid")
panic(err)
}

// unmarshal the config
if err := k.Unmarshal("", &conf); err != nil {
log.Error().Err(err).Msg("failed to unmarshal config file")
panic(err)
}

Expand All @@ -176,11 +186,13 @@ func Load(cfgFile *string) (*Config, error) {

return key, v
}), nil); err != nil {
log.Error().Err(err).Msg("failed to load env vars")
panic(err)
}

// unmarshal the env vars
if err := k.Unmarshal("", &conf); err != nil {
log.Error().Err(err).Msg("failed to unmarshal env vars")
panic(err)
}

Expand Down

0 comments on commit f585d62

Please sign in to comment.