Skip to content

Commit 0d0f567

Browse files
config: move config reading logic into function
1 parent 1c276b0 commit 0d0f567

File tree

2 files changed

+28
-15
lines changed

2 files changed

+28
-15
lines changed

src/config.go

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,23 +23,27 @@ type Config struct {
2323
KeepAliveTimeout int
2424
}
2525

26-
// Load config from the provided string.
27-
// Returns an error if the string is not a valid YAML.
28-
func loadConfigFromString(configString string) (*Config, error) {
29-
logrus.Info("loading config from string")
26+
// Tries to load a config from the `CONFIG` environment variable.
27+
// If the environment variable is not set, tries to load a config from the
28+
// provided path to the config file (YAML). Returns an error if the config could
29+
// not be loaded.
30+
func loadConfig(path string) (*Config, error) {
31+
config, err := loadConfigFromEnv()
32+
if err != nil {
33+
if !errors.Is(err, ErrNoConfigEnvVar) {
34+
return nil, err
35+
}
3036

31-
var config Config
32-
if err := yaml.Unmarshal([]byte(configString), &config); err != nil {
33-
return nil, fmt.Errorf("failed to unmarshal YAML file: %w", err)
37+
return loadConfigFromPath(path)
3438
}
3539

36-
return &config, nil
40+
return config, nil
3741
}
3842

3943
// ErrNoConfigEnvVar is returned when the CONFIG environment variable is not set.
4044
var ErrNoConfigEnvVar = errors.New("environment variable not set or invalid")
4145

42-
// Tries to load the config from environment variables.
46+
// Tries to load the config from environment variable (`CONFIG`).
4347
// Returns an error if not all environment variables are set.
4448
func loadConfigFromEnv() (*Config, error) {
4549
configEnv := os.Getenv("CONFIG")
@@ -61,3 +65,16 @@ func loadConfigFromPath(path string) (*Config, error) {
6165

6266
return loadConfigFromString(string(file))
6367
}
68+
69+
// Load config from the provided string.
70+
// Returns an error if the string is not a valid YAML.
71+
func loadConfigFromString(configString string) (*Config, error) {
72+
logrus.Info("loading config from string")
73+
74+
var config Config
75+
if err := yaml.Unmarshal([]byte(configString), &config); err != nil {
76+
return nil, fmt.Errorf("failed to unmarshal YAML file: %w", err)
77+
}
78+
79+
return &config, nil
80+
}

src/main.go

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -116,14 +116,10 @@ func main() {
116116
go killListener(c, beforeExit)
117117

118118
var err error
119+
config, err = loadConfig(*configFilePath)
119120

120-
config, err = loadConfigFromEnv()
121121
if err != nil {
122-
logrus.WithError(err).Info("failed to load config from environment variables, trying to load from file")
123-
124-
if config, err = loadConfigFromPath(*configFilePath); err != nil {
125-
logrus.WithError(err).Fatal("failed to load config file")
126-
}
122+
logrus.WithError(err).Fatal("could not load config")
127123
}
128124

129125
InitMatrix()

0 commit comments

Comments
 (0)