@marcel91200 / caddy Mirror · read-only / commits / 15faeacb
Fork 0

Fast and extensible multi-platform HTTP/1-2-3 web server with automatic HTTPS

Mirrored from https://github.com/Slazee/caddy.git

Synced Nov 12, 10:45 UTC

cmd: fix auto-detetction of .caddyfile extension (#6356)

* cmd: fix auto-detetction of .caddyfile extension Signed-off-by: Mohammed Al Sahaf <msaa1990@gmail.com> * move conditions around and add clarifying comment Signed-off-by: Mohammed Al Sahaf <msaa1990@gmail.com> * reject ambiguous config file name Signed-off-by: Mohammed Al Sahaf <msaa1990@gmail.com> --------- Signed-off-by: Mohammed Al Sahaf <msaa1990@gmail.com>

Mohammed Al Sahaf committed Jun 2, 2024 at 06:49 UTC 15faeacb6065d2395a80a6a5f868149744f5de4a
1 file changed +12 -3
cmd/main.go
+12 -3
@@ -163,9 +163,18 @@ func loadConfigWithLogger(logger *zap.Logger, configFile, adapterName string) ([
163 // caddyfile adapter for convenience
164 baseConfig := strings.ToLower(filepath.Base(configFile))
165 baseConfigExt := filepath.Ext(baseConfig)
166 - if (strings.HasPrefix(baseConfig, "caddyfile") ||
167 - strings.HasSuffix(baseConfig, ".caddyfile")) &&
168 - (len(baseConfigExt) == 0 || caddyconfig.GetAdapter(baseConfigExt[1:]) == nil) &&
166 + startsOrEndsInCaddyfile := strings.HasPrefix(baseConfig, "caddyfile") || strings.HasSuffix(baseConfig, ".caddyfile")
167 +
168 + // If the adapter is not specified, the config file is not starts with "caddyfile", and isn't a JSON file (e.g. Caddyfile.yaml),
169 + // then we don't know what the config format is.
170 + if adapterName == "" && startsOrEndsInCaddyfile && baseConfigExt != ".caddyfile" && baseConfigExt != ".json" {
171 + return nil, "", fmt.Errorf("ambiguous config file format; please specify adapter (use --adapter)")
172 + }
173 +
174 + // If the config file starts or ends with "caddyfile",
175 + // the extension of the config file is not ".json", AND
176 + // the user did not specify an adapter, then we assume it's Caddyfile.
177 + if startsOrEndsInCaddyfile &&
178 baseConfigExt != ".json" &&
179 adapterName == "" {
180 adapterName = "caddyfile"