mirror of
https://github.com/caddyserver/caddy.git
synced 2024-11-22 11:33:06 +08:00
* Require config for caddy validate - fixes #5612 Signed-off-by: Pistasj <hi@pistasjis.net> * Try making adjacent Caddyfile check its own function Signed-off-by: Pistasj <hi@pistasjis.net> * add Francis' suggestion Co-authored-by: Francis Lavoie <lavofr@gmail.com> * Refactor * Fix borked commit, sigh --------- Signed-off-by: Pistasj <hi@pistasjis.net> Co-authored-by: Francis Lavoie <lavofr@gmail.com> Co-authored-by: Matthew Holt <mholt@users.noreply.github.com>
This commit is contained in:
parent
11166889c5
commit
d8135505d3
|
@ -22,6 +22,7 @@ import (
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
|
"io/fs"
|
||||||
"log"
|
"log"
|
||||||
"net"
|
"net"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
@ -423,24 +424,12 @@ func cmdAdaptConfig(fl Flags) (int, error) {
|
||||||
adaptCmdPrettyFlag := fl.Bool("pretty")
|
adaptCmdPrettyFlag := fl.Bool("pretty")
|
||||||
adaptCmdValidateFlag := fl.Bool("validate")
|
adaptCmdValidateFlag := fl.Bool("validate")
|
||||||
|
|
||||||
// if no input file was specified, try a default
|
var err error
|
||||||
// Caddyfile if the Caddyfile adapter is plugged in
|
adaptCmdInputFlag, err = configFileWithRespectToDefault(caddy.Log(), adaptCmdInputFlag)
|
||||||
if adaptCmdInputFlag == "" && caddyconfig.GetAdapter("caddyfile") != nil {
|
if err != nil {
|
||||||
_, err := os.Stat("Caddyfile")
|
return caddy.ExitCodeFailedStartup, err
|
||||||
if err == nil {
|
|
||||||
// default Caddyfile exists
|
|
||||||
adaptCmdInputFlag = "Caddyfile"
|
|
||||||
caddy.Log().Info("using adjacent Caddyfile")
|
|
||||||
} else if !os.IsNotExist(err) {
|
|
||||||
// default Caddyfile exists, but error accessing it
|
|
||||||
return caddy.ExitCodeFailedStartup, fmt.Errorf("accessing default Caddyfile: %v", err)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if adaptCmdInputFlag == "" {
|
|
||||||
return caddy.ExitCodeFailedStartup,
|
|
||||||
fmt.Errorf("input file required when there is no Caddyfile in current directory (use --config flag)")
|
|
||||||
}
|
|
||||||
if adaptCmdAdapterFlag == "" {
|
if adaptCmdAdapterFlag == "" {
|
||||||
return caddy.ExitCodeFailedStartup,
|
return caddy.ExitCodeFailedStartup,
|
||||||
fmt.Errorf("adapter name is required (use --adapt flag or leave unspecified for default)")
|
fmt.Errorf("adapter name is required (use --adapt flag or leave unspecified for default)")
|
||||||
|
@ -517,6 +506,17 @@ func cmdValidateConfig(fl Flags) (int, error) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// use default config and ensure a config file is specified
|
||||||
|
var err error
|
||||||
|
validateCmdConfigFlag, err = configFileWithRespectToDefault(caddy.Log(), validateCmdConfigFlag)
|
||||||
|
if err != nil {
|
||||||
|
return caddy.ExitCodeFailedStartup, err
|
||||||
|
}
|
||||||
|
if validateCmdConfigFlag == "" {
|
||||||
|
return caddy.ExitCodeFailedStartup,
|
||||||
|
fmt.Errorf("input file required when there is no Caddyfile in current directory (use --config flag)")
|
||||||
|
}
|
||||||
|
|
||||||
input, _, err := LoadConfig(validateCmdConfigFlag, validateCmdAdapterFlag)
|
input, _, err := LoadConfig(validateCmdConfigFlag, validateCmdAdapterFlag)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return caddy.ExitCodeFailedStartup, err
|
return caddy.ExitCodeFailedStartup, err
|
||||||
|
@ -736,6 +736,31 @@ func DetermineAdminAPIAddress(address string, config []byte, configFile, configA
|
||||||
return caddy.DefaultAdminListen, nil
|
return caddy.DefaultAdminListen, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// configFileWithRespectToDefault returns the filename to use for loading the config, based
|
||||||
|
// on whether a config file is already specified and a supported default config file exists.
|
||||||
|
func configFileWithRespectToDefault(logger *zap.Logger, configFile string) (string, error) {
|
||||||
|
const defaultCaddyfile = "Caddyfile"
|
||||||
|
|
||||||
|
// if no input file was specified, try a default Caddyfile if the Caddyfile adapter is plugged in
|
||||||
|
if configFile == "" && caddyconfig.GetAdapter("caddyfile") != nil {
|
||||||
|
_, err := os.Stat(defaultCaddyfile)
|
||||||
|
if err == nil {
|
||||||
|
// default Caddyfile exists
|
||||||
|
if logger != nil {
|
||||||
|
logger.Info("using adjacent Caddyfile")
|
||||||
|
}
|
||||||
|
return defaultCaddyfile, nil
|
||||||
|
}
|
||||||
|
if !errors.Is(err, fs.ErrNotExist) {
|
||||||
|
// problem checking
|
||||||
|
return configFile, fmt.Errorf("checking if default Caddyfile exists: %v", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// default config file does not exist or is irrelevant
|
||||||
|
return configFile, nil
|
||||||
|
}
|
||||||
|
|
||||||
type moduleInfo struct {
|
type moduleInfo struct {
|
||||||
caddyModuleID string
|
caddyModuleID string
|
||||||
goModule *debug.Module
|
goModule *debug.Module
|
||||||
|
|
|
@ -117,9 +117,8 @@ func loadConfigWithLogger(logger *zap.Logger, configFile, adapterName string) ([
|
||||||
zap.String("config_adapter", adapterName))
|
zap.String("config_adapter", adapterName))
|
||||||
}
|
}
|
||||||
} else if adapterName == "" {
|
} else if adapterName == "" {
|
||||||
// as a special case when no config file or adapter
|
// if the Caddyfile adapter is plugged in, we can try using an
|
||||||
// is specified, see if the Caddyfile adapter is
|
// adjacent Caddyfile by default
|
||||||
// plugged in, and if so, try using a default Caddyfile
|
|
||||||
cfgAdapter = caddyconfig.GetAdapter("caddyfile")
|
cfgAdapter = caddyconfig.GetAdapter("caddyfile")
|
||||||
if cfgAdapter != nil {
|
if cfgAdapter != nil {
|
||||||
config, err = os.ReadFile("Caddyfile")
|
config, err = os.ReadFile("Caddyfile")
|
||||||
|
|
Loading…
Reference in New Issue
Block a user