writeDataToOutput writes data to stdout or a file, adding a newline if needed This is shared between ComponentResultsDisplay.WriteAll and writeVSAReport
(data []byte, formatName, file string, fs afero.Fs, cmd *cobra.Command)
| 1452 | // writeDataToOutput writes data to stdout or a file, adding a newline if needed |
| 1453 | // This is shared between ComponentResultsDisplay.WriteAll and writeVSAReport |
| 1454 | func writeDataToOutput(data []byte, formatName, file string, fs afero.Fs, cmd *cobra.Command) error { |
| 1455 | var writer io.Writer = cmd.OutOrStdout() |
| 1456 | if file != "" { |
| 1457 | f, err := fs.Create(file) |
| 1458 | if err != nil { |
| 1459 | return fmt.Errorf("failed to create output file %s: %w", file, err) |
| 1460 | } |
| 1461 | defer f.Close() |
| 1462 | writer = f |
| 1463 | } |
| 1464 | |
| 1465 | if _, err := writer.Write(data); err != nil { |
| 1466 | return fmt.Errorf("failed to write %s output: %w", formatName, err) |
| 1467 | } |
| 1468 | |
| 1469 | // Add newline if not present |
| 1470 | if !strings.HasSuffix(string(data), "\n") { |
| 1471 | if _, err := writer.Write([]byte("\n")); err != nil { |
| 1472 | return fmt.Errorf("failed to write newline: %w", err) |
| 1473 | } |
| 1474 | } |
| 1475 | |
| 1476 | return nil |
| 1477 | } |
| 1478 | |
| 1479 | // stringWriter is a helper to capture text output from WriteAll |
| 1480 | type stringWriter struct { |
no test coverage detected