Skip to content

Commit

Permalink
cmd: fix auto-detetction of .caddyfile extension (#6356)
Browse files Browse the repository at this point in the history
* 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>
  • Loading branch information
mohammed90 authored Jun 2, 2024
1 parent f8a2c60 commit 15faeac
Showing 1 changed file with 12 additions and 3 deletions.
15 changes: 12 additions & 3 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,9 +163,18 @@ func loadConfigWithLogger(logger *zap.Logger, configFile, adapterName string) ([
// caddyfile adapter for convenience
baseConfig := strings.ToLower(filepath.Base(configFile))
baseConfigExt := filepath.Ext(baseConfig)
if (strings.HasPrefix(baseConfig, "caddyfile") ||
strings.HasSuffix(baseConfig, ".caddyfile")) &&
(len(baseConfigExt) == 0 || caddyconfig.GetAdapter(baseConfigExt[1:]) == nil) &&
startsOrEndsInCaddyfile := strings.HasPrefix(baseConfig, "caddyfile") || strings.HasSuffix(baseConfig, ".caddyfile")

// If the adapter is not specified, the config file is not starts with "caddyfile", and isn't a JSON file (e.g. Caddyfile.yaml),
// then we don't know what the config format is.
if adapterName == "" && startsOrEndsInCaddyfile && baseConfigExt != ".caddyfile" && baseConfigExt != ".json" {
return nil, "", fmt.Errorf("ambiguous config file format; please specify adapter (use --adapter)")
}

// If the config file starts or ends with "caddyfile",
// the extension of the config file is not ".json", AND
// the user did not specify an adapter, then we assume it's Caddyfile.
if startsOrEndsInCaddyfile &&
baseConfigExt != ".json" &&
adapterName == "" {
adapterName = "caddyfile"
Expand Down

0 comments on commit 15faeac

Please sign in to comment.