@@ -48,83 +48,63 @@ func (hs *hooks) AddHook(hook Hook) {
48
48
func (hs hooks ) process (
49
49
ctx context.Context , cmd Cmder , fn func (context.Context , Cmder ) error ,
50
50
) error {
51
- ctx , err := hs .beforeProcess (ctx , cmd )
52
- if err != nil {
53
- cmd .SetErr (err )
54
- return err
51
+ if len (hs .hooks ) == 0 {
52
+ return fn (ctx , cmd )
55
53
}
56
54
57
- cmdErr := fn (ctx , cmd )
55
+ var hookIndex int
56
+ var retErr error
58
57
59
- if err := hs .afterProcess (ctx , cmd ); err != nil {
60
- cmd .SetErr (err )
61
- return err
58
+ for ; hookIndex < len (hs .hooks ) && retErr == nil ; hookIndex ++ {
59
+ ctx , retErr = hs .hooks [hookIndex ].BeforeProcess (ctx , cmd )
60
+ if retErr != nil {
61
+ cmd .SetErr (retErr )
62
+ }
62
63
}
63
64
64
- return cmdErr
65
- }
66
-
67
- func (hs hooks ) beforeProcess (ctx context.Context , cmd Cmder ) (context.Context , error ) {
68
- for _ , h := range hs .hooks {
69
- var err error
70
- ctx , err = h .BeforeProcess (ctx , cmd )
71
- if err != nil {
72
- return nil , err
73
- }
65
+ if retErr == nil {
66
+ retErr = fn (ctx , cmd )
74
67
}
75
- return ctx , nil
76
- }
77
68
78
- func (hs hooks ) afterProcess (ctx context.Context , cmd Cmder ) error {
79
- var firstErr error
80
- for i := len (hs .hooks ) - 1 ; i >= 0 ; i -- {
81
- h := hs .hooks [i ]
82
- if err := h .AfterProcess (ctx , cmd ); err != nil && firstErr == nil {
83
- firstErr = err
69
+ for hookIndex -- ; hookIndex >= 0 ; hookIndex -- {
70
+ if err := hs .hooks [hookIndex ].AfterProcess (ctx , cmd ); err != nil {
71
+ retErr = err
72
+ cmd .SetErr (retErr )
84
73
}
85
74
}
86
- return firstErr
75
+
76
+ return retErr
87
77
}
88
78
89
79
func (hs hooks ) processPipeline (
90
80
ctx context.Context , cmds []Cmder , fn func (context.Context , []Cmder ) error ,
91
81
) error {
92
- ctx , err := hs .beforeProcessPipeline (ctx , cmds )
93
- if err != nil {
94
- setCmdsErr (cmds , err )
95
- return err
82
+ if len (hs .hooks ) == 0 {
83
+ return fn (ctx , cmds )
96
84
}
97
85
98
- cmdsErr := fn (ctx , cmds )
86
+ var hookIndex int
87
+ var retErr error
99
88
100
- if err := hs .afterProcessPipeline (ctx , cmds ); err != nil {
101
- setCmdsErr (cmds , err )
102
- return err
89
+ for ; hookIndex < len (hs .hooks ) && retErr == nil ; hookIndex ++ {
90
+ ctx , retErr = hs .hooks [hookIndex ].BeforeProcessPipeline (ctx , cmds )
91
+ if retErr != nil {
92
+ setCmdsErr (cmds , retErr )
93
+ }
103
94
}
104
95
105
- return cmdsErr
106
- }
107
-
108
- func (hs hooks ) beforeProcessPipeline (ctx context.Context , cmds []Cmder ) (context.Context , error ) {
109
- for _ , h := range hs .hooks {
110
- var err error
111
- ctx , err = h .BeforeProcessPipeline (ctx , cmds )
112
- if err != nil {
113
- return nil , err
114
- }
96
+ if retErr == nil {
97
+ retErr = fn (ctx , cmds )
115
98
}
116
- return ctx , nil
117
- }
118
99
119
- func (hs hooks ) afterProcessPipeline (ctx context.Context , cmds []Cmder ) error {
120
- var firstErr error
121
- for i := len (hs .hooks ) - 1 ; i >= 0 ; i -- {
122
- h := hs .hooks [i ]
123
- if err := h .AfterProcessPipeline (ctx , cmds ); err != nil && firstErr == nil {
124
- firstErr = err
100
+ for hookIndex -- ; hookIndex >= 0 ; hookIndex -- {
101
+ if err := hs .hooks [hookIndex ].AfterProcessPipeline (ctx , cmds ); err != nil {
102
+ retErr = err
103
+ setCmdsErr (cmds , retErr )
125
104
}
126
105
}
127
- return firstErr
106
+
107
+ return retErr
128
108
}
129
109
130
110
func (hs hooks ) processTxPipeline (
0 commit comments