NewOutputFormatter creates a new OutputFormatter with the given formats. The first format is the default format. At least two formats must be provided.
(formats ...OutputFormat)
| 27 | // NewOutputFormatter creates a new OutputFormatter with the given formats. The |
| 28 | // first format is the default format. At least two formats must be provided. |
| 29 | func NewOutputFormatter(formats ...OutputFormat) *OutputFormatter { |
| 30 | if len(formats) < 2 { |
| 31 | panic("at least two output formats must be provided") |
| 32 | } |
| 33 | |
| 34 | formatIDs := make(map[string]struct{}, len(formats)) |
| 35 | for _, format := range formats { |
| 36 | if format.ID() == "" { |
| 37 | panic("output format ID must not be empty") |
| 38 | } |
| 39 | if _, ok := formatIDs[format.ID()]; ok { |
| 40 | panic("duplicate format ID: " + format.ID()) |
| 41 | } |
| 42 | formatIDs[format.ID()] = struct{}{} |
| 43 | } |
| 44 | |
| 45 | return &OutputFormatter{ |
| 46 | formats: formats, |
| 47 | formatID: formats[0].ID(), |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | // AttachOptions attaches the --output flag to the given command, and any |
| 52 | // additional flags required by the output formatters. |