diff options
author | Mohammed Al Sahaf <[email protected]> | 2024-06-02 06:49:38 +0300 |
---|---|---|
committer | GitHub <[email protected]> | 2024-06-02 03:49:38 +0000 |
commit | 15faeacb6065d2395a80a6a5f868149744f5de4a (patch) | |
tree | eeccac13f3c62138dc79043663128f30e1bebec2 | |
parent | f8a2c602971c2a85747b8eda7e01a40b585b3149 (diff) | |
download | caddy-2.8.3.tar.gz caddy-2.8.3.zip |
* 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]>
-rw-r--r-- | cmd/main.go | 15 |
1 files changed, 12 insertions, 3 deletions
diff --git a/cmd/main.go b/cmd/main.go index 31a121aa6..1f7d6156b 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -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" |