@@ -48,83 +48,65 @@ 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 ); hookIndex ++ {
59
+ ctx , retErr = hs .hooks [hookIndex ].BeforeProcess (ctx , cmd )
60
+ if retErr != nil {
61
+ cmd .SetErr (retErr )
62
+ break
63
+ }
62
64
}
63
65
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
- }
66
+ if retErr == nil {
67
+ retErr = fn (ctx , cmd )
74
68
}
75
- return ctx , nil
76
- }
77
69
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
70
+ for ; hookIndex >= 0 ; hookIndex -- {
71
+ if err := hs .hooks [hookIndex ].AfterProcess (ctx , cmd ); err != nil {
72
+ retErr = err
73
+ cmd .SetErr (retErr )
84
74
}
85
75
}
86
- return firstErr
76
+
77
+ return retErr
87
78
}
88
79
89
80
func (hs hooks ) processPipeline (
90
81
ctx context.Context , cmds []Cmder , fn func (context.Context , []Cmder ) error ,
91
82
) error {
92
- ctx , err := hs .beforeProcessPipeline (ctx , cmds )
93
- if err != nil {
94
- setCmdsErr (cmds , err )
95
- return err
83
+ if len (hs .hooks ) == 0 {
84
+ return fn (ctx , cmds )
96
85
}
97
86
98
- cmdsErr := fn (ctx , cmds )
87
+ var hookIndex int
88
+ var retErr error
99
89
100
- if err := hs .afterProcessPipeline (ctx , cmds ); err != nil {
101
- setCmdsErr (cmds , err )
102
- return err
90
+ for ; hookIndex < len (hs .hooks ); hookIndex ++ {
91
+ ctx , retErr = hs .hooks [hookIndex ].BeforeProcessPipeline (ctx , cmds )
92
+ if retErr != nil {
93
+ setCmdsErr (cmds , retErr )
94
+ break
95
+ }
103
96
}
104
97
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
- }
98
+ if retErr == nil {
99
+ retErr = fn (ctx , cmds )
115
100
}
116
- return ctx , nil
117
- }
118
101
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
102
+ for ; hookIndex >= 0 ; hookIndex -- {
103
+ if err := hs .hooks [hookIndex ].AfterProcessPipeline (ctx , cmds ); err != nil {
104
+ retErr = err
105
+ setCmdsErr (cmds , retErr )
125
106
}
126
107
}
127
- return firstErr
108
+
109
+ return retErr
128
110
}
129
111
130
112
func (hs hooks ) processTxPipeline (
0 commit comments