@@ -35,13 +35,21 @@ type Mapper interface {
35
35
InfoForObject (obj runtime.Object , preferredGVKs []unversioned.GroupVersionKind ) (* resource.Info , error )
36
36
}
37
37
38
+ // IgnoreErrorFunc provides a way to filter errors during the Bulk.Run. If this function returns
39
+ // true the error will NOT be added to the slice of errors returned by Bulk.Run.
40
+ //
41
+ // This may be used in conjunction with
42
+ // BulkAction.WithMessageAndPrefix if you are reporting some errors as warnings.
43
+ type IgnoreErrorFunc func (e error ) bool
44
+
38
45
// Bulk provides helpers for iterating over a list of items
39
46
type Bulk struct {
40
47
Mapper Mapper
41
48
42
- Op OpFunc
43
- After AfterFunc
44
- Retry RetryFunc
49
+ Op OpFunc
50
+ After AfterFunc
51
+ Retry RetryFunc
52
+ IgnoreError IgnoreErrorFunc
45
53
}
46
54
47
55
// Create attempts to create each item generically, gathering all errors in the
@@ -52,6 +60,10 @@ func (b *Bulk) Run(list *kapi.List, namespace string) []error {
52
60
if after == nil {
53
61
after = func (* resource.Info , error ) bool { return false }
54
62
}
63
+ ignoreError := b .IgnoreError
64
+ if ignoreError == nil {
65
+ ignoreError = func (e error ) bool { return false }
66
+ }
55
67
56
68
errs := []error {}
57
69
for i , item := range list .Items {
@@ -70,7 +82,9 @@ func (b *Bulk) Run(list *kapi.List, namespace string) []error {
70
82
}
71
83
}
72
84
if err != nil {
73
- errs = append (errs , err )
85
+ if ! ignoreError (err ) {
86
+ errs = append (errs , err )
87
+ }
74
88
if after (info , err ) {
75
89
break
76
90
}
@@ -85,22 +99,22 @@ func (b *Bulk) Run(list *kapi.List, namespace string) []error {
85
99
return errs
86
100
}
87
101
88
- func NewPrintNameOrErrorAfterIndent (mapper meta.RESTMapper , short bool , operation string , out , errs io.Writer , dryRun bool , indent string ) AfterFunc {
102
+ func NewPrintNameOrErrorAfterIndent (mapper meta.RESTMapper , short bool , operation string , out , errs io.Writer , dryRun bool , indent string , prefixForError PrefixForError ) AfterFunc {
89
103
return func (info * resource.Info , err error ) bool {
90
104
if err == nil {
91
105
fmt .Fprintf (out , indent )
92
106
cmdutil .PrintSuccess (mapper , short , out , info .Mapping .Resource , info .Name , dryRun , operation )
93
107
} else {
94
- fmt .Fprintf (errs , "%serror : %v\n " , indent , err )
108
+ fmt .Fprintf (errs , "%s%s : %v\n " , indent , prefixForError ( err ) , err )
95
109
}
96
110
return false
97
111
}
98
112
}
99
113
100
- func NewPrintErrorAfter (mapper meta.RESTMapper , errs io.Writer ) func (* resource.Info , error ) bool {
114
+ func NewPrintErrorAfter (mapper meta.RESTMapper , errs io.Writer , prefixForError PrefixForError ) func (* resource.Info , error ) bool {
101
115
return func (info * resource.Info , err error ) bool {
102
116
if err != nil {
103
- fmt .Fprintf (errs , "error : %v\n " , err )
117
+ fmt .Fprintf (errs , "%s : %v\n " , prefixForError ( err ) , err )
104
118
}
105
119
return false
106
120
}
@@ -186,24 +200,31 @@ func (b *BulkAction) DefaultIndent() string {
186
200
return ""
187
201
}
188
202
189
- func (b BulkAction ) WithMessage (action , individual string ) Runner {
203
+ // PrefixForError allows customization of the prefix that will be printed for any error that occurs in the BulkAction.
204
+ type PrefixForError func (e error ) string
205
+
206
+ func (b BulkAction ) WithMessageAndPrefix (action , individual string , prefixForError PrefixForError ) Runner {
190
207
b .Action = action
191
208
switch {
192
209
// TODO: this should be b printer
193
210
case b .Output == "" :
194
- b .Bulk .After = NewPrintNameOrErrorAfterIndent (b .Bulk .Mapper , false , individual , b .Out , b .ErrOut , b .DryRun , b .DefaultIndent ())
211
+ b .Bulk .After = NewPrintNameOrErrorAfterIndent (b .Bulk .Mapper , false , individual , b .Out , b .ErrOut , b .DryRun , b .DefaultIndent (), prefixForError )
195
212
// TODO: needs to be unified with the name printer (incremental vs exact execution), possibly by creating b synthetic printer?
196
213
case b .Output == "name" :
197
- b .Bulk .After = NewPrintNameOrErrorAfterIndent (b .Bulk .Mapper , true , individual , b .Out , b .ErrOut , b .DryRun , b .DefaultIndent ())
214
+ b .Bulk .After = NewPrintNameOrErrorAfterIndent (b .Bulk .Mapper , true , individual , b .Out , b .ErrOut , b .DryRun , b .DefaultIndent (), prefixForError )
198
215
default :
199
- b .Bulk .After = NewPrintErrorAfter (b .Bulk .Mapper , b .ErrOut )
216
+ b .Bulk .After = NewPrintErrorAfter (b .Bulk .Mapper , b .ErrOut , prefixForError )
200
217
if b .StopOnError {
201
218
b .Bulk .After = HaltOnError (b .Bulk .After )
202
219
}
203
220
}
204
221
return & b
205
222
}
206
223
224
+ func (b BulkAction ) WithMessage (action , individual string ) Runner {
225
+ return b .WithMessageAndPrefix (action , individual , func (e error ) string { return "error" })
226
+ }
227
+
207
228
func (b * BulkAction ) Run (list * kapi.List , namespace string ) []error {
208
229
run := b .Bulk
209
230
0 commit comments