Add -config flag using cobra to specify config file path
This commit is contained in:
@@ -3,6 +3,7 @@ package config
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log/slog"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
@@ -48,29 +49,9 @@ func New(_ fx.Lifecycle, params Params) (*Config, error) {
|
||||
log := params.Logger.Get()
|
||||
name := params.Globals.Appname
|
||||
|
||||
var sc *smartconfig.Config
|
||||
var err error
|
||||
|
||||
// Try loading config from standard locations
|
||||
configPaths := []string{
|
||||
fmt.Sprintf("/etc/%s/config.yml", name),
|
||||
fmt.Sprintf("/etc/%s/config.yaml", name),
|
||||
filepath.Join(os.Getenv("HOME"), ".config", name, "config.yml"),
|
||||
filepath.Join(os.Getenv("HOME"), ".config", name, "config.yaml"),
|
||||
"config.yml",
|
||||
"config.yaml",
|
||||
}
|
||||
|
||||
for _, path := range configPaths {
|
||||
if _, statErr := os.Stat(path); statErr == nil {
|
||||
sc, err = smartconfig.NewFromConfigPath(path)
|
||||
if err == nil {
|
||||
log.Info("loaded config file", "path", path)
|
||||
|
||||
break
|
||||
}
|
||||
log.Warn("failed to parse config file", "path", path, "error", err)
|
||||
}
|
||||
sc, err := loadConfigFile(log, name)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if sc == nil {
|
||||
@@ -103,6 +84,48 @@ func New(_ fx.Lifecycle, params Params) (*Config, error) {
|
||||
return c, nil
|
||||
}
|
||||
|
||||
// loadConfigFile loads configuration from PIXA_CONFIG_PATH env var or standard locations.
|
||||
func loadConfigFile(log *slog.Logger, appName string) (*smartconfig.Config, error) {
|
||||
// Check for explicit config path from environment
|
||||
if envPath := os.Getenv("PIXA_CONFIG_PATH"); envPath != "" {
|
||||
sc, err := smartconfig.NewFromConfigPath(envPath)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to load config from %s: %w", envPath, err)
|
||||
}
|
||||
|
||||
log.Info("loaded config file", "path", envPath)
|
||||
|
||||
return sc, nil
|
||||
}
|
||||
|
||||
// Try loading config from standard locations
|
||||
configPaths := []string{
|
||||
fmt.Sprintf("/etc/%s/config.yml", appName),
|
||||
fmt.Sprintf("/etc/%s/config.yaml", appName),
|
||||
filepath.Join(os.Getenv("HOME"), ".config", appName, "config.yml"),
|
||||
filepath.Join(os.Getenv("HOME"), ".config", appName, "config.yaml"),
|
||||
"config.yml",
|
||||
"config.yaml",
|
||||
}
|
||||
|
||||
for _, path := range configPaths {
|
||||
if _, statErr := os.Stat(path); statErr == nil {
|
||||
sc, err := smartconfig.NewFromConfigPath(path)
|
||||
if err != nil {
|
||||
log.Warn("failed to parse config file", "path", path, "error", err)
|
||||
|
||||
continue
|
||||
}
|
||||
|
||||
log.Info("loaded config file", "path", path)
|
||||
|
||||
return sc, nil
|
||||
}
|
||||
}
|
||||
|
||||
return nil, nil //nolint:nilnil // nil config is valid (use defaults)
|
||||
}
|
||||
|
||||
func getString(sc *smartconfig.Config, key, defaultVal string) string {
|
||||
if sc == nil {
|
||||
return defaultVal
|
||||
|
||||
Reference in New Issue
Block a user