@@ -25,7 +25,14 @@ func main() {
25
25
var defaultConf = conf .GetDefaultConf ()
26
26
27
27
args = loadArgsValid ()
28
- cnf = loadConfValid (args .Config , defaultConf , "config.toml" )
28
+ if args .Verbose {
29
+ fmt .Printf ("args: %+v\n " , args )
30
+ }
31
+
32
+ cnf = loadConfValid (args .Verbose , args .Config , defaultConf , "config.toml" )
33
+ if args .Verbose {
34
+ fmt .Printf ("conf: %+v\n " , cnf )
35
+ }
29
36
overrideConf (& cnf , args )
30
37
validateConf (& cnf )
31
38
app .InitLogger (cnf .Log , args .Verbose )
@@ -103,20 +110,29 @@ func getAppDir() string {
103
110
return filepath .Dir (dir )
104
111
}
105
112
106
- func loadConfValid (path string , defaultConf conf.Conf , defaultConfPath string ) conf.Conf {
113
+ func loadConfValid (verbose bool , path string , defaultConf conf.Conf , defaultConfPath string ) conf.Conf {
107
114
if path == "" {
108
115
path = defaultConfPath
116
+ if verbose {
117
+ fmt .Println ("no config file specified, using default config file: " , path )
118
+ }
109
119
}
110
120
// app executable dir + config.toml has the highest priority
111
121
preferredPath := filepath .Join (getAppDir (), path )
112
122
if _ , err := os .Stat (preferredPath ); err == nil {
113
123
path = preferredPath
124
+ if verbose {
125
+ fmt .Println ("using config file: " , path )
126
+ }
114
127
}
128
+
115
129
err := decode (path , & defaultConf )
116
- if err != nil {
117
- logger .Warn ("failed to load config file: " , err , " using default config" )
130
+ if err != nil && verbose {
131
+ os .Stderr .WriteString (fmt .Sprintf ("Failed to load config file: %s\n " , err ))
132
+ }else
133
+ {
134
+ logger .WithField ("conf" , & defaultConf ).Debug ("configuration loaded" )
118
135
}
119
- logger .WithField ("conf" , & defaultConf ).Debug ("configuration loaded" )
120
136
return defaultConf
121
137
}
122
138
@@ -127,10 +143,18 @@ func decode(path string, conf *conf.Conf) (error) {
127
143
}
128
144
129
145
switch ext {
130
- case ". toml" :
146
+ case "toml" :
131
147
_ , err = toml .DecodeFile (path , conf )
132
- case ".yaml" , ".yml" :
133
- err = yaml .Unmarshal ([]byte (path ), conf )
148
+ case "yaml" , "yml" :
149
+ content , err := os .ReadFile (path )
150
+ if err != nil {
151
+ return fmt .Errorf ("failed to read config file: %s" , err )
152
+ }
153
+
154
+ err = yaml .Unmarshal ([]byte (content ), conf )
155
+ if err != nil {
156
+ return fmt .Errorf ("failed to parse config file: %s" , err )
157
+ }
134
158
default :
135
159
err = fmt .Errorf ("unsupported config file extension: %s" , ext )
136
160
}
0 commit comments