@marcel91200 / caddy Mirror · read-only / commits / 7088605c
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 regression in auto-detect of Caddyfile (#6362)

* cmd: fix regression in auto-detect of Caddyfile Signed-off-by: Mohammed Al Sahaf <msaa1990@gmail.com> * fix typo Co-authored-by: Git'Fellow <12234510+solracsf@users.noreply.github.com> * add tests * address review comments --------- Signed-off-by: Mohammed Al Sahaf <msaa1990@gmail.com> Co-authored-by: Git'Fellow <12234510+solracsf@users.noreply.github.com>

Mohammed Al Sahaf committed Jun 2, 2024 at 14:40 UTC 7088605cc11c52c2777ab613dfc5c2a9816006e4
2 files changed +130 -20
cmd/main.go
+41 -20
@@ -107,6 +107,44 @@ func LoadConfig(configFile, adapterName string) ([]byte, string, error) {
107 return loadConfigWithLogger(caddy.Log(), configFile, adapterName)
108 }
109
110 +func isCaddyfile(configFile, adapterName string) (bool, error) {
111 + if adapterName == "caddyfile" {
112 + return true, nil
113 + }
114 +
115 + // as a special case, if a config file starts with "caddyfile" or
116 + // has a ".caddyfile" extension, and no adapter is specified, and
117 + // no adapter module name matches the extension, assume
118 + // caddyfile adapter for convenience
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")
123 +
124 + if baseConfigExt == ".json" {
125 + return false, nil
126 + }
127 +
128 + // If the adapter is not specified,
129 + // the config file starts with "caddyfile",
130 + // the config file has an extension,
131 + // and isn't a JSON file (e.g. Caddyfile.yaml),
132 + // 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 == "" {
143 + return true, nil
144 + }
145 + return false, nil
146 +}
147 +
148 func loadConfigWithLogger(logger *zap.Logger, configFile, adapterName string) ([]byte, string, error) {
149 // if no logger is provided, use a nop logger
150 // just so we don't have to check for nil
@@ -157,27 +195,10 @@ func loadConfigWithLogger(logger *zap.Logger, configFile, adapterName string) ([
195 }
196 }
197
160 - // as a special case, if a config file starts with "caddyfile" or
161 - // has a ".caddyfile" extension, and no adapter is specified, and
162 - // no adapter module name matches the extension, assume
163 - // caddyfile adapter for convenience
164 - baseConfig := strings.ToLower(filepath.Base(configFile))
165 - baseConfigExt := filepath.Ext(baseConfig)
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 == "" {
198 + if yes, err := isCaddyfile(configFile, adapterName); yes {
199 adapterName = "caddyfile"
200 + } else if err != nil {
201 + return nil, "", err
202 }
203
204 // load config adapter
cmd/main_test.go
+89
@@ -168,3 +168,92 @@ here"
168 }
169 }
170 }
171 +
172 +func Test_isCaddyfile(t *testing.T) {
173 + type args struct {
174 + configFile string
175 + adapterName string
176 + }
177 + tests := []struct {
178 + name string
179 + args args
180 + want bool
181 + wantErr bool
182 + }{
183 + {
184 + name: "bare Caddyfile without adapter",
185 + args: args{
186 + configFile: "Caddyfile",
187 + adapterName: "",
188 + },
189 + want: true,
190 + wantErr: false,
191 + },
192 + {
193 + name: "local Caddyfile without adapter",
194 + args: args{
195 + configFile: "./Caddyfile",
196 + adapterName: "",
197 + },
198 + want: true,
199 + wantErr: false,
200 + },
201 + {
202 + name: "local caddyfile with adapter",
203 + args: args{
204 + configFile: "./Caddyfile",
205 + adapterName: "caddyfile",
206 + },
207 + want: true,
208 + wantErr: false,
209 + },
210 + {
211 + name: "ends with .caddyfile with adapter",
212 + args: args{
213 + configFile: "./conf.caddyfile",
214 + adapterName: "caddyfile",
215 + },
216 + want: true,
217 + wantErr: false,
218 + },
219 + {
220 + name: "ends with .caddyfile without adapter",
221 + args: args{
222 + configFile: "./conf.caddyfile",
223 + adapterName: "",
224 + },
225 + want: true,
226 + wantErr: false,
227 + },
228 + {
229 + name: "config is Caddyfile.yaml without adapter",
230 + args: args{
231 + configFile: "./Caddyfile.yaml",
232 + adapterName: "",
233 + },
234 + want: false,
235 + wantErr: true,
236 + },
237 + {
238 + name: "json is not caddyfile but not error",
239 + args: args{
240 + configFile: "./Caddyfile.json",
241 + adapterName: "",
242 + },
243 + want: false,
244 + wantErr: false,
245 + },
246 + }
247 + for _, tt := range tests {
248 + t.Run(tt.name, func(t *testing.T) {
249 + got, err := isCaddyfile(tt.args.configFile, tt.args.adapterName)
250 + if (err != nil) != tt.wantErr {
251 + t.Errorf("isCaddyfile() error = %v, wantErr %v", err, tt.wantErr)
252 + return
253 + }
254 + if got != tt.want {
255 + t.Errorf("isCaddyfile() = %v, want %v", got, tt.want)
256 + }
257 + })
258 + }
259 +}