Files
beelzebub/parser/configurationsParser.go

126 lines
4.0 KiB
Go
Raw Normal View History

2022-05-08 20:49:53 +02:00
package parser
import (
"fmt"
log "github.com/sirupsen/logrus"
"gopkg.in/yaml.v3"
"io/ioutil"
"os"
"path/filepath"
)
type BeelzebubCoreConfigurations struct {
Core struct {
Logging Logging `yaml:"logging"`
Tracing Tracing `yaml:"tracing"`
2022-05-08 20:49:53 +02:00
}
}
type Logging struct {
Debug bool `yaml:"debug"`
DebugReportCaller bool `yaml:"debugReportCaller"`
LogDisableTimestamp bool `yaml:"logDisableTimestamp"`
LogsPath string `yaml:"logsPath,omitempty"`
}
type Tracing struct {
MongoEnabled bool `yaml:"mongoEnabled,omitempty"`
MongoURI string `yaml:"mongoURI,omitempty"`
}
2022-05-08 20:49:53 +02:00
type BeelzebubServiceConfiguration struct {
ApiVersion string `yaml:"apiVersion"`
Protocol string `yaml:"protocol"`
Address string `yaml:"address"`
Commands []Command `yaml:"commands"`
ServerVersion string `yaml:"serverVersion"`
ServerName string `yaml:"serverName"`
DeadlineTimeoutSeconds int `yaml:"deadlineTimeoutSeconds"`
PasswordRegex string `yaml:"passwordRegex"`
}
type Command struct {
Regex string `yaml:"regex"`
Handler string `yaml:"handler"`
Headers []string `yaml:"headers"`
StatusCode int `yaml:"statusCode"`
}
type configurationsParser struct {
2022-05-12 22:46:15 +02:00
configurationsCorePath string
configurationsServicesDirectory string
readFileBytesByFilePathDependency ReadFileBytesByFilePath
2022-05-14 16:51:00 +02:00
gelAllFilesNameByDirNameDependency GelAllFilesNameByDirName
2022-05-08 20:49:53 +02:00
}
2022-05-12 22:46:15 +02:00
type ReadFileBytesByFilePath func(filePath string) ([]byte, error)
2022-05-08 20:49:53 +02:00
2022-05-14 16:51:00 +02:00
type GelAllFilesNameByDirName func(dirName string) ([]string, error)
2022-05-08 20:49:53 +02:00
// Init Parser, return a configurationsParser and use the DI Pattern to inject the dependencies
func Init(configurationsCorePath, configurationsServicesDirectory string) *configurationsParser {
return &configurationsParser{
2022-05-12 22:46:15 +02:00
configurationsCorePath: configurationsCorePath,
configurationsServicesDirectory: configurationsServicesDirectory,
readFileBytesByFilePathDependency: readFileBytesByFilePath,
2022-05-14 16:51:00 +02:00
gelAllFilesNameByDirNameDependency: gelAllFilesNameByDirName,
2022-05-08 20:49:53 +02:00
}
}
func (bp configurationsParser) ReadConfigurationsCore() (*BeelzebubCoreConfigurations, error) {
2022-05-12 22:46:15 +02:00
buf, err := bp.readFileBytesByFilePathDependency(bp.configurationsCorePath)
2022-05-08 20:49:53 +02:00
if err != nil {
return nil, fmt.Errorf("in file %s: %v", bp.configurationsCorePath, err)
}
beelzebubConfiguration := &BeelzebubCoreConfigurations{}
err = yaml.Unmarshal(buf, beelzebubConfiguration)
if err != nil {
return nil, fmt.Errorf("in file %s: %v", bp.configurationsCorePath, err)
}
return beelzebubConfiguration, nil
}
func (bp configurationsParser) ReadConfigurationsServices() ([]BeelzebubServiceConfiguration, error) {
2022-05-14 16:51:00 +02:00
services, err := bp.gelAllFilesNameByDirNameDependency(bp.configurationsServicesDirectory)
2022-05-08 20:49:53 +02:00
if err != nil {
return nil, fmt.Errorf("in directory %s: %v", bp.configurationsServicesDirectory, err)
}
var servicesConfiguration []BeelzebubServiceConfiguration
for _, servicesName := range services {
filePath := filepath.Join(bp.configurationsServicesDirectory, servicesName)
2022-05-12 22:46:15 +02:00
buf, err := bp.readFileBytesByFilePathDependency(filePath)
2022-05-08 20:49:53 +02:00
if err != nil {
return nil, fmt.Errorf("in file %s: %v", filePath, err)
}
beelzebubServiceConfiguration := &BeelzebubServiceConfiguration{}
err = yaml.Unmarshal(buf, beelzebubServiceConfiguration)
if err != nil {
return nil, fmt.Errorf("in file %s: %v", filePath, err)
}
log.Debug(beelzebubServiceConfiguration)
servicesConfiguration = append(servicesConfiguration, *beelzebubServiceConfiguration)
}
return servicesConfiguration, nil
}
2022-05-14 16:51:00 +02:00
func gelAllFilesNameByDirName(dirName string) ([]string, error) {
2022-05-08 20:49:53 +02:00
var filesName []string
files, err := ioutil.ReadDir(dirName)
if err != nil {
return nil, err
}
for _, file := range files {
filesName = append(filesName, file.Name())
}
return filesName, nil
}
2022-05-12 22:46:15 +02:00
func readFileBytesByFilePath(filePath string) ([]byte, error) {
2022-05-08 20:49:53 +02:00
return os.ReadFile(filePath)
}