Skip to content

Commit

Permalink
refactor(config): loop over files and stat to check for existence
Browse files Browse the repository at this point in the history
  • Loading branch information
GeorgeMac committed Nov 21, 2024
1 parent d8638e7 commit 2b73e62
Showing 1 changed file with 12 additions and 23 deletions.
35 changes: 12 additions & 23 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,40 +92,29 @@ func processValue[T any](val reflect.Value, method func(T) error) error {
}

func ReadFromFS(filesystem fs.FS) (_ *Config, err error) {
dirfs, ok := filesystem.(fs.ReadDirFS)
statfs, ok := filesystem.(fs.StatFS)
if !ok {
return nil, errors.New("filesystem provided does not support opening directories")
}

entries, err := dirfs.ReadDir(".")
if err != nil {
return nil, err
}

var configPath string
files := []string{"glu.yaml", "glu.yml", "glu.json"}
for _, path := range files {
if _, err := statfs.Stat(path); err != nil {
if errors.Is(err, fs.ErrNotExist) {
continue
}

for _, ent := range entries {
if ent.IsDir() {
continue
return nil, err
}

switch ent.Name() {
case "glu.yaml", "glu.yml", "glu.json":
configPath = ent.Name()
slog.Debug("configuration found", "path", path)

slog.Debug("configuration found", "path", configPath)

break
}
return readFromPath(filesystem, path)
}

if configPath == "" {
slog.Debug("could not locate glu configuration file", "attempted", []string{"glu.yaml", "glu.yml", "glu.json"})

return readFrom(config.NewDecoder[Config](&emptyReader{}, yaml.Unmarshal))
}
slog.Debug("could not locate glu configuration file", "attempted", files)

return readFromPath(filesystem, configPath)
return readFrom(config.NewDecoder[Config](&emptyReader{}, yaml.Unmarshal))
}

func readFromPath(fs fs.FS, configPath string) (_ *Config, err error) {
Expand Down

0 comments on commit 2b73e62

Please sign in to comment.