From 952fb90d4e682172707c5e5e4a4af916eee0a0f3 Mon Sep 17 00:00:00 2001 From: Paolo Fabio Zaino Date: Sat, 20 Jan 2024 17:51:51 +0000 Subject: [PATCH] Improved config management --- pkg/config/test_config.yml | 22 +++++++++++++++++++ pkg/config/types.go | 32 +++++++++++++++++++++------ pkg/config/types_test.go | 45 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 92 insertions(+), 7 deletions(-) create mode 100644 pkg/config/test_config.yml create mode 100644 pkg/config/types_test.go diff --git a/pkg/config/test_config.yml b/pkg/config/test_config.yml new file mode 100644 index 00000000..422d524e --- /dev/null +++ b/pkg/config/test_config.yml @@ -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 diff --git a/pkg/config/types.go b/pkg/config/types.go index 68a0a2c5..5657dc14 100644 --- a/pkg/config/types.go +++ b/pkg/config/types.go @@ -15,6 +15,7 @@ package config import ( + "fmt" "os" "runtime" @@ -53,18 +54,30 @@ 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 @@ -72,7 +85,7 @@ func getConfigFile(confName string) (Config, error) { 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 @@ -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{} } diff --git a/pkg/config/types_test.go b/pkg/config/types_test.go new file mode 100644 index 00000000..488a6fc7 --- /dev/null +++ b/pkg/config/types_test.go @@ -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") + } +}