mirror of
https://github.com/caddyserver/caddy.git
synced 2024-11-22 15:19:25 +08:00
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>
This commit is contained in:
parent
72ce78d9af
commit
e473ae6803
41
cmd/main.go
41
cmd/main.go
|
@ -17,6 +17,7 @@ package caddycmd
|
||||||
import (
|
import (
|
||||||
"bufio"
|
"bufio"
|
||||||
"bytes"
|
"bytes"
|
||||||
|
"encoding/json"
|
||||||
"errors"
|
"errors"
|
||||||
"flag"
|
"flag"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
@ -107,6 +108,12 @@ func LoadConfig(configFile, adapterName string) ([]byte, string, error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func loadConfigWithLogger(logger *zap.Logger, 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
|
// specifying an adapter without a config file is ambiguous
|
||||||
if adapterName != "" && configFile == "" {
|
if adapterName != "" && configFile == "" {
|
||||||
return nil, "", fmt.Errorf("cannot adapt config without config file (use --config)")
|
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 != "" {
|
||||||
if configFile == "-" {
|
if configFile == "-" {
|
||||||
config, err = io.ReadAll(os.Stdin)
|
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 {
|
} else {
|
||||||
config, err = os.ReadFile(configFile)
|
config, err = os.ReadFile(configFile)
|
||||||
}
|
if err != nil {
|
||||||
if err != nil {
|
return nil, "", fmt.Errorf("reading config from file: %v", err)
|
||||||
return nil, "", fmt.Errorf("reading config file: %v", err)
|
}
|
||||||
}
|
logger.Info("using config from file", zap.String("file", configFile))
|
||||||
if logger != nil {
|
|
||||||
logger.Info("using provided configuration",
|
|
||||||
zap.String("config_file", configFile),
|
|
||||||
zap.String("config_adapter", adapterName))
|
|
||||||
}
|
}
|
||||||
} else if adapterName == "" {
|
} else if adapterName == "" {
|
||||||
// if the Caddyfile adapter is plugged in, we can try using an
|
// 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 {
|
} else {
|
||||||
// success reading default Caddyfile
|
// success reading default Caddyfile
|
||||||
configFile = "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 {
|
if err != nil {
|
||||||
return nil, "", fmt.Errorf("adapting config using %s: %v", adapterName, err)
|
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 {
|
for _, warn := range warnings {
|
||||||
msg := warn.Message
|
msg := warn.Message
|
||||||
if warn.Directive != "" {
|
if warn.Directive != "" {
|
||||||
msg = fmt.Sprintf("%s: %s", warn.Directive, warn.Message)
|
msg = fmt.Sprintf("%s: %s", warn.Directive, warn.Message)
|
||||||
}
|
}
|
||||||
if logger != nil {
|
logger.Warn(msg,
|
||||||
logger.Warn(msg, zap.String("adapter", adapterName), zap.String("file", warn.File), zap.Int("line", warn.Line))
|
zap.String("adapter", adapterName),
|
||||||
}
|
zap.String("file", warn.File),
|
||||||
|
zap.Int("line", warn.Line))
|
||||||
}
|
}
|
||||||
config = adaptedConfig
|
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
|
return config, configFile, nil
|
||||||
|
|
Loading…
Reference in New Issue
Block a user