@@ -7,6 +7,7 @@ package logic
77
88import (
99 "fmt"
10+ "io"
1011 "os"
1112 "os/exec"
1213 "path/filepath"
@@ -42,10 +43,6 @@ func NewHooksExecutor(migrationContext *base.MigrationContext) *HooksExecutor {
4243 }
4344}
4445
45- func (this * HooksExecutor ) initHooks () error {
46- return nil
47- }
48-
4946func (this * HooksExecutor ) applyEnvironmentVariables (extraVariables ... string ) []string {
5047 env := os .Environ ()
5148 env = append (env , fmt .Sprintf ("GH_OST_DATABASE_NAME=%s" , this .migrationContext .DatabaseName ))
@@ -76,13 +73,13 @@ func (this *HooksExecutor) applyEnvironmentVariables(extraVariables ...string) [
7673}
7774
7875// executeHook executes a command, and sets relevant environment variables
79- // combined output & error are printed to gh-ost's standard error .
80- func (this * HooksExecutor ) executeHook (hook string , extraVariables ... string ) error {
76+ // combined output & error are printed to the provided io.Writer .
77+ func (this * HooksExecutor ) executeHook (out io. Writer , hook string , extraVariables ... string ) error {
8178 cmd := exec .Command (hook )
8279 cmd .Env = this .applyEnvironmentVariables (extraVariables ... )
8380
8481 combinedOutput , err := cmd .CombinedOutput ()
85- fmt .Fprintln (os . Stderr , string (combinedOutput ))
82+ fmt .Fprintln (out , string (combinedOutput ))
8683 return log .Errore (err )
8784}
8885
@@ -95,69 +92,69 @@ func (this *HooksExecutor) detectHooks(baseName string) (hooks []string, err err
9592 return hooks , err
9693}
9794
98- func (this * HooksExecutor ) executeHooks (baseName string , extraVariables ... string ) error {
95+ func (this * HooksExecutor ) executeHooks (out io. Writer , baseName string , extraVariables ... string ) error {
9996 hooks , err := this .detectHooks (baseName )
10097 if err != nil {
10198 return err
10299 }
103100 for _ , hook := range hooks {
104101 log .Infof ("executing %+v hook: %+v" , baseName , hook )
105- if err := this .executeHook (hook , extraVariables ... ); err != nil {
102+ if err := this .executeHook (out , hook , extraVariables ... ); err != nil {
106103 return err
107104 }
108105 }
109106 return nil
110107}
111108
112109func (this * HooksExecutor ) onStartup () error {
113- return this .executeHooks (onStartup )
110+ return this .executeHooks (os . Stderr , onStartup )
114111}
115112
116113func (this * HooksExecutor ) onValidated () error {
117- return this .executeHooks (onValidated )
114+ return this .executeHooks (os . Stderr , onValidated )
118115}
119116
120117func (this * HooksExecutor ) onRowCountComplete () error {
121- return this .executeHooks (onRowCountComplete )
118+ return this .executeHooks (os . Stderr , onRowCountComplete )
122119}
123120func (this * HooksExecutor ) onBeforeRowCopy () error {
124- return this .executeHooks (onBeforeRowCopy )
121+ return this .executeHooks (os . Stderr , onBeforeRowCopy )
125122}
126123
127124func (this * HooksExecutor ) onRowCopyComplete () error {
128- return this .executeHooks (onRowCopyComplete )
125+ return this .executeHooks (os . Stderr , onRowCopyComplete )
129126}
130127
131128func (this * HooksExecutor ) onBeginPostponed () error {
132- return this .executeHooks (onBeginPostponed )
129+ return this .executeHooks (os . Stderr , onBeginPostponed )
133130}
134131
135132func (this * HooksExecutor ) onBeforeCutOver () error {
136- return this .executeHooks (onBeforeCutOver )
133+ return this .executeHooks (os . Stderr , onBeforeCutOver )
137134}
138135
139136func (this * HooksExecutor ) onInteractiveCommand (command string ) error {
140137 v := fmt .Sprintf ("GH_OST_COMMAND='%s'" , command )
141- return this .executeHooks (onInteractiveCommand , v )
138+ return this .executeHooks (os . Stderr , onInteractiveCommand , v )
142139}
143140
144141func (this * HooksExecutor ) onSuccess () error {
145- return this .executeHooks (onSuccess )
142+ return this .executeHooks (os . Stderr , onSuccess )
146143}
147144
148145func (this * HooksExecutor ) onFailure () error {
149- return this .executeHooks (onFailure )
146+ return this .executeHooks (os . Stderr , onFailure )
150147}
151148
152149func (this * HooksExecutor ) onStatus (statusMessage string ) error {
153150 v := fmt .Sprintf ("GH_OST_STATUS='%s'" , statusMessage )
154- return this .executeHooks (onStatus , v )
151+ return this .executeHooks (os . Stderr , onStatus , v )
155152}
156153
157154func (this * HooksExecutor ) onStopReplication () error {
158- return this .executeHooks (onStopReplication )
155+ return this .executeHooks (os . Stderr , onStopReplication )
159156}
160157
161158func (this * HooksExecutor ) onStartReplication () error {
162- return this .executeHooks (onStartReplication )
159+ return this .executeHooks (os . Stderr , onStartReplication )
163160}
0 commit comments