@@ -25,6 +25,7 @@ type ImportOptions struct {
25
25
Index string
26
26
ForwardToReplicas bool
27
27
ClearExistingRules bool
28
+ Wait bool
28
29
Scanner * bufio.Scanner
29
30
30
31
DoConfirm bool
@@ -102,6 +103,7 @@ func NewImportCmd(f *cmdutil.Factory, runF func(*ImportOptions) error) *cobra.Co
102
103
BoolVarP (& opts .ForwardToReplicas , "forward-to-replicas" , "f" , true , "Forward the rules to the index replicas" )
103
104
cmd .Flags ().
104
105
BoolVarP (& opts .ClearExistingRules , "clear-existing-rules" , "c" , false , "Clear existing rules before importing new ones" )
106
+ cmd .Flags ().BoolVarP (& opts .Wait , "wait" , "w" , false , "wait for the operation to complete" )
105
107
106
108
return cmd
107
109
}
@@ -147,42 +149,75 @@ func runImportCmd(opts *ImportOptions) error {
147
149
148
150
var rule search.Rule
149
151
if err := json .Unmarshal ([]byte (line ), & rule ); err != nil {
150
- err := fmt . Errorf ( "failed to parse JSON rule on line %d: %s" , count , err )
151
- return err
152
+ opts . IO . StopProgressIndicator ( )
153
+ return fmt . Errorf ( "failed to parse JSON rule on line %d: %s" , count , err )
152
154
}
153
155
154
156
rules = append (rules , rule )
155
157
count ++
156
158
157
159
// If requested, only clear existing rules the first time
158
160
if count == batchSize {
159
- _ , err := client .SaveRules (
161
+ res , err := client .SaveRules (
160
162
client .NewApiSaveRulesRequest (opts .Index , rules ).
161
163
WithClearExistingRules (clearExistingRules ).
162
164
WithForwardToReplicas (opts .ForwardToReplicas ),
163
165
)
164
166
if err != nil {
167
+ opts .IO .StopProgressIndicator ()
165
168
return err
166
169
}
167
- rules = make ([]search.Rule , 0 , batchSize )
170
+ if opts .Wait {
171
+ _ , err := client .WaitForTask (opts .Index , res .TaskID )
172
+ if err != nil {
173
+ opts .IO .StopProgressIndicator ()
174
+ return err
175
+ }
176
+ }
168
177
totalCount += count
169
178
opts .IO .UpdateProgressIndicatorLabel (fmt .Sprintf ("Imported %d rules" , totalCount ))
179
+
180
+ rules = make ([]search.Rule , 0 , batchSize )
170
181
count = 0
171
182
clearExistingRules = false
172
183
}
173
184
}
174
185
175
186
if count > 0 {
176
187
totalCount += count
177
- if _ , err := client .SaveRules (client .NewApiSaveRulesRequest (opts .Index , rules ).WithForwardToReplicas (opts .ForwardToReplicas )); err != nil {
188
+ res , err := client .SaveRules (
189
+ client .NewApiSaveRulesRequest (opts .Index , rules ).
190
+ WithForwardToReplicas (opts .ForwardToReplicas ),
191
+ )
192
+ if err != nil {
193
+ opts .IO .StopProgressIndicator ()
178
194
return err
179
195
}
196
+ if opts .Wait {
197
+ _ , err := client .WaitForTask (opts .Index , res .TaskID )
198
+ if err != nil {
199
+ opts .IO .StopProgressIndicator ()
200
+ return err
201
+ }
202
+ }
180
203
}
181
204
// Clear rules if 0 rules are imported and the clear existing is set
182
205
if totalCount == 0 && opts .ClearExistingRules {
183
- if _ , err := client .ClearRules (client .NewApiClearRulesRequest (opts .Index ).WithForwardToReplicas (opts .ForwardToReplicas )); err != nil {
206
+ res , err := client .ClearRules (
207
+ client .NewApiClearRulesRequest (opts .Index ).
208
+ WithForwardToReplicas (opts .ForwardToReplicas ),
209
+ )
210
+ if err != nil {
211
+ opts .IO .StopProgressIndicator ()
184
212
return err
185
213
}
214
+ if opts .Wait {
215
+ _ , err := client .WaitForTask (opts .Index , res .TaskID )
216
+ if err != nil {
217
+ opts .IO .StopProgressIndicator ()
218
+ return err
219
+ }
220
+ }
186
221
}
187
222
188
223
opts .IO .StopProgressIndicator ()
0 commit comments