-
Notifications
You must be signed in to change notification settings - Fork 0
/
root.go
executable file
·92 lines (74 loc) · 2.77 KB
/
root.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
package config
import (
"encoding/json"
"fmt"
"strings"
"github.com/PrunedNeuron/Fluoride/pkg/errors"
"github.com/spf13/viper"
"go.uber.org/zap"
)
// Configuration is the primary configuration model
type Configuration struct {
Application ApplicationConfiguration `json:"application" yaml:"application" mapstructure:"application"`
Profiler ProfilerConfiguration `json:"profiler" yaml:"profiler" mapstructure:"profiler"`
Logger LoggerConfiguration `json:"logger" yaml:"logger" mapstructure:"logger"`
Server ServerConfiguration `json:"server" yaml:"server" mapstructure:"server"`
Database DatabaseConfiguration `json:"database" yaml:"database" mapstructure:"database"`
}
var (
configuration Configuration
logger *zap.SugaredLogger
)
// Configure loads the configuration file into the config structs
func Configure(configurations ...string) {
logger := zap.S().With("package", "config")
SetDefaults()
viper.SetTypeByDefaultValue(true)
viper.SetEnvKeyReplacer(strings.NewReplacer(".", "_"))
viper.AutomaticEnv()
if len(configurations) > 1 {
logger.Errorf("Cannot process multiple configuration files")
} else if len(configurations) == 0 || configurations[0] == "" {
logger.Debugf("No configuration file passed, using defaults") // Default configuration file if nothing is passed
viper.SetConfigName("config")
viper.AddConfigPath(".")
viper.SetConfigType("yml")
if err := viper.ReadInConfig(); err != nil {
logger.Errorf("Failed to read configuration file! ERROR: %s\n", err.Error())
}
logger.Debugf("Unmarshalling the read configuration file into the configuration struct")
err := viper.Unmarshal(&configuration)
if err != nil {
logger.Errorf("Failed to unmarshal configuration file into struct, %s", err.Error())
}
} else {
logger.Debugf("Valid configuration file passed, using")
viper.SetConfigFile(configurations[0])
if err := viper.ReadInConfig(); err != nil {
logger.Errorf("Failed to read custom configuration file!: %s ERROR: %s\n", configurations[0], err.Error())
}
}
}
// GetConfig returns the global configuration
func GetConfig() *Configuration {
if configuration.IsEmpty() {
Configure()
}
return &configuration
}
// SetConfig sets the global configuration
func SetConfig(config *Configuration) {
configuration = *config
}
func (config *Configuration) String() string {
json, err := json.Marshal(config)
if err != nil {
zap.S().Errorf("Failed to marshal struct into json, error: %s\n", err)
return errors.ErrMarshal.Error()
}
return fmt.Sprintf(string(json))
}
// IsEmpty checks whether the struct is empty / uninitialized / nil
func (config *Configuration) IsEmpty() bool {
return config.Logger.IsEmpty() && config.Server.IsEmpty() && config.Application.IsEmpty() && config.Database.IsEmpty()
}