Skip to content

Commit 15faeac

Browse files
authored
cmd: fix auto-detetction of .caddyfile extension (#6356)
* cmd: fix auto-detetction of .caddyfile extension Signed-off-by: Mohammed Al Sahaf <[email protected]> * move conditions around and add clarifying comment Signed-off-by: Mohammed Al Sahaf <[email protected]> * reject ambiguous config file name Signed-off-by: Mohammed Al Sahaf <[email protected]> --------- Signed-off-by: Mohammed Al Sahaf <[email protected]>
1 parent f8a2c60 commit 15faeac

File tree

1 file changed

+12
-3
lines changed

1 file changed

+12
-3
lines changed

cmd/main.go

+12-3
Original file line numberDiff line numberDiff line change
@@ -163,9 +163,18 @@ func loadConfigWithLogger(logger *zap.Logger, configFile, adapterName string) ([
163163
// caddyfile adapter for convenience
164164
baseConfig := strings.ToLower(filepath.Base(configFile))
165165
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 &&
169178
baseConfigExt != ".json" &&
170179
adapterName == "" {
171180
adapterName = "caddyfile"

0 commit comments

Comments
 (0)