MCPcopy
hub / github.com/caddyserver/caddy / cmdRun

Function cmdRun

cmd/commandfuncs.go:188–354  ·  view source on GitHub ↗
(fl Flags)

Source from the content-addressed store, hash-verified

186}
187
188func cmdRun(fl Flags) (int, error) {
189 caddy.TrapSignals()
190
191 // set up buffered logging for early startup
192 // so that we can hold onto logs until after
193 // the config is loaded (or fails to load)
194 // so that we can write the logs to the user's
195 // configured output. we must be sure to flush
196 // on any error before the config is loaded.
197 logger, defaultLogger, logBuffer := caddy.BufferedLog()
198
199 undoMaxProcs := setResourceLimits(logger)
200 defer undoMaxProcs()
201 // release the local reference to the undo function so it can be GC'd;
202 // the deferred call above has already captured the actual function value.
203 undoMaxProcs = nil //nolint:ineffassign,wastedassign
204
205 configFlag := fl.String("config")
206 configAdapterFlag := fl.String("adapter")
207 resumeFlag := fl.Bool("resume")
208 printEnvFlag := fl.Bool("environ")
209 watchFlag := fl.Bool("watch")
210 pidfileFlag := fl.String("pidfile")
211 pingbackFlag := fl.String("pingback")
212
213 // load all additional envs as soon as possible
214 err := handleEnvFileFlag(fl)
215 if err != nil {
216 logBuffer.FlushTo(defaultLogger)
217 return caddy.ExitCodeFailedStartup, err
218 }
219
220 // if we are supposed to print the environment, do that first
221 if printEnvFlag {
222 printEnvironment()
223 }
224
225 // load the config, depending on flags
226 var config []byte
227 if resumeFlag {
228 config, err = os.ReadFile(caddy.ConfigAutosavePath)
229 if errors.Is(err, fs.ErrNotExist) {
230 // not a bad error; just can't resume if autosave file doesn't exist
231 logger.Info("no autosave file exists", zap.String("autosave_file", caddy.ConfigAutosavePath))
232 resumeFlag = false
233 } else if err != nil {
234 logBuffer.FlushTo(defaultLogger)
235 return caddy.ExitCodeFailedStartup, err
236 } else {
237 if configFlag == "" {
238 logger.Info("resuming from last configuration",
239 zap.String("autosave_file", caddy.ConfigAutosavePath))
240 } else {
241 // if they also specified a config file, user should be aware that we're not
242 // using it (doing so could lead to data/config loss by overwriting!)
243 logger.Warn("--config and --resume flags were used together; ignoring --config and resuming from last configuration",
244 zap.String("autosave_file", caddy.ConfigAutosavePath))
245 }

Callers

nothing calls this directly

Calls 13

setResourceLimitsFunction · 0.85
handleEnvFileFlagFunction · 0.85
printEnvironmentFunction · 0.85
LoadConfigFunction · 0.85
watchConfigFileFunction · 0.85
ReadFileMethod · 0.80
LoadMethod · 0.80
FlushToMethod · 0.65
StringMethod · 0.45
BoolMethod · 0.45
ErrorMethod · 0.45
WriteMethod · 0.45

Tested by

no test coverage detected