Skip to content

Commit

Permalink
feat: add --skip-hooks global flag to avoid running hooks, fixes #2129
Browse files Browse the repository at this point in the history
, fixes #6185 (#6195)
  • Loading branch information
hanoii committed Jun 14, 2024
1 parent b2a747d commit 28a5afc
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 0 deletions.
1 change: 1 addition & 0 deletions cmd/ddev/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ func Execute() {
func init() {

RootCmd.PersistentFlags().BoolVarP(&output.JSONOutput, "json-output", "j", false, "If true, user-oriented output will be in JSON format.")
RootCmd.PersistentFlags().BoolVarP(&ddevapp.SkipHooks, "skip-hooks", "", false, "If true, any hook normally run by the command will be skipped.")

output.LogSetUp()

Expand Down
8 changes: 8 additions & 0 deletions pkg/ddevapp/ddevapp.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,10 @@ type DdevApp struct {
ComposeYaml map[string]interface{} `yaml:"-"`
}

// Global variable that's set from --skip-hooks global flag.
// If true, all hooks would be skiped.
var SkipHooks = false

// GetType returns the application type as a (lowercase) string
func (app *DdevApp) GetType() string {
return strings.ToLower(app.Type)
Expand Down Expand Up @@ -1004,6 +1008,10 @@ func (app *DdevApp) ComposeFiles() ([]string, error) {

// ProcessHooks executes Tasks defined in Hooks
func (app *DdevApp) ProcessHooks(hookName string) error {
if SkipHooks {
output.UserOut.Debugf("Skipping the execution of %s hook...", hookName)
return nil
}
if cmds := app.Hooks[hookName]; len(cmds) > 0 {
output.UserOut.Debugf("Executing %s hook...", hookName)
}
Expand Down
12 changes: 12 additions & 0 deletions pkg/ddevapp/hooks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,18 @@ func TestProcessHooks(t *testing.T) {
assert.FileExists(filepath.Join(app.AppRoot, fmt.Sprintf("TestProcessHooks%s.txt", app.GetRouterHTTPSPort())))
assert.FileExists(filepath.Join(app.AppRoot, "touch_works_after_and.txt"))

// Make sure skip hooks work
ddevapp.SkipHooks = true
app.Hooks = map[string][]ddevapp.YAMLTask{
"hook-test-skip-hooks": {
{"exec": "\"echo TestProcessHooks > /var/www/html/TestProcessHooksSkipHooks${DDEV_ROUTER_HTTPS_PORT}.txt\""},
},
}
err = app.ProcessHooks("hook-test")
require.NoError(t, err)
assert.NoFileExists(filepath.Join(app.AppRoot, fmt.Sprintf("TestProcessHooksSkipHooks%s.txt", app.GetRouterHTTPSPort())))
ddevapp.SkipHooks = false

// Attempt processing hooks with a guaranteed failure
app.Hooks = map[string][]ddevapp.YAMLTask{
"hook-test": {
Expand Down

0 comments on commit 28a5afc

Please sign in to comment.