walking through configurations directory to scan all files

This commit is contained in:
Baipyrus 2024-09-24 14:44:25 +02:00
parent fec0d007a0
commit 4c11cb4fb2

View File

@ -2,27 +2,45 @@ package util
import (
"encoding/json"
"io/fs"
"path/filepath"
"io"
"os"
)
func ReadConfigs(name string) ([]*Config, error) {
file, err := os.Open(name)
func ReadConfigs(cfgPath string) ([]*Config, error) {
var configs []*Config
err := filepath.Walk(cfgPath, func(path string, info fs.FileInfo, err error) error {
if info.IsDir() {
return nil
}
file, err := os.Open(path)
if err != nil {
return nil, err
return nil
}
defer file.Close()
bytes, _ := io.ReadAll(file)
bytes, err := io.ReadAll(file)
if err != nil {
return nil
}
var config []*Config
var config *Config
err = json.Unmarshal(bytes, &config)
if err != nil {
return nil, err
return nil
}
return config, nil
configs = append(configs, config)
return nil
})
return configs, err
}
func SaveConfig(name string, config Config) error {