mirror of
https://github.com/mariocandela/beelzebub.git
synced 2025-07-01 18:47:26 -04:00
Improve code quality
This commit is contained in:
24
main.go
24
main.go
@ -12,56 +12,56 @@ import (
|
|||||||
var quit = make(chan struct{})
|
var quit = make(chan struct{})
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
configurationsParser := parser.Init("./configurations/beelzebub.yaml", "./configurations/services/")
|
parser := parser.Init("./configurations/beelzebub.yaml", "./configurations/services/")
|
||||||
|
|
||||||
coreConfigurations, err := configurationsParser.ReadConfigurationsCore()
|
coreConfigurations, err := parser.ReadConfigurationsCore()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
fileLogs := configureLogging(coreConfigurations.Core.Logging)
|
fileLogs := configureLoggingByConfigurations(coreConfigurations.Core.Logging)
|
||||||
defer fileLogs.Close()
|
defer fileLogs.Close()
|
||||||
|
|
||||||
beelzebubServicesConfiguration, err := configurationsParser.ReadConfigurationsServices()
|
beelzebubServicesConfiguration, err := parser.ReadConfigurationsServices()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Protocol strategies
|
// Init Protocol strategies
|
||||||
secureShellStrategy := &protocols.SecureShellStrategy{}
|
secureShellStrategy := &protocols.SecureShellStrategy{}
|
||||||
hypertextTransferProtocolStrategy := &protocols.HypertextTransferProtocolStrategy{}
|
hypertextTransferProtocolStrategy := &protocols.HypertextTransferProtocolStrategy{}
|
||||||
|
|
||||||
// Protocol manager
|
// Init protocol manager, with simple log on stout trace strategy and default protocol HTTP
|
||||||
serviceManager := protocols.InitProtocolManager(logStrategy, hypertextTransferProtocolStrategy)
|
protocolManager := protocols.InitProtocolManager(traceStrategyStdout, hypertextTransferProtocolStrategy)
|
||||||
|
|
||||||
for _, beelzebubServiceConfiguration := range beelzebubServicesConfiguration {
|
for _, beelzebubServiceConfiguration := range beelzebubServicesConfiguration {
|
||||||
switch beelzebubServiceConfiguration.Protocol {
|
switch beelzebubServiceConfiguration.Protocol {
|
||||||
case "http":
|
case "http":
|
||||||
serviceManager.SetProtocolStrategy(hypertextTransferProtocolStrategy)
|
protocolManager.SetProtocolStrategy(hypertextTransferProtocolStrategy)
|
||||||
break
|
break
|
||||||
case "ssh":
|
case "ssh":
|
||||||
serviceManager.SetProtocolStrategy(secureShellStrategy)
|
protocolManager.SetProtocolStrategy(secureShellStrategy)
|
||||||
break
|
break
|
||||||
default:
|
default:
|
||||||
log.Fatalf("Protocol %s not managed", beelzebubServiceConfiguration.Protocol)
|
log.Fatalf("Protocol %s not managed", beelzebubServiceConfiguration.Protocol)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
err := serviceManager.InitService(beelzebubServiceConfiguration)
|
err := protocolManager.InitService(beelzebubServiceConfiguration)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Errorf("Error during init protocol: %s, %s", beelzebubServiceConfiguration.Protocol, err.Error())
|
log.Errorf("Error during init protocol: %s, %s", beelzebubServiceConfiguration.Protocol, err.Error())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
<-quit
|
<-quit
|
||||||
}
|
}
|
||||||
func logStrategy(event tracer.Event) {
|
func traceStrategyStdout(event tracer.Event) {
|
||||||
log.WithFields(log.Fields{
|
log.WithFields(log.Fields{
|
||||||
"status": event.Status.String(),
|
"status": event.Status.String(),
|
||||||
"event": event,
|
"event": event,
|
||||||
}).Info("New Event")
|
}).Info("New Event")
|
||||||
}
|
}
|
||||||
|
|
||||||
func configureLogging(configurations parser.Logging) *os.File {
|
func configureLoggingByConfigurations(configurations parser.Logging) *os.File {
|
||||||
file, err := os.OpenFile(configurations.LogsPath, os.O_APPEND|os.O_CREATE|os.O_RDWR, 0666)
|
file, err := os.OpenFile(configurations.LogsPath, os.O_APPEND|os.O_CREATE|os.O_RDWR, 0666)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalf("error opening file: %v", err)
|
log.Fatalf("error opening file: %v", err)
|
||||||
|
@ -43,26 +43,26 @@ type Command struct {
|
|||||||
type configurationsParser struct {
|
type configurationsParser struct {
|
||||||
configurationsCorePath string
|
configurationsCorePath string
|
||||||
configurationsServicesDirectory string
|
configurationsServicesDirectory string
|
||||||
readFileBytes ReadFileBytes
|
readFileBytesByFilePathDependency ReadFileBytesByFilePath
|
||||||
readDir ReadDir
|
gelAllFinesNameByDirNameDependency GelAllFinesNameByDirName
|
||||||
}
|
}
|
||||||
|
|
||||||
type ReadFileBytes func(filePath string) ([]byte, error)
|
type ReadFileBytesByFilePath func(filePath string) ([]byte, error)
|
||||||
|
|
||||||
type ReadDir func(dirName string) ([]string, error)
|
type GelAllFinesNameByDirName func(dirName string) ([]string, error)
|
||||||
|
|
||||||
// Init Parser, return a configurationsParser and use the DI Pattern to inject the dependencies
|
// Init Parser, return a configurationsParser and use the DI Pattern to inject the dependencies
|
||||||
func Init(configurationsCorePath, configurationsServicesDirectory string) *configurationsParser {
|
func Init(configurationsCorePath, configurationsServicesDirectory string) *configurationsParser {
|
||||||
return &configurationsParser{
|
return &configurationsParser{
|
||||||
configurationsCorePath: configurationsCorePath,
|
configurationsCorePath: configurationsCorePath,
|
||||||
configurationsServicesDirectory: configurationsServicesDirectory,
|
configurationsServicesDirectory: configurationsServicesDirectory,
|
||||||
readFileBytes: readFileBytes,
|
readFileBytesByFilePathDependency: readFileBytesByFilePath,
|
||||||
readDir: readDir,
|
gelAllFinesNameByDirNameDependency: gelAllFinesNameByDirName,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (bp configurationsParser) ReadConfigurationsCore() (*BeelzebubCoreConfigurations, error) {
|
func (bp configurationsParser) ReadConfigurationsCore() (*BeelzebubCoreConfigurations, error) {
|
||||||
buf, err := bp.readFileBytes(bp.configurationsCorePath)
|
buf, err := bp.readFileBytesByFilePathDependency(bp.configurationsCorePath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("in file %s: %v", bp.configurationsCorePath, err)
|
return nil, fmt.Errorf("in file %s: %v", bp.configurationsCorePath, err)
|
||||||
}
|
}
|
||||||
@ -77,7 +77,7 @@ func (bp configurationsParser) ReadConfigurationsCore() (*BeelzebubCoreConfigura
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (bp configurationsParser) ReadConfigurationsServices() ([]BeelzebubServiceConfiguration, error) {
|
func (bp configurationsParser) ReadConfigurationsServices() ([]BeelzebubServiceConfiguration, error) {
|
||||||
services, err := bp.readDir(bp.configurationsServicesDirectory)
|
services, err := bp.gelAllFinesNameByDirNameDependency(bp.configurationsServicesDirectory)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("in directory %s: %v", bp.configurationsServicesDirectory, err)
|
return nil, fmt.Errorf("in directory %s: %v", bp.configurationsServicesDirectory, err)
|
||||||
}
|
}
|
||||||
@ -85,7 +85,7 @@ func (bp configurationsParser) ReadConfigurationsServices() ([]BeelzebubServiceC
|
|||||||
var servicesConfiguration []BeelzebubServiceConfiguration
|
var servicesConfiguration []BeelzebubServiceConfiguration
|
||||||
for _, servicesName := range services {
|
for _, servicesName := range services {
|
||||||
filePath := filepath.Join(bp.configurationsServicesDirectory, servicesName)
|
filePath := filepath.Join(bp.configurationsServicesDirectory, servicesName)
|
||||||
buf, err := bp.readFileBytes(filePath)
|
buf, err := bp.readFileBytesByFilePathDependency(filePath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("in file %s: %v", filePath, err)
|
return nil, fmt.Errorf("in file %s: %v", filePath, err)
|
||||||
}
|
}
|
||||||
@ -101,7 +101,7 @@ func (bp configurationsParser) ReadConfigurationsServices() ([]BeelzebubServiceC
|
|||||||
return servicesConfiguration, nil
|
return servicesConfiguration, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func readDir(dirName string) ([]string, error) {
|
func gelAllFinesNameByDirName(dirName string) ([]string, error) {
|
||||||
var filesName []string
|
var filesName []string
|
||||||
files, err := ioutil.ReadDir(dirName)
|
files, err := ioutil.ReadDir(dirName)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -114,6 +114,6 @@ func readDir(dirName string) ([]string, error) {
|
|||||||
return filesName, nil
|
return filesName, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func readFileBytes(filePath string) ([]byte, error) {
|
func readFileBytesByFilePath(filePath string) ([]byte, error) {
|
||||||
return os.ReadFile(filePath)
|
return os.ReadFile(filePath)
|
||||||
}
|
}
|
||||||
|
@ -50,14 +50,14 @@ commands:
|
|||||||
func TestReadConfigurationsCoreError(t *testing.T) {
|
func TestReadConfigurationsCoreError(t *testing.T) {
|
||||||
configurationsParser := Init("mockConfigurationsCorePath", "mockConfigurationsServicesDirectory")
|
configurationsParser := Init("mockConfigurationsCorePath", "mockConfigurationsServicesDirectory")
|
||||||
|
|
||||||
configurationsParser.readFileBytes = mockReadfilebytesError
|
configurationsParser.readFileBytesByFilePathDependency = mockReadfilebytesError
|
||||||
beelzebubCoreConfigurations, err := configurationsParser.ReadConfigurationsCore()
|
beelzebubCoreConfigurations, err := configurationsParser.ReadConfigurationsCore()
|
||||||
|
|
||||||
assert.Nil(t, beelzebubCoreConfigurations)
|
assert.Nil(t, beelzebubCoreConfigurations)
|
||||||
assert.Error(t, err)
|
assert.Error(t, err)
|
||||||
assert.Equal(t, "in file mockConfigurationsCorePath: mockErrorReadFileBytes", err.Error())
|
assert.Equal(t, "in file mockConfigurationsCorePath: mockErrorReadFileBytes", err.Error())
|
||||||
|
|
||||||
configurationsParser.readFileBytes = mockReadfilebytesFormatError
|
configurationsParser.readFileBytesByFilePathDependency = mockReadfilebytesFormatError
|
||||||
|
|
||||||
beelzebubCoreConfigurations, err = configurationsParser.ReadConfigurationsCore()
|
beelzebubCoreConfigurations, err = configurationsParser.ReadConfigurationsCore()
|
||||||
assert.Nil(t, beelzebubCoreConfigurations)
|
assert.Nil(t, beelzebubCoreConfigurations)
|
||||||
@ -67,7 +67,7 @@ func TestReadConfigurationsCoreError(t *testing.T) {
|
|||||||
|
|
||||||
func TestReadConfigurationsCoreValid(t *testing.T) {
|
func TestReadConfigurationsCoreValid(t *testing.T) {
|
||||||
configurationsParser := Init("", "")
|
configurationsParser := Init("", "")
|
||||||
configurationsParser.readFileBytes = mockReadfilebytesConfigurationsCore
|
configurationsParser.readFileBytesByFilePathDependency = mockReadfilebytesConfigurationsCore
|
||||||
|
|
||||||
coreConfigurations, err := configurationsParser.ReadConfigurationsCore()
|
coreConfigurations, err := configurationsParser.ReadConfigurationsCore()
|
||||||
assert.Nil(t, err)
|
assert.Nil(t, err)
|
||||||
@ -82,8 +82,8 @@ func TestReadConfigurationsCoreValid(t *testing.T) {
|
|||||||
func TestReadConfigurationsServicesFail(t *testing.T) {
|
func TestReadConfigurationsServicesFail(t *testing.T) {
|
||||||
configurationsParser := Init("", "")
|
configurationsParser := Init("", "")
|
||||||
|
|
||||||
configurationsParser.readFileBytes = mockReadfilebytesError
|
configurationsParser.readFileBytesByFilePathDependency = mockReadfilebytesError
|
||||||
configurationsParser.readDir = mockReadDirError
|
configurationsParser.gelAllFinesNameByDirNameDependency = mockReadDirError
|
||||||
|
|
||||||
beelzebubServiceConfiguration, err := configurationsParser.ReadConfigurationsServices()
|
beelzebubServiceConfiguration, err := configurationsParser.ReadConfigurationsServices()
|
||||||
assert.Nil(t, beelzebubServiceConfiguration)
|
assert.Nil(t, beelzebubServiceConfiguration)
|
||||||
@ -93,8 +93,8 @@ func TestReadConfigurationsServicesFail(t *testing.T) {
|
|||||||
func TestReadConfigurationsServicesValid(t *testing.T) {
|
func TestReadConfigurationsServicesValid(t *testing.T) {
|
||||||
configurationsParser := Init("", "")
|
configurationsParser := Init("", "")
|
||||||
|
|
||||||
configurationsParser.readFileBytes = mockReadfilebytesBeelzebubServiceConfiguration
|
configurationsParser.readFileBytesByFilePathDependency = mockReadfilebytesBeelzebubServiceConfiguration
|
||||||
configurationsParser.readDir = mockReadDirValid
|
configurationsParser.gelAllFinesNameByDirNameDependency = mockReadDirValid
|
||||||
|
|
||||||
beelzebubServicesConfiguration, err := configurationsParser.ReadConfigurationsServices()
|
beelzebubServicesConfiguration, err := configurationsParser.ReadConfigurationsServices()
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user