Skip to content

Commit

Permalink
Configuration verification before it's passed along (#42)
Browse files Browse the repository at this point in the history
* Configuration verification before it's passed along

* fixed spaces
  • Loading branch information
Ignacio authored Dec 21, 2022
1 parent ccf437d commit d2f8c58
Showing 1 changed file with 34 additions and 0 deletions.
34 changes: 34 additions & 0 deletions configuration/configuration.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package configuration

import (
"os"
"reflect"

"github.com/sirupsen/logrus"
"github.com/spf13/viper"
Expand Down Expand Up @@ -41,6 +42,7 @@ func (c *Configuration) GetConfiguration() *Configuration {
if err != nil {
logrus.Fatal("Following error reading from config file", err)
}

err = viper.Unmarshal(&c)
if err != nil {
logrus.Fatal("Error structuring configuration", err)
Expand All @@ -49,9 +51,41 @@ func (c *Configuration) GetConfiguration() *Configuration {
if c.DownloadDir == "" {
c.DownloadDir = "/tmp/syncron/"
}
checkConfig(*c)

logrus.Info("Your configuration file was read succesfully")
logrus.Info("Reading from bucket: ", c.S3.Bucket)

return c
}

// This function checks a Configuration struct and its embedded S3 struct for empty fields.
// It does this by using the reflect package to get the values and types.
func checkConfig(c Configuration) {

values := reflect.ValueOf(c)
valuesS3 := reflect.ValueOf(c.S3)

//Check if struct that holds configuration is empty
if reflect.ValueOf(c).IsZero() {
logrus.Fatal("Configuration struct appears to be empty")
}

typesS3 := valuesS3.Type()
types := values.Type()

// Check for configuration struct fields
for i := 0; i < valuesS3.NumField(); i++ {
if valuesS3.Field(i).IsZero() {
logrus.Fatal(typesS3.Field(i).Name, " field in config appears to be empty")
}
}

// Check for S3 struct fields
for i := 0; i < values.NumField(); i++ {
if values.Field(i).IsZero() {
logrus.Fatal(types.Field(i).Name, " field in config appears to be empty")
}
}

}

0 comments on commit d2f8c58

Please sign in to comment.