@@ -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
+ 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
+
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,27 +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, 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 {
180
199
adapterName = "caddyfile"
200
+ } else if err != nil {
201
+ return nil , "" , err
181
202
}
182
203
183
204
// load config adapter
0 commit comments