Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cmd: remove zealous check of Caddyfile auto-detection #6370

Merged
merged 5 commits into from
Jun 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 7 additions & 11 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,6 @@ func isCaddyfile(configFile, adapterName string) (bool, error) {
baseConfig := strings.ToLower(filepath.Base(configFile))
baseConfigExt := filepath.Ext(baseConfig)
startsOrEndsInCaddyfile := strings.HasPrefix(baseConfig, "caddyfile") || strings.HasSuffix(baseConfig, ".caddyfile")
extNotCaddyfileOrJSON := (baseConfigExt != "" && baseConfigExt != ".caddyfile" && baseConfigExt != ".json")

if baseConfigExt == ".json" {
return false, nil
Expand All @@ -130,18 +129,15 @@ func isCaddyfile(configFile, adapterName string) (bool, error) {
// the config file has an extension,
// and isn't a JSON file (e.g. Caddyfile.yaml),
// then we don't know what the config format is.
if adapterName == "" && startsOrEndsInCaddyfile && extNotCaddyfileOrJSON {
return false, fmt.Errorf("ambiguous config file format; please specify adapter (use --adapter)")
}

// If the config file starts or ends with "caddyfile",
// the extension of the config file is not ".json", AND
// the user did not specify an adapter, then we assume it's Caddyfile.
if startsOrEndsInCaddyfile &&
baseConfigExt != ".json" &&
adapterName == "" {
if adapterName == "" && startsOrEndsInCaddyfile {
return true, nil
}

// adapter is not empty,
// adapter is not "caddyfile",
// extension is not ".json",
// extension is not ".caddyfile"
// file does not start with "Caddyfile"
return false, nil
}

Expand Down
27 changes: 24 additions & 3 deletions cmd/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -226,15 +226,16 @@ func Test_isCaddyfile(t *testing.T) {
wantErr: false,
},
{
name: "config is Caddyfile.yaml without adapter",
name: "config is Caddyfile.yaml with adapter",
args: args{
configFile: "./Caddyfile.yaml",
adapterName: "",
adapterName: "yaml",
},
want: false,
wantErr: true,
wantErr: false,
},
{

name: "json is not caddyfile but not error",
args: args{
configFile: "./Caddyfile.json",
Expand All @@ -243,6 +244,26 @@ func Test_isCaddyfile(t *testing.T) {
want: false,
wantErr: false,
},
{

name: "prefix of Caddyfile and ./ with any extension is Caddyfile",
args: args{
configFile: "./Caddyfile.prd",
adapterName: "",
},
want: true,
wantErr: false,
},
{

name: "prefix of Caddyfile without ./ with any extension is Caddyfile",
args: args{
configFile: "Caddyfile.prd",
adapterName: "",
},
want: true,
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand Down
Loading