@@ -107,6 +107,44 @@ func LoadConfig(configFile, adapterName string) ([]byte, string, error) {
107
107
return loadConfigWithLogger (caddy .Log (), configFile , adapterName )
108
108
}
109
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
+ notCaddyfileNorJSON := (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 does not 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 && notCaddyfileNorJSON {
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
+
110
148
func loadConfigWithLogger (logger * zap.Logger , configFile , adapterName string ) ([]byte , string , error ) {
111
149
// if no logger is provided, use a nop logger
112
150
// just so we don't have to check for nil
@@ -157,30 +195,10 @@ func loadConfigWithLogger(logger *zap.Logger, configFile, adapterName string) ([
157
195
}
158
196
}
159
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,
169
- // the config file does not starts with "caddyfile",
170
- // the config file has an extension,
171
- // and isn't a JSON file (e.g. Caddyfile.yaml),
172
- // then we don't know what the config format is.
173
- if adapterName == "" && startsOrEndsInCaddyfile && (baseConfigExt != "" && baseConfigExt != ".caddyfile" && baseConfigExt != ".json" ) {
174
- return nil , "" , fmt .Errorf ("ambiguous config file format; please specify adapter (use --adapter)" )
175
- }
176
-
177
- // If the config file starts or ends with "caddyfile",
178
- // the extension of the config file is not ".json", AND
179
- // the user did not specify an adapter, then we assume it's Caddyfile.
180
- if startsOrEndsInCaddyfile &&
181
- baseConfigExt != ".json" &&
182
- adapterName == "" {
198
+ if yes , err := isCaddyfile (configFile , adapterName ); yes {
183
199
adapterName = "caddyfile"
200
+ } else if err != nil {
201
+ return nil , "" , err
184
202
}
185
203
186
204
// load config adapter
0 commit comments