@marcel91200 / caddy Mirror · read-only / commits / 243351b2
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: remove zealous check of Caddyfile auto-detection (#6370)

* cmd: remove zealous check of Caddyfile auto-detection * add test case * remove redundant check, add comment * one more case

Mohammed Al Sahaf committed Jun 5, 2024 at 17:57 UTC 243351b2b19d39790311320c4ba7e1ac1692bbe8
2 files changed +31 -14
cmd/main.go
+7 -11
@@ -119,7 +119,6 @@ func isCaddyfile(configFile, adapterName string) (bool, error) {
119 baseConfig := strings.ToLower(filepath.Base(configFile))
120 baseConfigExt := filepath.Ext(baseConfig)
121 startsOrEndsInCaddyfile := strings.HasPrefix(baseConfig, "caddyfile") || strings.HasSuffix(baseConfig, ".caddyfile")
122 - extNotCaddyfileOrJSON := (baseConfigExt != "" && baseConfigExt != ".caddyfile" && baseConfigExt != ".json")
122
123 if baseConfigExt == ".json" {
124 return false, nil
@@ -130,18 +129,15 @@ func isCaddyfile(configFile, adapterName string) (bool, error) {
129 // the config file has an extension,
130 // and isn't a JSON file (e.g. Caddyfile.yaml),
131 // then we don't know what the config format is.
133 - if adapterName == "" && startsOrEndsInCaddyfile && extNotCaddyfileOrJSON {
134 - return false, fmt.Errorf("ambiguous config file format; please specify adapter (use --adapter)")
135 - }
136 -
137 - // If the config file starts or ends with "caddyfile",
138 - // the extension of the config file is not ".json", AND
139 - // the user did not specify an adapter, then we assume it's Caddyfile.
140 - if startsOrEndsInCaddyfile &&
141 - baseConfigExt != ".json" &&
142 - adapterName == "" {
132 + if adapterName == "" && startsOrEndsInCaddyfile {
133 return true, nil
134 }
135 +
136 + // adapter is not empty,
137 + // adapter is not "caddyfile",
138 + // extension is not ".json",
139 + // extension is not ".caddyfile"
140 + // file does not start with "Caddyfile"
141 return false, nil
142 }
143
cmd/main_test.go
+24 -3
@@ -226,15 +226,16 @@ func Test_isCaddyfile(t *testing.T) {
226 wantErr: false,
227 },
228 {
229 - name: "config is Caddyfile.yaml without adapter",
229 + name: "config is Caddyfile.yaml with adapter",
230 args: args{
231 configFile: "./Caddyfile.yaml",
232 - adapterName: "",
232 + adapterName: "yaml",
233 },
234 want: false,
235 - wantErr: true,
235 + wantErr: false,
236 },
237 {
238 +
239 name: "json is not caddyfile but not error",
240 args: args{
241 configFile: "./Caddyfile.json",
@@ -243,6 +244,26 @@ func Test_isCaddyfile(t *testing.T) {
244 want: false,
245 wantErr: false,
246 },
247 + {
248 +
249 + name: "prefix of Caddyfile and ./ with any extension is Caddyfile",
250 + args: args{
251 + configFile: "./Caddyfile.prd",
252 + adapterName: "",
253 + },
254 + want: true,
255 + wantErr: false,
256 + },
257 + {
258 +
259 + name: "prefix of Caddyfile without ./ with any extension is Caddyfile",
260 + args: args{
261 + configFile: "Caddyfile.prd",
262 + adapterName: "",
263 + },
264 + want: true,
265 + wantErr: false,
266 + },
267 }
268 for _, tt := range tests {
269 t.Run(tt.name, func(t *testing.T) {