Fast and extensible multi-platform HTTP/1-2-3 web server with automatic HTTPS
Mirrored from https://github.com/Slazee/caddy.git
Synced Nov 14, 05:15 UTC
cmd: Emit error if reload cannot find a config to load
Matthew Holt committed
Jan 22, 2020 at 10:04 UTC
6614d1c495f3266037d36b4c2f4f70fcc73acad1
2 files changed
+18
-11
cmd/commandfuncs.go
+5
-2
@@ -171,7 +171,7 @@ func cmdRun(fl Flags) (int, error) {
171
}
172
// we don't use 'else' here since this value might have been changed in 'if' block; i.e. not mutually exclusive
173
if !runCmdResumeFlag {
174
- config, err = loadConfig(runCmdConfigFlag, runCmdConfigAdapterFlag)
174
+ config, _, err = loadConfig(runCmdConfigFlag, runCmdConfigAdapterFlag)
175
if err != nil {
176
return caddy.ExitCodeFailedStartup, err
177
}
@@ -265,10 +265,13 @@ func cmdReload(fl Flags) (int, error) {
265
reloadCmdAddrFlag := fl.String("address")
266
267
// get the config in caddy's native format
268
- config, err := loadConfig(reloadCmdConfigFlag, reloadCmdConfigAdapterFlag)
268
+ config, hasConfig, err := loadConfig(reloadCmdConfigFlag, reloadCmdConfigAdapterFlag)
269
if err != nil {
270
return caddy.ExitCodeFailedStartup, err
271
}
272
+ if !hasConfig {
273
+ return caddy.ExitCodeFailedStartup, fmt.Errorf("no config file to load")
274
+ }
275
276
// get the address of the admin listener and craft endpoint URL
277
adminAddr := reloadCmdAddrFlag
cmd/main.go
+13
-9
@@ -93,12 +93,16 @@ func handlePingbackConn(conn net.Conn, expect []byte) error {
93
94
// loadConfig loads the config from configFile and adapts it
95
// using adapterName. If adapterName is specified, configFile
96
-// must be also. It prints any warnings to stderr, and returns
97
-// the resulting JSON config bytes.
98
-func loadConfig(configFile, adapterName string) ([]byte, error) {
96
+// must be also. If no configFile is specified, it tries
97
+// loading a default config file. The lack of a config file is
98
+// not treated as an error, but false will be returned if
99
+// there is no config available. It prints any warnings to stderr,
100
+// and returns the resulting JSON config bytes along with
101
+// whether a config file was loaded or not.
102
+func loadConfig(configFile, adapterName string) ([]byte, bool, error) {
103
// specifying an adapter without a config file is ambiguous
104
if adapterName != "" && configFile == "" {
101
- return nil, fmt.Errorf("cannot adapt config without config file (use --config)")
105
+ return nil, false, fmt.Errorf("cannot adapt config without config file (use --config)")
106
}
107
108
// load initial config and adapter
@@ -108,7 +112,7 @@ func loadConfig(configFile, adapterName string) ([]byte, error) {
112
if configFile != "" {
113
config, err = ioutil.ReadFile(configFile)
114
if err != nil {
111
- return nil, fmt.Errorf("reading config file: %v", err)
115
+ return nil, false, fmt.Errorf("reading config file: %v", err)
116
}
117
caddy.Log().Info("using provided configuration",
118
zap.String("config_file", configFile),
@@ -125,7 +129,7 @@ func loadConfig(configFile, adapterName string) ([]byte, error) {
129
cfgAdapter = nil
130
} else if err != nil {
131
// default Caddyfile exists, but error reading it
128
- return nil, fmt.Errorf("reading default Caddyfile: %v", err)
132
+ return nil, false, fmt.Errorf("reading default Caddyfile: %v", err)
133
} else {
134
// success reading default Caddyfile
135
configFile = "Caddyfile"
@@ -147,7 +151,7 @@ func loadConfig(configFile, adapterName string) ([]byte, error) {
151
if adapterName != "" {
152
cfgAdapter = caddyconfig.GetAdapter(adapterName)
153
if cfgAdapter == nil {
150
- return nil, fmt.Errorf("unrecognized config adapter: %s", adapterName)
154
+ return nil, false, fmt.Errorf("unrecognized config adapter: %s", adapterName)
155
}
156
}
157
@@ -157,7 +161,7 @@ func loadConfig(configFile, adapterName string) ([]byte, error) {
161
"filename": configFile,
162
})
163
if err != nil {
160
- return nil, fmt.Errorf("adapting config using %s: %v", adapterName, err)
164
+ return nil, false, fmt.Errorf("adapting config using %s: %v", adapterName, err)
165
}
166
for _, warn := range warnings {
167
msg := warn.Message
@@ -169,7 +173,7 @@ func loadConfig(configFile, adapterName string) ([]byte, error) {
173
config = adaptedConfig
174
}
175
172
- return config, nil
176
+ return config, configFile != "", nil
177
}
178
179
// Flags wraps a FlagSet so that typed values