@@ -234,74 +234,9 @@ func (o *options) sternConfig() (*stern.Config, error) {
234
234
return nil , errors .New ("color should be one of 'always', 'never', or 'auto'" )
235
235
}
236
236
237
- t := o .template
238
- if t == "" {
239
- switch o .output {
240
- case "default" :
241
- if color .NoColor {
242
- t = "{{.PodName}} {{.ContainerName}} {{.Message}}"
243
- if o .allNamespaces || len (o .namespaces ) > 1 {
244
- t = fmt .Sprintf ("{{.Namespace}} %s" , t )
245
- }
246
- } else {
247
- t = "{{color .PodColor .PodName}} {{color .ContainerColor .ContainerName}} {{.Message}}"
248
- if o .allNamespaces || len (o .namespaces ) > 1 {
249
- t = fmt .Sprintf ("{{color .PodColor .Namespace}} %s" , t )
250
- }
251
-
252
- }
253
- case "raw" :
254
- t = "{{.Message}}"
255
- case "json" :
256
- t = "{{json .}}"
257
- case "extjson" :
258
- t = "\" pod\" : \" {{color .PodColor .PodName}}\" , \" container\" : \" {{color .ContainerColor .ContainerName}}\" , \" message\" : {{extjson .Message}}"
259
- if o .allNamespaces {
260
- t = fmt .Sprintf ("\" namespace\" : \" {{color .PodColor .Namespace}}\" , %s" , t )
261
- }
262
- t = fmt .Sprintf ("{%s}" , t )
263
- case "ppextjson" :
264
- t = " \" pod\" : \" {{color .PodColor .PodName}}\" ,\n \" container\" : \" {{color .ContainerColor .ContainerName}}\" ,\n \" message\" : {{extjson .Message}}"
265
- if o .allNamespaces {
266
- t = fmt .Sprintf (" \" namespace\" : \" {{color .PodColor .Namespace}}\" ,\n %s" , t )
267
- }
268
- t = fmt .Sprintf ("{\n %s\n }" , t )
269
- }
270
- t += "\n "
271
- }
272
-
273
- funs := map [string ]interface {}{
274
- "json" : func (in interface {}) (string , error ) {
275
- b , err := json .Marshal (in )
276
- if err != nil {
277
- return "" , err
278
- }
279
- return string (b ), nil
280
- },
281
- "parseJSON" : func (text string ) (map [string ]interface {}, error ) {
282
- obj := make (map [string ]interface {})
283
- if err := json .Unmarshal ([]byte (text ), & obj ); err != nil {
284
- return obj , err
285
- }
286
- return obj , nil
287
- },
288
- "extjson" : func (in string ) (string , error ) {
289
- if json .Valid ([]byte (in )) {
290
- return strings .TrimSuffix (in , "\n " ), nil
291
- }
292
- b , err := json .Marshal (in )
293
- if err != nil {
294
- return "" , err
295
- }
296
- return strings .TrimSuffix (string (b ), "\n " ), nil
297
- },
298
- "color" : func (color color.Color , text string ) string {
299
- return color .SprintFunc ()(text )
300
- },
301
- }
302
- template , err := template .New ("log" ).Funcs (funs ).Parse (t )
237
+ template , err := o .generateTemplate ()
303
238
if err != nil {
304
- return nil , errors . Wrap ( err , "unable to parse template" )
239
+ return nil , err
305
240
}
306
241
307
242
namespaces := []string {}
@@ -402,6 +337,71 @@ func (o *options) AddFlags(fs *pflag.FlagSet) {
402
337
fs .BoolVarP (& o .version , "version" , "v" , o .version , "Print the version and exit." )
403
338
}
404
339
340
+ func (o * options ) generateTemplate () (* template.Template , error ) {
341
+ t := o .template
342
+ if t == "" {
343
+ switch o .output {
344
+ case "default" :
345
+ t = "{{color .PodColor .PodName}} {{color .ContainerColor .ContainerName}} {{.Message}}"
346
+ if o .allNamespaces || len (o .namespaces ) > 1 {
347
+ t = fmt .Sprintf ("{{color .PodColor .Namespace}} %s" , t )
348
+ }
349
+ case "raw" :
350
+ t = "{{.Message}}"
351
+ case "json" :
352
+ t = "{{json .}}"
353
+ case "extjson" :
354
+ t = "\" pod\" : \" {{color .PodColor .PodName}}\" , \" container\" : \" {{color .ContainerColor .ContainerName}}\" , \" message\" : {{extjson .Message}}"
355
+ if o .allNamespaces {
356
+ t = fmt .Sprintf ("\" namespace\" : \" {{color .PodColor .Namespace}}\" , %s" , t )
357
+ }
358
+ t = fmt .Sprintf ("{%s}" , t )
359
+ case "ppextjson" :
360
+ t = " \" pod\" : \" {{color .PodColor .PodName}}\" ,\n \" container\" : \" {{color .ContainerColor .ContainerName}}\" ,\n \" message\" : {{extjson .Message}}"
361
+ if o .allNamespaces {
362
+ t = fmt .Sprintf (" \" namespace\" : \" {{color .PodColor .Namespace}}\" ,\n %s" , t )
363
+ }
364
+ t = fmt .Sprintf ("{\n %s\n }" , t )
365
+ }
366
+ t += "\n "
367
+ }
368
+
369
+ funs := map [string ]interface {}{
370
+ "json" : func (in interface {}) (string , error ) {
371
+ b , err := json .Marshal (in )
372
+ if err != nil {
373
+ return "" , err
374
+ }
375
+ return string (b ), nil
376
+ },
377
+ "parseJSON" : func (text string ) (map [string ]interface {}, error ) {
378
+ obj := make (map [string ]interface {})
379
+ if err := json .Unmarshal ([]byte (text ), & obj ); err != nil {
380
+ return obj , err
381
+ }
382
+ return obj , nil
383
+ },
384
+ "extjson" : func (in string ) (string , error ) {
385
+ if json .Valid ([]byte (in )) {
386
+ return strings .TrimSuffix (in , "\n " ), nil
387
+ }
388
+ b , err := json .Marshal (in )
389
+ if err != nil {
390
+ return "" , err
391
+ }
392
+ return strings .TrimSuffix (string (b ), "\n " ), nil
393
+ },
394
+ "color" : func (color color.Color , text string ) string {
395
+ return color .SprintFunc ()(text )
396
+ },
397
+ }
398
+ template , err := template .New ("log" ).Funcs (funs ).Parse (t )
399
+ if err != nil {
400
+ return nil , errors .Wrap (err , "unable to parse template" )
401
+ }
402
+ return template , err
403
+ }
404
+
405
405
func NewSternCmd (stream genericclioptions.IOStreams ) (* cobra.Command , error ) {
406
406
o := NewOptions (stream )
407
407
0 commit comments