Skip to content

Commit

Permalink
fix: do not validate config in ddev clean, delete -O, stop -U (#6209)
Browse files Browse the repository at this point in the history
  • Loading branch information
stasadev committed May 21, 2024
1 parent 17abdad commit 30884ea
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 2 deletions.
5 changes: 5 additions & 0 deletions cmd/ddev/cmd/clean.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,12 @@ Additional commands that can help clean up resources:
util.Failed("No project provided. See ddev clean --help for usage")
}

// Skip project validation
originalRunValidateConfig := ddevapp.RunValidateConfig
ddevapp.RunValidateConfig = false
projects, err := getRequestedProjects(args, cleanAll)
ddevapp.RunValidateConfig = originalRunValidateConfig

if err != nil {
util.Failed("Failed to get project(s) '%v': %v", args, err)
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/ddev/cmd/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -453,7 +453,7 @@ func TestCmdDisasterConfig(t *testing.T) {
_, err = exec.RunHostCommand(DdevBin, "delete", "-Oy", t.Name())
assert.NoError(err)
_, err = exec.RunHostCommand(DdevBin, "delete", "-Oy", t.Name()+"_subdir")
assert.Error(err)
assert.NoError(err)
_ = os.RemoveAll(tmpDir)
})

Expand Down
11 changes: 10 additions & 1 deletion cmd/ddev/cmd/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,13 @@ ddev delete --all`,
if noConfirm && deleteAll {
util.Failed("Sorry, it's not possible to use flags --all and --yes together")
}
projects, err := getRequestedProjects(args, deleteAll)

// Skip project validation if --omit-snapshot is provided
originalRunValidateConfig := ddevapp.RunValidateConfig
ddevapp.RunValidateConfig = !omitSnapshot
projects, err := getRequestedProjectsExtended(args, deleteAll, true)
ddevapp.RunValidateConfig = originalRunValidateConfig

if err != nil {
util.Failed("Failed to get project(s): %v", err)
}
Expand All @@ -42,6 +48,9 @@ ddev delete --all`,
for _, project := range projects {
if !noConfirm {
prompt := "OK to delete this project and its database?\n %s in %s\nThe code and its .ddev directory will not be touched.\n"
if project.AppRoot == "" {
prompt = "OK to delete this project and its database?\n %s in a non-existent directory %v\n"
}
if !omitSnapshot {
prompt = prompt + "A database snapshot will be made before the database is deleted.\n"
}
Expand Down
5 changes: 5 additions & 0 deletions cmd/ddev/cmd/stop.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,12 @@ ddev stop --remove-data`,
args = append(args, projectName)
}

// Skip project validation if --unlist is provided
originalRunValidateConfig := ddevapp.RunValidateConfig
ddevapp.RunValidateConfig = !unlist
projects, err := getRequestedProjects(args, stopAll)
ddevapp.RunValidateConfig = originalRunValidateConfig

if err != nil {
util.Failed("Failed to get project(s): %v", err)
}
Expand Down
9 changes: 9 additions & 0 deletions cmd/ddev/cmd/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,19 @@ package cmd

import (
"fmt"

"github.com/ddev/ddev/pkg/ddevapp"
"github.com/ddev/ddev/pkg/globalconfig"
)

// getRequestedProjects will collect and return the requested projects from command line arguments and flags.
func getRequestedProjects(names []string, all bool) ([]*ddevapp.DdevApp, error) {
return getRequestedProjectsExtended(names, all, false)
}

// getRequestedProjectsExtended will collect and return the requested projects from command line arguments and flags.
// If withNonExisting is true, it will return project stubs even if they don't exist.
func getRequestedProjectsExtended(names []string, all bool, withNonExisting bool) ([]*ddevapp.DdevApp, error) {
requestedProjects := make([]*ddevapp.DdevApp, 0)

// If no project is specified, return the current project
Expand Down Expand Up @@ -54,6 +61,8 @@ func getRequestedProjects(names []string, all bool) ([]*ddevapp.DdevApp, error)
p := globalconfig.GetProject(name)
if p != nil && p.AppRoot != "" {
requestedProjectsMap[name] = &ddevapp.DdevApp{Name: name, AppRoot: p.AppRoot}
} else if withNonExisting {
requestedProjectsMap[name] = &ddevapp.DdevApp{Name: name}
} else {
return nil, fmt.Errorf("could not find requested project '%s', you may need to use \"ddev start\" to add it to the project catalog", name)
}
Expand Down
10 changes: 10 additions & 0 deletions pkg/ddevapp/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@ import (
// Regexp pattern to determine if a hostname is valid per RFC 1123.
var hostRegex = regexp.MustCompile(`^(([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\-]*[a-zA-Z0-9])\.)*([A-Za-z0-9]|[A-Za-z0-9][A-Za-z0-9\-]*[A-Za-z0-9])$`)

// RunValidateConfig controls whether to run ValidateConfig() function.
// In some cases we don't actually need to check the config, e.g. when deleting the project.
// It is enabled by default.
var RunValidateConfig = true

// init() is for testing situations only, allowing us to override the default webserver type
// or caching behavior
func init() {
Expand Down Expand Up @@ -441,6 +446,11 @@ func ValidateProjectName(name string) error {

// ValidateConfig ensures the configuration meets ddev's requirements.
func (app *DdevApp) ValidateConfig() error {
// Skip project validation on request.
if !RunValidateConfig {
return nil
}

// Validate ddev version constraint, if any
if app.DdevVersionConstraint != "" {
constraint := app.DdevVersionConstraint
Expand Down

0 comments on commit 30884ea

Please sign in to comment.