Skip to content

Commit

Permalink
add support to expand env variables
Browse files Browse the repository at this point in the history
  • Loading branch information
aboxis committed Jul 9, 2024
1 parent 12eeabe commit 8a66310
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 2 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 3 additions & 1 deletion config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 8a66310

Please sign in to comment.