@@ -144,76 +144,50 @@ func (o *options) sternConfig() (*stern.Config, error) {
144
144
return nil , errors .Wrap (err , "failed to compile regular expression from query" )
145
145
}
146
146
147
- var excludePod []* regexp.Regexp
148
- for _ , s := range o .excludePod {
149
- re , err := regexp .Compile (s )
150
- if err != nil {
151
- return nil , errors .Wrap (err , "failed to compile regular expression for excluded pod query" )
152
- }
153
-
154
- excludePod = append (excludePod , re )
147
+ excludePod , err := compileREs (o .excludePod )
148
+ if err != nil {
149
+ return nil , errors .Wrap (err , "failed to compile regular expression for excluded pod query" )
155
150
}
156
151
157
152
container , err := regexp .Compile (o .container )
158
153
if err != nil {
159
154
return nil , errors .Wrap (err , "failed to compile regular expression for container query" )
160
155
}
161
156
162
- var excludeContainer []* regexp.Regexp
163
- for _ , s := range o .excludeContainer {
164
- re , err := regexp .Compile (s )
165
- if err != nil {
166
- return nil , errors .Wrap (err , "failed to compile regular expression for excluded container query" )
167
- }
168
-
169
- excludeContainer = append (excludeContainer , re )
157
+ excludeContainer , err := compileREs (o .excludeContainer )
158
+ if err != nil {
159
+ return nil , errors .Wrap (err , "failed to compile regular expression for excluded container query" )
170
160
}
171
161
172
- var exclude []* regexp.Regexp
173
- for _ , ex := range o .exclude {
174
- rex , err := regexp .Compile (ex )
175
- if err != nil {
176
- return nil , errors .Wrap (err , "failed to compile regular expression for exclusion filter" )
177
- }
178
-
179
- exclude = append (exclude , rex )
162
+ exclude , err := compileREs (o .exclude )
163
+ if err != nil {
164
+ return nil , errors .Wrap (err , "failed to compile regular expression for exclusion filter" )
180
165
}
181
166
182
- var include []* regexp.Regexp
183
- for _ , inc := range o .include {
184
- rin , err := regexp .Compile (inc )
185
- if err != nil {
186
- return nil , errors .Wrap (err , "failed to compile regular expression for inclusion filter" )
187
- }
188
-
189
- include = append (include , rin )
167
+ include , err := compileREs (o .include )
168
+ if err != nil {
169
+ return nil , errors .Wrap (err , "failed to compile regular expression for inclusion filter" )
190
170
}
191
171
192
172
containerStates := []stern.ContainerState {}
193
- if o .containerStates != nil {
194
- for _ , containerStateStr := range makeUnique (o .containerStates ) {
195
- containerState , err := stern .NewContainerState (containerStateStr )
196
- if err != nil {
197
- return nil , err
198
- }
199
- containerStates = append (containerStates , containerState )
173
+ for _ , containerStateStr := range makeUnique (o .containerStates ) {
174
+ containerState , err := stern .NewContainerState (containerStateStr )
175
+ if err != nil {
176
+ return nil , err
200
177
}
178
+ containerStates = append (containerStates , containerState )
201
179
}
202
180
203
- var labelSelector labels.Selector
204
- if o .selector == "" {
205
- labelSelector = labels .Everything ()
206
- } else {
181
+ labelSelector := labels .Everything ()
182
+ if o .selector != "
E864
;" {
207
183
labelSelector , err = labels .Parse (o .selector )
208
184
if err != nil {
209
185
return nil , errors .Wrap (err , "failed to parse selector as label selector" )
210
186
}
211
187
}
212
188
213
- var fieldSelector fields.Selector
214
- if o .fieldSelector == "" {
215
- fieldSelector = fields .Everything ()
216
- } else {
189
+ fieldSelector := fields .Everything ()
190
+ if o .fieldSelector != "" {
217
191
fieldSelector , err = fields .ParseSelector (o .fieldSelector )
218
192
if err != nil {
219
193
return nil , errors .Wrap (err , "failed to parse selector as field selector" )
@@ -225,12 +199,13 @@ func (o *options) sternConfig() (*stern.Config, error) {
225
199
tailLines = & o .tail
226
200
}
227
201
228
- colorFlag := o .color
229
- if colorFlag == "always" {
202
+ switch o .color {
203
+ case "always" :
230
204
color .NoColor = false
231
- } else if colorFlag == "never" {
205
+ case "never" :
232
206
color .NoColor = true
233
- } else if colorFlag != "auto" {
207
+ case "auto" :
208
+ default :
234
209
return nil , errors .New ("color should be one of 'always', 'never', or 'auto'" )
235
210
}
236
211
@@ -239,10 +214,7 @@ func (o *options) sternConfig() (*stern.Config, error) {
239
214
return nil , err
240
215
}
241
216
242
- namespaces := []string {}
243
- if o .namespaces != nil {
244
- namespaces = makeUnique (o .namespaces )
245
- }
217
+ namespaces := makeUnique (o .namespaces )
246
218
247
219
// --timezone
248
220
location , err := time .LoadLocation (o .timezone )
@@ -464,3 +436,15 @@ func makeUnique(items []string) []string {
464
436
465
437
return result
466
438
}
439
+
440
+ func compileREs (exprs []string ) ([]* regexp.Regexp , error ) {
441
+ var regexps []* regexp.Regexp
442
+ for _ , s := range exprs {
443
+ re , err := regexp .Compile (s )
444
+ if err != nil {
445
+ return nil , err
446
+ }
447
+ regexps = append (regexps , re )
448
+ }
449
+ return regexps , nil
450
+ }
0 commit comments