aboutsummaryrefslogtreecommitdiffhomepage
path: root/cmd
diff options
context:
space:
mode:
authorFrancis Lavoie <[email protected]>2024-03-05 14:26:30 -0500
committerGitHub <[email protected]>2024-03-05 19:26:30 +0000
commite473ae6803a95a8e85ba867d1fa1d205d98b73d8 (patch)
tree44039db8b5bc81650f772911a89156012f4f47ca /cmd
parent72ce78d9af0204a4018ce197ff95f681a720818b (diff)
downloadcaddy-e473ae6803a95a8e85ba867d1fa1d205d98b73d8.tar.gz
caddy-e473ae6803a95a8e85ba867d1fa1d205d98b73d8.zip
cmd: Adjust config load logs/errors (#6032)
* cmd: Adjust config load logs/errors * Update cmd/main.go Co-authored-by: Matt Holt <[email protected]> --------- Co-authored-by: Matt Holt <[email protected]>
Diffstat (limited to 'cmd')
-rw-r--r--cmd/main.go41
1 files changed, 27 insertions, 14 deletions
diff --git a/cmd/main.go b/cmd/main.go
index 6fd58c62f..d832cbc5e 100644
--- a/cmd/main.go
+++ b/cmd/main.go
@@ -17,6 +17,7 @@ package caddycmd
import (
"bufio"
"bytes"
+ "encoding/json"
"errors"
"flag"
"fmt"
@@ -107,6 +108,12 @@ func LoadConfig(configFile, adapterName string) ([]byte, string, error) {
}
func loadConfigWithLogger(logger *zap.Logger, configFile, adapterName string) ([]byte, string, error) {
+ // if no logger is provided, use a nop logger
+ // just so we don't have to check for nil
+ if logger == nil {
+ logger = zap.NewNop()
+ }
+
// specifying an adapter without a config file is ambiguous
if adapterName != "" && configFile == "" {
return nil, "", fmt.Errorf("cannot adapt config without config file (use --config)")
@@ -119,16 +126,16 @@ func loadConfigWithLogger(logger *zap.Logger, configFile, adapterName string) ([
if configFile != "" {
if configFile == "-" {
config, err = io.ReadAll(os.Stdin)
+ if err != nil {
+ return nil, "", fmt.Errorf("reading config from stdin: %v", err)
+ }
+ logger.Info("using config from stdin")
} else {
config, err = os.ReadFile(configFile)
- }
- if err != nil {
- return nil, "", fmt.Errorf("reading config file: %v", err)
- }
- if logger != nil {
- logger.Info("using provided configuration",
- zap.String("config_file", configFile),
- zap.String("config_adapter", adapterName))
+ if err != nil {
+ return nil, "", fmt.Errorf("reading config from file: %v", err)
+ }
+ logger.Info("using config from file", zap.String("file", configFile))
}
} else if adapterName == "" {
// if the Caddyfile adapter is plugged in, we can try using an
@@ -145,9 +152,7 @@ func loadConfigWithLogger(logger *zap.Logger, configFile, adapterName string) ([
} else {
// success reading default Caddyfile
configFile = "Caddyfile"
- if logger != nil {
- logger.Info("using adjacent Caddyfile")
- }
+ logger.Info("using adjacent Caddyfile")
}
}
}
@@ -177,16 +182,24 @@ func loadConfigWithLogger(logger *zap.Logger, configFile, adapterName string) ([
if err != nil {
return nil, "", fmt.Errorf("adapting config using %s: %v", adapterName, err)
}
+ logger.Info("adapted config to JSON", zap.String("adapter", adapterName))
for _, warn := range warnings {
msg := warn.Message
if warn.Directive != "" {
msg = fmt.Sprintf("%s: %s", warn.Directive, warn.Message)
}
- if logger != nil {
- logger.Warn(msg, zap.String("adapter", adapterName), zap.String("file", warn.File), zap.Int("line", warn.Line))
- }
+ logger.Warn(msg,
+ zap.String("adapter", adapterName),
+ zap.String("file", warn.File),
+ zap.Int("line", warn.Line))
}
config = adaptedConfig
+ } else {
+ // validate that the config is at least valid JSON
+ err = json.Unmarshal(config, new(any))
+ if err != nil {
+ return nil, "", fmt.Errorf("config is not valid JSON: %v; did you mean to use a config adapter (the --adapter flag)?", err)
+ }
}
return config, configFile, nil