Browse Source

cmd: Emit error if reload cannot find a config to load

master
Matthew Holt 5 years ago
parent
commit
6614d1c495
No known key found for this signature in database GPG Key ID: 2A349DD577D586A5
  1. 7
      cmd/commandfuncs.go
  2. 22
      cmd/main.go

7
cmd/commandfuncs.go

@ -171,7 +171,7 @@ func cmdRun(fl Flags) (int, error) {
} }
// we don't use 'else' here since this value might have been changed in 'if' block; i.e. not mutually exclusive // we don't use 'else' here since this value might have been changed in 'if' block; i.e. not mutually exclusive
if !runCmdResumeFlag { if !runCmdResumeFlag {
config, err = loadConfig(runCmdConfigFlag, runCmdConfigAdapterFlag) config, _, err = loadConfig(runCmdConfigFlag, runCmdConfigAdapterFlag)
if err != nil { if err != nil {
return caddy.ExitCodeFailedStartup, err return caddy.ExitCodeFailedStartup, err
} }
@ -265,10 +265,13 @@ func cmdReload(fl Flags) (int, error) {
reloadCmdAddrFlag := fl.String("address") reloadCmdAddrFlag := fl.String("address")
// get the config in caddy's native format // get the config in caddy's native format
config, err := loadConfig(reloadCmdConfigFlag, reloadCmdConfigAdapterFlag) config, hasConfig, err := loadConfig(reloadCmdConfigFlag, reloadCmdConfigAdapterFlag)
if err != nil { if err != nil {
return caddy.ExitCodeFailedStartup, err return caddy.ExitCodeFailedStartup, err
} }
if !hasConfig {
return caddy.ExitCodeFailedStartup, fmt.Errorf("no config file to load")
}
// get the address of the admin listener and craft endpoint URL // get the address of the admin listener and craft endpoint URL
adminAddr := reloadCmdAddrFlag adminAddr := reloadCmdAddrFlag

22
cmd/main.go

@ -93,12 +93,16 @@ func handlePingbackConn(conn net.Conn, expect []byte) error {
// loadConfig loads the config from configFile and adapts it // loadConfig loads the config from configFile and adapts it
// using adapterName. If adapterName is specified, configFile // using adapterName. If adapterName is specified, configFile
// must be also. It prints any warnings to stderr, and returns // must be also. If no configFile is specified, it tries
// the resulting JSON config bytes. // loading a default config file. The lack of a config file is
func loadConfig(configFile, adapterName string) ([]byte, error) { // not treated as an error, but false will be returned if
// there is no config available. It prints any warnings to stderr,
// and returns the resulting JSON config bytes along with
// whether a config file was loaded or not.
func loadConfig(configFile, adapterName string) ([]byte, bool, error) {
// 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, false, fmt.Errorf("cannot adapt config without config file (use --config)")
} }
// load initial config and adapter // load initial config and adapter
@ -108,7 +112,7 @@ func loadConfig(configFile, adapterName string) ([]byte, error) {
if configFile != "" { if configFile != "" {
config, err = ioutil.ReadFile(configFile) config, err = ioutil.ReadFile(configFile)
if err != nil { if err != nil {
return nil, fmt.Errorf("reading config file: %v", err) return nil, false, fmt.Errorf("reading config file: %v", err)
} }
caddy.Log().Info("using provided configuration", caddy.Log().Info("using provided configuration",
zap.String("config_file", configFile), zap.String("config_file", configFile),
@ -125,7 +129,7 @@ func loadConfig(configFile, adapterName string) ([]byte, error) {
cfgAdapter = nil cfgAdapter = nil
} else if err != nil { } else if err != nil {
// default Caddyfile exists, but error reading it // default Caddyfile exists, but error reading it
return nil, fmt.Errorf("reading default Caddyfile: %v", err) return nil, false, fmt.Errorf("reading default Caddyfile: %v", err)
} else { } else {
// success reading default Caddyfile // success reading default Caddyfile
configFile = "Caddyfile" configFile = "Caddyfile"
@ -147,7 +151,7 @@ func loadConfig(configFile, adapterName string) ([]byte, error) {
if adapterName != "" { if adapterName != "" {
cfgAdapter = caddyconfig.GetAdapter(adapterName) cfgAdapter = caddyconfig.GetAdapter(adapterName)
if cfgAdapter == nil { if cfgAdapter == nil {
return nil, fmt.Errorf("unrecognized config adapter: %s", adapterName) return nil, false, fmt.Errorf("unrecognized config adapter: %s", adapterName)
} }
} }
@ -157,7 +161,7 @@ func loadConfig(configFile, adapterName string) ([]byte, error) {
"filename": configFile, "filename": configFile,
}) })
if err != nil { if err != nil {
return nil, fmt.Errorf("adapting config using %s: %v", adapterName, err) return nil, false, fmt.Errorf("adapting config using %s: %v", adapterName, err)
} }
for _, warn := range warnings { for _, warn := range warnings {
msg := warn.Message msg := warn.Message
@ -169,7 +173,7 @@ func loadConfig(configFile, adapterName string) ([]byte, error) {
config = adaptedConfig config = adaptedConfig
} }
return config, nil return config, configFile != "", nil
} }
// Flags wraps a FlagSet so that typed values // Flags wraps a FlagSet so that typed values

Loading…
Cancel
Save