Skip to content

Commit 7088605

Browse files
mohammed90solracsf
andauthoredJun 2, 2024··
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>
1 parent 15faeac commit 7088605

File tree

2 files changed

+130
-20
lines changed

2 files changed

+130
-20
lines changed
 

‎cmd/main.go

+41-20
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,44 @@ func LoadConfig(configFile, adapterName string) ([]byte, string, error) {
107107
return loadConfigWithLogger(caddy.Log(), configFile, adapterName)
108108
}
109109

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+
110148
func loadConfigWithLogger(logger *zap.Logger, configFile, adapterName string) ([]byte, string, error) {
111149
// if no logger is provided, use a nop logger
112150
// just so we don't have to check for nil
@@ -157,27 +195,10 @@ func loadConfigWithLogger(logger *zap.Logger, configFile, adapterName string) ([
157195
}
158196
}
159197

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 {
180199
adapterName = "caddyfile"
200+
} else if err != nil {
201+
return nil, "", err
181202
}
182203

183204
// load config adapter

‎cmd/main_test.go

+89
Original file line numberDiff line numberDiff line change
@@ -168,3 +168,92 @@ here"
168168
}
169169
}
170170
}
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+
}

0 commit comments

Comments
 (0)
Please sign in to comment.