| 51 | } |
| 52 | |
| 53 | func listBundle[T any]( |
| 54 | jsonValue any, |
| 55 | items []T, |
| 56 | humanTitle string, |
| 57 | humanHeaders []string, |
| 58 | toonName string, |
| 59 | toonFields []string, |
| 60 | humanRow func(T) []string, |
| 61 | toonRow func(T) []string, |
| 62 | ) outputBundle { |
| 63 | return outputBundle{ |
| 64 | jsonValue: jsonValue, |
| 65 | jsonl: func(cmd *cobra.Command) error { |
| 66 | return writeJSONLines(cmd, items) |
| 67 | }, |
| 68 | human: func() (string, error) { |
| 69 | if humanRow == nil { |
| 70 | return "", errors.New("cli: human list row renderer is required") |
| 71 | } |
| 72 | rows := make([][]string, 0, len(items)) |
| 73 | for _, item := range items { |
| 74 | rows = append(rows, humanRow(item)) |
| 75 | } |
| 76 | return renderHumanTable(humanTitle, humanHeaders, rows), nil |
| 77 | }, |
| 78 | toon: func() (string, error) { |
| 79 | if toonRow == nil { |
| 80 | return "", errors.New("cli: toon list row renderer is required") |
| 81 | } |
| 82 | rows := make([][]string, 0, len(items)) |
| 83 | for _, item := range items { |
| 84 | rows = append(rows, toonRow(item)) |
| 85 | } |
| 86 | return renderToonArray(toonName, toonFields, rows), nil |
| 87 | }, |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | type keyValue struct { |
| 92 | Label string |