Skip to content

Commit

Permalink
Improved config management
Browse files Browse the repository at this point in the history
  • Loading branch information
pzaino committed Jan 20, 2024
1 parent 6b68319 commit 952fb90
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 7 deletions.
22 changes: 22 additions & 0 deletions pkg/config/test_config.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
database:
host: localhost
port: 5432
user: postgres
password: test_password
dbname: SitesIndex
crawler:
workers: 5
depth: 1
delay: 2
timeout: 10
maintenance: 60
api:
port: 8080
host: 0.0.0.0
timeout: 10
selenium:
type: chrome
port: 4444
headless: true
host: localhost
debug_level: 1
32 changes: 25 additions & 7 deletions pkg/config/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package config

import (
"fmt"
"os"
"runtime"

Expand Down Expand Up @@ -53,26 +54,38 @@ type Config struct {
DebugLevel int `yaml:"debug_level"`
}

func fileExists(filename string) bool {
info, err := os.Stat(filename)
if os.IsNotExist(err) {
return false
}
return !info.IsDir()
}

func getConfigFile(confName string) (Config, error) {

// Check if the configuration file exists
if !fileExists(confName) {
return Config{}, fmt.Errorf("file does not exist: %s", confName)
}

// Read the configuration file
data, err := os.ReadFile(confName)

// If the configuration file has been found and is not empty, unmarshal it
var config Config
if data != nil && err == nil {
if (data != nil) && (err == nil) {
err = yaml.Unmarshal(data, &config)
if err != nil {
return config, err
}
}
return config, nil
return config, err
}

// This function is responsible for loading the configuration file
// and return the Config struct
func LoadConfig(confName string) (Config, error) {

// Get the configuration file
config, _ := getConfigFile(confName)
config, err := getConfigFile(confName)

// Set the OS variable
config.OS = runtime.GOOS
Expand Down Expand Up @@ -134,5 +147,10 @@ func LoadConfig(confName string) (Config, error) {
config.Selenium.Host = "localhost"
}

return config, nil
return config, err
}

// This function is responsible for returning true if the config is empty or false otherwise
func ConfigIsEmpty(config Config) bool {
return config == Config{}
}
45 changes: 45 additions & 0 deletions pkg/config/types_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// Copyright 2023 Paolo Fabio Zaino
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package config

import (
"testing"
)

// Test LoadConfig
func TestLoadConfig(t *testing.T) {
// Call the function
config, err := LoadConfig("./test_config.yml")

// Check for errors
if err != nil {
t.Errorf("LoadConfig returned an error: %v", err)
}

// Check if the returned structure matches the expected output
if ConfigIsEmpty(config) {
t.Errorf("No config was loaded")
}

// Additional checks can be added to validate the contents of 'config'
}

// Test LoadConfigInvalidFile
func TestLoadConfigInvalidFile(t *testing.T) {
_, err := LoadConfig("./invalid_test_config.yaml")
if err == nil {
t.Errorf("Expected error, got none")
}
}

0 comments on commit 952fb90

Please sign in to comment.