Print prints formatted lists in different formats
(toJSON any, format string, outWriter io.Writer, writerFn func(w io.Writer), headers ...string)
| 27 | |
| 28 | // Print prints formatted lists in different formats |
| 29 | func Print(toJSON any, format string, outWriter io.Writer, writerFn func(w io.Writer), headers ...string) error { |
| 30 | switch strings.ToLower(format) { |
| 31 | case TABLE, PRETTY, "": |
| 32 | return PrintPrettySection(outWriter, writerFn, headers...) |
| 33 | case TemplateLegacyJSON: |
| 34 | switch reflect.TypeOf(toJSON).Kind() { |
| 35 | case reflect.Slice: |
| 36 | s := reflect.ValueOf(toJSON) |
| 37 | for i := 0; i < s.Len(); i++ { |
| 38 | obj := s.Index(i).Interface() |
| 39 | outJSON, err := ToJSON(obj, "", "") |
| 40 | if err != nil { |
| 41 | return err |
| 42 | } |
| 43 | _, _ = fmt.Fprint(outWriter, outJSON) |
| 44 | } |
| 45 | default: |
| 46 | outJSON, err := ToStandardJSON(toJSON) |
| 47 | if err != nil { |
| 48 | return err |
| 49 | } |
| 50 | _, _ = fmt.Fprintln(outWriter, outJSON) |
| 51 | } |
| 52 | case JSON: |
| 53 | switch reflect.TypeOf(toJSON).Kind() { |
| 54 | case reflect.Slice: |
| 55 | outJSON, err := ToJSON(toJSON, "", "") |
| 56 | if err != nil { |
| 57 | return err |
| 58 | } |
| 59 | _, _ = fmt.Fprint(outWriter, outJSON) |
| 60 | default: |
| 61 | outJSON, err := ToStandardJSON(toJSON) |
| 62 | if err != nil { |
| 63 | return err |
| 64 | } |
| 65 | _, _ = fmt.Fprintln(outWriter, outJSON) |
| 66 | } |
| 67 | default: |
| 68 | return fmt.Errorf("format value %q could not be parsed: %w", format, api.ErrParsingFailed) |
| 69 | } |
| 70 | return nil |
| 71 | } |