Skip to content

Commit

Permalink
Use config#Load from our library
Browse files Browse the repository at this point in the history
  • Loading branch information
lippserd committed Jan 15, 2025
1 parent 4c38be1 commit 8f5e40e
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 44 deletions.
46 changes: 4 additions & 42 deletions internal/command/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
icingadbconfig "github.com/icinga/icingadb/internal/config"
"github.com/icinga/icingadb/pkg/icingaredis/telemetry"
"github.com/pkg/errors"
"io/fs"
"os"
"time"
)
Expand Down Expand Up @@ -38,48 +37,11 @@ func New() *Command {
os.Exit(0)
}

// This function supports configuration loading in three scenarios:
//
// 1. Load configuration solely from YAML files when no relevant environment variables are set.
//
// 2. Combine YAML file and environment variable configurations, allowing environment variables
// to supplement or override possible incomplete YAML configurations.
//
// 3. Load entirely from environment variables if the default YAML config file is absent and
// no specific config path is provided by the user.
var cfg icingadbconfig.Config
var configPath string
if flags.Config != "" {
configPath = flags.Config
} else {
configPath = icingadbconfig.DefaultConfigPath
}

if err := config.FromYAMLFile(configPath, &cfg); err != nil {
if errors.Is(err, config.ErrInvalidArgument) {
panic(err)
}

// Allow continuation with FromEnv by handling:
//
// - ErrInvalidConfiguration:
// The configuration may be incomplete and will be revalidated in FromEnv.
//
// - Non-existent file errors:
// If no explicit config path is set, fallback to environment variables is allowed.
configIsInvalid := errors.Is(err, config.ErrInvalidConfiguration)
defaultConfigFileDoesNotExist := errors.Is(err, fs.ErrNotExist) &&
configPath == icingadbconfig.DefaultConfigPath
if !(configIsInvalid || defaultConfigFileDoesNotExist) {
utils.PrintErrorThenExit(err, 1)
}
}

// Call FromEnv regardless of the outcome from FromYAMLFile.
// If no environment variables are set, configuration relies entirely on YAML.
// Otherwise, environment variables can supplement, override YAML settings, or serve as the sole source.
// FromEnv also includes validation, ensuring completeness after considering both sources.
if err := config.FromEnv(&cfg, config.EnvOptions{Prefix: "ICINGADB_"}); err != nil {
if err := config.Load(&cfg, config.LoadOptions{
Flags: flags,
EnvOptions: config.EnvOptions{Prefix: "ICINGADB_"},
}); err != nil {
if errors.Is(err, config.ErrInvalidArgument) {
panic(err)
}
Expand Down
21 changes: 19 additions & 2 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,25 @@ func (c *Config) Validate() error {
type Flags struct {
// Version decides whether to just print the version and exit.
Version bool `long:"version" description:"print version and exit"`
// Config is the path to the config file
Config string `short:"c" long:"config" description:"path to config file"`

// Config is the path to the config file. If not provided, it defaults to DefaultConfigPath.
Config string `short:"c" long:"config" description:"path to config file" default:"/etc/icingadb/config.yml"`
// default must be kept in sync with DefaultConfigPath.
}

// GetConfigPath retrieves the path to the configuration file.
// It returns the path specified via the command line, or DefaultConfigPath if none is provided.
func (f Flags) GetConfigPath() string {
if f.Config == "" {
return DefaultConfigPath
}

return f.Config
}

// IsExplicitConfigPath indicates whether the configuration file path was explicitly set.
func (f Flags) IsExplicitConfigPath() bool {
return f.Config != ""
}

// RetentionConfig defines configuration for history retention.
Expand Down

0 comments on commit 8f5e40e

Please sign in to comment.