| 1 |
package tablewriter |
| 2 |
|
| 3 |
import ( |
| 4 |
"encoding/csv" |
| 5 |
"io" |
| 6 |
"os" |
| 7 |
) |
| 8 |
|
| 9 |
// NewCSV Start A new table by importing from a CSV file |
| 10 |
// Takes io.Writer and csv File name |
| 11 |
func NewCSV(writer io.Writer, fileName string, hasHeader bool, opts ...Option) (*Table, error) { |
| 12 |
// Open the CSV file |
| 13 |
file, err := os.Open(fileName) |
| 14 |
if err != nil { |
| 15 |
// Log implicitly handled by NewTable if logger is configured via opts |
| 16 |
return nil, err // Return nil *Table on error |
| 17 |
} |
| 18 |
defer file.Close() // Ensure file is closed |
| 19 |
|
| 20 |
// Create a CSV reader |
| 21 |
csvReader := csv.NewReader(file) |
| 22 |
|
| 23 |
// Delegate to NewCSVReader, passing through the options |
| 24 |
return NewCSVReader(writer, csvReader, hasHeader, opts...) |
| 25 |
} |
| 26 |
|
| 27 |
// NewCSVReader Start a New Table Writer with csv.Reader |
| 28 |
// This enables customisation such as reader.Comma = ';' |
| 29 |
// See http://golang.org/src/pkg/encoding/csv/reader.go?s=3213:3671#L94 |
| 30 |
func NewCSVReader(writer io.Writer, csvReader *csv.Reader, hasHeader bool, opts ...Option) (*Table, error) { |
| 31 |
// Create a new table instance using the modern API and provided options. |
| 32 |
// Options configure the table's appearance and behavior (renderer, borders, etc.). |
| 33 |
t := NewTable(writer, opts...) // Logger setup happens here if WithLogger/WithDebug is passed |
| 34 |
|
| 35 |
// Process header row if specified |
| 36 |
if hasHeader { |
| 37 |
headers, err := csvReader.Read() |
| 38 |
if err != nil { |
| 39 |
// Handle EOF specifically: means the CSV was empty or contained only an empty header line. |
| 40 |
if err == io.EOF { |
| 41 |
t.logger.Debug("NewCSVReader: CSV empty or only header found (EOF after header read attempt).") |
| 42 |
// Return the table configured by opts, but without data/header. |
| 43 |
// It's ready for Render() which will likely output nothing or just borders if configured. |
| 44 |
return t, nil |
| 45 |
} |
| 46 |
// Log other read errors |
| 47 |
t.logger.Errorf("NewCSVReader: Error reading CSV header: %v", err) |
| 48 |
return nil, err // Return nil *Table on critical read error |
| 49 |
} |
| 50 |
|
| 51 |
// Check if the read header is genuinely empty (e.g., a blank line in the CSV) |
| 52 |
isEmptyHeader := true |
| 53 |
for _, h := range headers { |
| 54 |
if h != "" { |
| 55 |
isEmptyHeader = false |
| 56 |
break |
| 57 |
} |
| 58 |
} |
| 59 |
|
| 60 |
if !isEmptyHeader { |
| 61 |
t.Header(headers) // Use the Table method to set the header data |
| 62 |
t.logger.Debugf("NewCSVReader: Header set from CSV: %v", headers) |
| 63 |
} else { |
| 64 |
t.logger.Debug("NewCSVReader: Read an empty header line, skipping setting table header.") |
| 65 |
} |
| 66 |
} |
| 67 |
|
| 68 |
// Process data rows |
| 69 |
rowCount := 0 |
| 70 |
for { |
| 71 |
record, err := csvReader.Read() |
| 72 |
if err == io.EOF { |
| 73 |
break // Reached the end of the CSV data |
| 74 |
} |
| 75 |
if err != nil { |
| 76 |
// Log other read errors during data processing |
| 77 |
t.logger.Errorf("NewCSVReader: Error reading CSV record: %v", err) |
| 78 |
return nil, err // Return nil *Table on critical read error |
| 79 |
} |
| 80 |
|
| 81 |
// Append the record to the table's internal buffer (for batch rendering). |
| 82 |
// The Table.Append method handles conversion and storage. |
| 83 |
if appendErr := t.Append(record); appendErr != nil { |
| 84 |
t.logger.Errorf("NewCSVReader: Error appending record #%d: %v", rowCount+1, appendErr) |
| 85 |
// Decide if append error is fatal. For now, let's treat it as fatal. |
| 86 |
return nil, appendErr |
| 87 |
} |
| 88 |
rowCount++ |
| 89 |
} |
| 90 |
t.logger.Debugf("NewCSVReader: Finished reading CSV. Appended %d data rows.", rowCount) |
| 91 |
|
| 92 |
// Return the configured and populated table instance, ready for Render() call. |
| 93 |
return t, nil |
| 94 |
} |