Skip to content

Commit aacefbe

Browse files
Adler FleurantAdler Fleurant
authored andcommitted
Fixes #54: Verifies that configuration files exist before trying to load it.
1 parent 3e5b6ad commit aacefbe

1 file changed

Lines changed: 31 additions & 13 deletions

File tree

pkg/config/config.go

Lines changed: 31 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,19 @@ package config
1111

1212
import (
1313
"encoding/json"
14+
"github.com/micro/go-config/source/env"
15+
"github.com/micro/go-config/source/flag"
1416
"os"
1517

16-
config "github.com/micro/go-config"
17-
"github.com/micro/go-config/source/env"
18+
"github.com/micro/go-config"
1819
"github.com/micro/go-config/source/file"
19-
"github.com/micro/go-config/source/flag"
2020
"github.com/micro/go-config/source/memory"
2121
"github.com/redhat-cop/cert-operator/pkg/certs"
22-
"github.com/sirupsen/logrus"
22+
logf "sigs.k8s.io/controller-runtime/pkg/runtime/log"
2323
)
2424

25+
var log = logf.Log.WithName("Config")
26+
2527
type Config struct {
2628
// Notifiers []notifier.Notifier `json:"notifiers"`
2729
Provider certs.ProviderConfig `json:"provider"`
@@ -44,7 +46,6 @@ type AnnotationConfig struct {
4446

4547
const (
4648
defaultConfigFile = "/etc/cert-operator/config.yaml"
47-
defaultProvider = "self-signed"
4849
defaultConfig = `
4950
{
5051
"general": {
@@ -74,31 +75,48 @@ func NewConfig() Config {
7475
memorySource := memory.NewSource(
7576
memory.WithData(data),
7677
)
77-
// Load json config file
78-
tmpConfig.Load(
78+
79+
log.Info("Using default configuration.")
80+
_ = tmpConfig.Load(
7981
memorySource,
80-
file.NewSource(
81-
file.WithPath(getConfigFile()),
82-
),
82+
)
83+
84+
configFile := getConfigFile()
85+
if fileExist(configFile) {
86+
log.Info("Loading config file from ", configFile)
87+
88+
_ = tmpConfig.Load(file.NewSource(
89+
file.WithPath(configFile),
90+
))
91+
}
92+
93+
_ = tmpConfig.Load(
8394
env.NewSource(),
8495
flag.NewSource(),
8596
)
8697
var conf Config
8798

88-
tmpConfig.Scan(&conf)
99+
err := tmpConfig.Scan(&conf)
100+
101+
if err != nil {
102+
log.Error(err, "Could not load configurations properly.")
103+
}
89104

90105
return conf
91106
}
92107

93108
func getConfigFile() (configFile string) {
94109
if value, ok := os.LookupEnv("CERT_OP_CONFIG"); ok {
95-
logrus.Infof("Loading custom config file from %v", value)
96110
return value
97111
}
98-
logrus.Infof("Loading default config file from %v", defaultConfigFile)
99112
return defaultConfigFile
100113
}
101114

115+
func fileExist(path string) bool {
116+
_, err := os.Stat(path)
117+
return !os.IsNotExist(err)
118+
}
119+
102120
func (c *Config) String() string {
103121
out, err := json.Marshal(c)
104122
if err != nil {

0 commit comments

Comments
 (0)