Improve code quality

This commit is contained in:
Mario
2022-05-12 22:46:15 +02:00
parent 187a849475
commit 3d020d7548
3 changed files with 34 additions and 34 deletions

24
main.go
View File

@ -12,56 +12,56 @@ import (
var quit = make(chan struct{})
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 {
log.Fatal(err)
}
fileLogs := configureLogging(coreConfigurations.Core.Logging)
fileLogs := configureLoggingByConfigurations(coreConfigurations.Core.Logging)
defer fileLogs.Close()
beelzebubServicesConfiguration, err := configurationsParser.ReadConfigurationsServices()
beelzebubServicesConfiguration, err := parser.ReadConfigurationsServices()
if err != nil {
log.Fatal(err)
}
// Protocol strategies
// Init Protocol strategies
secureShellStrategy := &protocols.SecureShellStrategy{}
hypertextTransferProtocolStrategy := &protocols.HypertextTransferProtocolStrategy{}
// Protocol manager
serviceManager := protocols.InitProtocolManager(logStrategy, hypertextTransferProtocolStrategy)
// Init protocol manager, with simple log on stout trace strategy and default protocol HTTP
protocolManager := protocols.InitProtocolManager(traceStrategyStdout, hypertextTransferProtocolStrategy)
for _, beelzebubServiceConfiguration := range beelzebubServicesConfiguration {
switch beelzebubServiceConfiguration.Protocol {
case "http":
serviceManager.SetProtocolStrategy(hypertextTransferProtocolStrategy)
protocolManager.SetProtocolStrategy(hypertextTransferProtocolStrategy)
break
case "ssh":
serviceManager.SetProtocolStrategy(secureShellStrategy)
protocolManager.SetProtocolStrategy(secureShellStrategy)
break
default:
log.Fatalf("Protocol %s not managed", beelzebubServiceConfiguration.Protocol)
continue
}
err := serviceManager.InitService(beelzebubServiceConfiguration)
err := protocolManager.InitService(beelzebubServiceConfiguration)
if err != nil {
log.Errorf("Error during init protocol: %s, %s", beelzebubServiceConfiguration.Protocol, err.Error())
}
}
<-quit
}
func logStrategy(event tracer.Event) {
func traceStrategyStdout(event tracer.Event) {
log.WithFields(log.Fields{
"status": event.Status.String(),
"event": 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)
if err != nil {
log.Fatalf("error opening file: %v", err)

View File

@ -41,28 +41,28 @@ type Command struct {
}
type configurationsParser struct {
configurationsCorePath string
configurationsServicesDirectory string
readFileBytes ReadFileBytes
readDir ReadDir
configurationsCorePath string
configurationsServicesDirectory string
readFileBytesByFilePathDependency ReadFileBytesByFilePath
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
func Init(configurationsCorePath, configurationsServicesDirectory string) *configurationsParser {
return &configurationsParser{
configurationsCorePath: configurationsCorePath,
configurationsServicesDirectory: configurationsServicesDirectory,
readFileBytes: readFileBytes,
readDir: readDir,
configurationsCorePath: configurationsCorePath,
configurationsServicesDirectory: configurationsServicesDirectory,
readFileBytesByFilePathDependency: readFileBytesByFilePath,
gelAllFinesNameByDirNameDependency: gelAllFinesNameByDirName,
}
}
func (bp configurationsParser) ReadConfigurationsCore() (*BeelzebubCoreConfigurations, error) {
buf, err := bp.readFileBytes(bp.configurationsCorePath)
buf, err := bp.readFileBytesByFilePathDependency(bp.configurationsCorePath)
if err != nil {
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) {
services, err := bp.readDir(bp.configurationsServicesDirectory)
services, err := bp.gelAllFinesNameByDirNameDependency(bp.configurationsServicesDirectory)
if err != nil {
return nil, fmt.Errorf("in directory %s: %v", bp.configurationsServicesDirectory, err)
}
@ -85,7 +85,7 @@ func (bp configurationsParser) ReadConfigurationsServices() ([]BeelzebubServiceC
var servicesConfiguration []BeelzebubServiceConfiguration
for _, servicesName := range services {
filePath := filepath.Join(bp.configurationsServicesDirectory, servicesName)
buf, err := bp.readFileBytes(filePath)
buf, err := bp.readFileBytesByFilePathDependency(filePath)
if err != nil {
return nil, fmt.Errorf("in file %s: %v", filePath, err)
}
@ -101,7 +101,7 @@ func (bp configurationsParser) ReadConfigurationsServices() ([]BeelzebubServiceC
return servicesConfiguration, nil
}
func readDir(dirName string) ([]string, error) {
func gelAllFinesNameByDirName(dirName string) ([]string, error) {
var filesName []string
files, err := ioutil.ReadDir(dirName)
if err != nil {
@ -114,6 +114,6 @@ func readDir(dirName string) ([]string, error) {
return filesName, nil
}
func readFileBytes(filePath string) ([]byte, error) {
func readFileBytesByFilePath(filePath string) ([]byte, error) {
return os.ReadFile(filePath)
}

View File

@ -50,14 +50,14 @@ commands:
func TestReadConfigurationsCoreError(t *testing.T) {
configurationsParser := Init("mockConfigurationsCorePath", "mockConfigurationsServicesDirectory")
configurationsParser.readFileBytes = mockReadfilebytesError
configurationsParser.readFileBytesByFilePathDependency = mockReadfilebytesError
beelzebubCoreConfigurations, err := configurationsParser.ReadConfigurationsCore()
assert.Nil(t, beelzebubCoreConfigurations)
assert.Error(t, err)
assert.Equal(t, "in file mockConfigurationsCorePath: mockErrorReadFileBytes", err.Error())
configurationsParser.readFileBytes = mockReadfilebytesFormatError
configurationsParser.readFileBytesByFilePathDependency = mockReadfilebytesFormatError
beelzebubCoreConfigurations, err = configurationsParser.ReadConfigurationsCore()
assert.Nil(t, beelzebubCoreConfigurations)
@ -67,7 +67,7 @@ func TestReadConfigurationsCoreError(t *testing.T) {
func TestReadConfigurationsCoreValid(t *testing.T) {
configurationsParser := Init("", "")
configurationsParser.readFileBytes = mockReadfilebytesConfigurationsCore
configurationsParser.readFileBytesByFilePathDependency = mockReadfilebytesConfigurationsCore
coreConfigurations, err := configurationsParser.ReadConfigurationsCore()
assert.Nil(t, err)
@ -82,8 +82,8 @@ func TestReadConfigurationsCoreValid(t *testing.T) {
func TestReadConfigurationsServicesFail(t *testing.T) {
configurationsParser := Init("", "")
configurationsParser.readFileBytes = mockReadfilebytesError
configurationsParser.readDir = mockReadDirError
configurationsParser.readFileBytesByFilePathDependency = mockReadfilebytesError
configurationsParser.gelAllFinesNameByDirNameDependency = mockReadDirError
beelzebubServiceConfiguration, err := configurationsParser.ReadConfigurationsServices()
assert.Nil(t, beelzebubServiceConfiguration)
@ -93,8 +93,8 @@ func TestReadConfigurationsServicesFail(t *testing.T) {
func TestReadConfigurationsServicesValid(t *testing.T) {
configurationsParser := Init("", "")
configurationsParser.readFileBytes = mockReadfilebytesBeelzebubServiceConfiguration
configurationsParser.readDir = mockReadDirValid
configurationsParser.readFileBytesByFilePathDependency = mockReadfilebytesBeelzebubServiceConfiguration
configurationsParser.gelAllFinesNameByDirNameDependency = mockReadDirValid
beelzebubServicesConfiguration, err := configurationsParser.ReadConfigurationsServices()