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
if err != nil {
return nil, err
}
defer file.Close()
err := filepath.Walk(cfgPath, func(path string, info fs.FileInfo, err error) error {
if info.IsDir() {
return nil
}
bytes, _ := io.ReadAll(file)
file, err := os.Open(path)
var config []*Config
err = json.Unmarshal(bytes, &config)
if err != nil {
return nil
}
defer file.Close()
if err != nil {
return nil, err
}
return config, nil
bytes, err := io.ReadAll(file)
if err != nil {
return nil
}
var config *Config
err = json.Unmarshal(bytes, &config)
if err != nil {
return nil
}
configs = append(configs, config)
return nil
})
return configs, err
}
func SaveConfig(name string, config Config) error {