Fast and extensible multi-platform HTTP/1-2-3 web server with automatic HTTPS
Mirrored from https://github.com/Slazee/caddy.git
Synced Nov 12, 17:00 UTC
cmd: Adjust config load logs/errors (#6032)
* cmd: Adjust config load logs/errors * Update cmd/main.go Co-authored-by: Matt Holt <mholt@users.noreply.github.com> --------- Co-authored-by: Matt Holt <mholt@users.noreply.github.com>
Francis Lavoie committed
Mar 5, 2024 at 14:26 UTC
e473ae6803a95a8e85ba867d1fa1d205d98b73d8
1 file changed
+27
-14
cmd/main.go
+27
-14
@@ -17,6 +17,7 @@ package caddycmd
17
import (
18
"bufio"
19
"bytes"
20
+ "encoding/json"
21
"errors"
22
"flag"
23
"fmt"
@@ -107,6 +108,12 @@ func LoadConfig(configFile, adapterName string) ([]byte, string, error) {
108
}
109
110
func loadConfigWithLogger(logger *zap.Logger, configFile, adapterName string) ([]byte, string, error) {
111
+ // if no logger is provided, use a nop logger
112
+ // just so we don't have to check for nil
113
+ if logger == nil {
114
+ logger = zap.NewNop()
115
+ }
116
+
117
// specifying an adapter without a config file is ambiguous
118
if adapterName != "" && configFile == "" {
119
return nil, "", fmt.Errorf("cannot adapt config without config file (use --config)")
@@ -119,16 +126,16 @@ func loadConfigWithLogger(logger *zap.Logger, configFile, adapterName string) ([
126
if configFile != "" {
127
if configFile == "-" {
128
config, err = io.ReadAll(os.Stdin)
129
+ if err != nil {
130
+ return nil, "", fmt.Errorf("reading config from stdin: %v", err)
131
+ }
132
+ logger.Info("using config from stdin")
133
} else {
134
config, err = os.ReadFile(configFile)
124
- }
125
- if err != nil {
126
- return nil, "", fmt.Errorf("reading config file: %v", err)
127
- }
128
- if logger != nil {
129
- logger.Info("using provided configuration",
130
- zap.String("config_file", configFile),
131
- zap.String("config_adapter", adapterName))
135
+ if err != nil {
136
+ return nil, "", fmt.Errorf("reading config from file: %v", err)
137
+ }
138
+ logger.Info("using config from file", zap.String("file", configFile))
139
}
140
} else if adapterName == "" {
141
// if the Caddyfile adapter is plugged in, we can try using an
@@ -145,9 +152,7 @@ func loadConfigWithLogger(logger *zap.Logger, configFile, adapterName string) ([
152
} else {
153
// success reading default Caddyfile
154
configFile = "Caddyfile"
148
- if logger != nil {
149
- logger.Info("using adjacent Caddyfile")
150
- }
155
+ logger.Info("using adjacent Caddyfile")
156
}
157
}
158
}
@@ -177,16 +182,24 @@ func loadConfigWithLogger(logger *zap.Logger, configFile, adapterName string) ([
182
if err != nil {
183
return nil, "", fmt.Errorf("adapting config using %s: %v", adapterName, err)
184
}
185
+ logger.Info("adapted config to JSON", zap.String("adapter", adapterName))
186
for _, warn := range warnings {
187
msg := warn.Message
188
if warn.Directive != "" {
189
msg = fmt.Sprintf("%s: %s", warn.Directive, warn.Message)
190
}
185
- if logger != nil {
186
- logger.Warn(msg, zap.String("adapter", adapterName), zap.String("file", warn.File), zap.Int("line", warn.Line))
187
- }
191
+ logger.Warn(msg,
192
+ zap.String("adapter", adapterName),
193
+ zap.String("file", warn.File),
194
+ zap.Int("line", warn.Line))
195
}
196
config = adaptedConfig
197
+ } else {
198
+ // validate that the config is at least valid JSON
199
+ err = json.Unmarshal(config, new(any))
200
+ if err != nil {
201
+ return nil, "", fmt.Errorf("config is not valid JSON: %v; did you mean to use a config adapter (the --adapter flag)?", err)
202
+ }
203
}
204
205
return config, configFile, nil