From 8a66310cf9733de2650f1661bd5fe280d9a3d46a Mon Sep 17 00:00:00 2001 From: Ales Date: Tue, 9 Jul 2024 19:21:19 +0200 Subject: [PATCH] add support to expand env variables --- README.md | 2 +- config.go | 4 +++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 328ad87..70dd39e 100644 --- a/README.md +++ b/README.md @@ -28,7 +28,7 @@ import ( ) func main() { - configPaths := []string{"./config", "/etc/myapp"} + configPaths := []string{"./config", "/etc/myapp", "$HOME/.myapp","."} cfg, err := config.NewConfig("config.json", configPaths) if err != nil { log.Fatalf("Error loading config: %v", err) diff --git a/config.go b/config.go index a056300..6aa0563 100644 --- a/config.go +++ b/config.go @@ -16,7 +16,9 @@ type Config struct { func NewConfig(configName string, configPaths []string) (*Config, error) { var configData map[string]interface{} for _, path := range configPaths { - fullPath := filepath.Join(path, configName) + // Expand environment variables in the path + expandedPath := os.ExpandEnv(path) + fullPath := filepath.Join(expandedPath, configName) if _, err := os.Stat(fullPath); err == nil { file, err := os.ReadFile(fullPath) if err != nil {