Skip to content

Commit

Permalink
update tsv code
Browse files Browse the repository at this point in the history
  • Loading branch information
Cerebrovinny committed Feb 9, 2025
1 parent c0d58e0 commit 7d23cf2
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 6 deletions.
41 changes: 36 additions & 5 deletions pkg/list/list_stacks.go
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ func FilterAndListStacks(stacksMap map[string]any, component string, listConfig
}
return string(jsonBytes) + utils.GetLineEnding(), nil

case FormatCSV:
case FormatCSV, FormatTSV:
// Only include columns that have values
var nonEmptyHeaders []string
var nonEmptyColumnIndexes []int
Expand All @@ -280,15 +280,46 @@ func FilterAndListStacks(stacksMap map[string]any, component string, listConfig
}
}

// Set appropriate delimiter based on format
fileDelimiter := delimiter
if delimiter == "\t" || delimiter == "" {
switch format {
case FormatCSV:
fileDelimiter = ","
case FormatTSV:
fileDelimiter = "\t"
}
}

var output strings.Builder
output.WriteString(strings.Join(nonEmptyHeaders, delimiter) + utils.GetLineEnding())

// Helper function to escape values
escapeValue := func(s string) string {
// For TSV, just replace tabs with spaces to maintain format
if format == FormatTSV {
return strings.ReplaceAll(s, "\t", " ")
}
// For CSV, use standard CSV escaping
if strings.Contains(s, fileDelimiter) || strings.Contains(s, "\n") || strings.Contains(s, "\"") {
return "\""+strings.ReplaceAll(s, "\"", "\"\"")+"\""
}
return s
}

// Write headers
escapedHeaders := make([]string, len(nonEmptyHeaders))
for i, header := range nonEmptyHeaders {
escapedHeaders[i] = escapeValue(header)
}
output.WriteString(strings.Join(escapedHeaders, fileDelimiter) + utils.GetLineEnding())

// Write rows
for _, row := range rows {
var nonEmptyRow []string
var escapedRow []string
for _, i := range nonEmptyColumnIndexes {
nonEmptyRow = append(nonEmptyRow, row[i])
escapedRow = append(escapedRow, escapeValue(row[i]))
}
output.WriteString(strings.Join(nonEmptyRow, delimiter) + utils.GetLineEnding())
output.WriteString(strings.Join(escapedRow, fileDelimiter) + utils.GetLineEnding())
}
return output.String(), nil

Expand Down
3 changes: 2 additions & 1 deletion pkg/list/list_workflows.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,15 @@ const (
FormatTable = "table"
FormatJSON = "json"
FormatCSV = "csv"
FormatTSV = "tsv"
)

// ValidateFormat checks if the given format is supported
func ValidateFormat(format string) error {
if format == "" {
return nil
}
validFormats := []string{FormatTable, FormatJSON, FormatCSV}
validFormats := []string{FormatTable, FormatJSON, FormatCSV, FormatTSV}
for _, f := range validFormats {
if format == f {
return nil
Expand Down

0 comments on commit 7d23cf2

Please sign in to comment.