GoSetup-bubble/bootstrap/load.go

95 lines
2.1 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package bootstrap
import (
"bubble/types"
"github.com/sirupsen/logrus"
"gopkg.in/yaml.v3"
"io/fs"
"os"
"path/filepath"
"strings"
)
func LoadApplications() []types.PackageApplication {
var apps []types.PackageApplication
err := filepath.WalkDir(types.ConfsDir, func(path string, d fs.DirEntry, err error) error {
if err != nil || d == nil || d.IsDir() {
return err
}
if filepath.Ext(d.Name()) != ".yaml" && filepath.Ext(d.Name()) != ".yml" {
return nil
}
app, err := loadFromYAML(path)
if err != nil {
logrus.Errorf("Ошибка загрузки %s: %v", path, err)
return nil
}
apps = append(apps, app)
return nil
})
if err != nil {
logrus.Fatal(err)
}
return apps
}
func loadFromYAML(filePath string) (types.PackageApplication, error) {
var app types.PackageApplication
data, err := os.ReadFile(filePath)
if err != nil {
return app, err
}
if err = yaml.Unmarshal(data, &app); err != nil {
return app, err
}
app = connectVariables(app)
return app, nil
}
// connectVariables — связывает поля с переменными по указателям
// и инициализирует Value = Default
func connectVariables(app types.PackageApplication) types.PackageApplication {
varMap := make(map[string]*types.Variable)
for i := range app.Variables {
v := &app.Variables[i]
// сразу устанавливаем дефолт
switch v.Type {
case "bool":
v.Value = strings.ToLower(v.Default) == "true"
default:
v.Value = v.Default
}
varMap[v.Name] = v
}
for i := range app.Sections {
section := &app.Sections[i]
for j := range section.Fields {
field := &section.Fields[j]
if v, exists := varMap[field.VariableName]; exists {
field.Variable = v
field.Active = true
} else {
logrus.Warnf("Переменная не найдена: %s (в поле %s)", field.VariableName, field.Name)
// Создаём заглушку
field.Variable = &types.Variable{
Name: field.VariableName,
Type: "string",
Default: "",
Value: "",
}
field.Active = true
}
}
}
return app
}