first commit
This commit is contained in:
commit
5203032b83
25 changed files with 2825 additions and 0 deletions
90
bootstrap/load.go
Normal file
90
bootstrap/load.go
Normal file
|
|
@ -0,0 +1,90 @@
|
|||
package bootstrap
|
||||
|
||||
import (
|
||||
"bubble/types"
|
||||
"github.com/sirupsen/logrus"
|
||||
"gopkg.in/yaml.v3"
|
||||
"io/fs"
|
||||
"os"
|
||||
"path/filepath"
|
||||
)
|
||||
|
||||
const ConfDir = "./confs/"
|
||||
|
||||
func LoadApplications() []types.PackageApplication {
|
||||
var apps []types.PackageApplication
|
||||
|
||||
err := filepath.WalkDir(ConfDir, 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]
|
||||
v.Value = v.Default // сразу устанавливаем дефолт
|
||||
varMap[v.Name] = v
|
||||
}
|
||||
|
||||
for i := range app.Sections {
|
||||
section := &app.Sections[i]
|
||||
for j := range section.Fields {
|
||||
field := §ion.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
|
||||
}
|
||||
27
bootstrap/logs.go
Normal file
27
bootstrap/logs.go
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
package bootstrap
|
||||
|
||||
import (
|
||||
"github.com/sirupsen/logrus"
|
||||
"gopkg.in/natefinch/lumberjack.v2"
|
||||
"io"
|
||||
)
|
||||
|
||||
func Logs() {
|
||||
TextFormatter := &logrus.TextFormatter{
|
||||
FullTimestamp: true,
|
||||
TimestampFormat: "15:04:05 02-01-2006",
|
||||
DisableQuote: true,
|
||||
PadLevelText: true,
|
||||
}
|
||||
logWriter := &lumberjack.Logger{
|
||||
Filename: "log.txt",
|
||||
Compress: true,
|
||||
MaxSize: 50,
|
||||
LocalTime: true,
|
||||
}
|
||||
output := io.MultiWriter(logWriter)
|
||||
|
||||
logrus.SetFormatter(TextFormatter)
|
||||
logrus.SetOutput(output)
|
||||
logrus.SetLevel(logrus.InfoLevel)
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue