Fix yaml output single quote (#814)

Fix #659

Reviewed-on: https://gitea.com/gitea/tea/pulls/814
This commit is contained in:
Lunny Xiao
2025-09-14 00:23:12 +00:00
parent 4f513ca3e3
commit 6acb29efd7
2 changed files with 28 additions and 5 deletions

View File

@ -104,15 +104,17 @@ func (t *table) fprint(f io.Writer, output string) {
} }
// outputTable prints structured data as table // outputTable prints structured data as table
func outputTable(f io.Writer, headers []string, values [][]string) { func outputTable(f io.Writer, headers []string, values [][]string) error {
table := tablewriter.NewWriter(f) table := tablewriter.NewWriter(f)
if len(headers) > 0 { if len(headers) > 0 {
table.Header(headers) table.Header(headers)
} }
for _, value := range values { for _, value := range values {
table.Append(value) if err := table.Append(value); err != nil {
return err
}
} }
table.Render() return table.Render()
} }
// outputSimple prints structured data as space delimited value // outputSimple prints structured data as space delimited value
@ -145,9 +147,9 @@ func outputYaml(f io.Writer, headers []string, values [][]string) {
for j, val := range value { for j, val := range value {
intVal, _ := strconv.Atoi(val) intVal, _ := strconv.Atoi(val)
if strconv.Itoa(intVal) == val { if strconv.Itoa(intVal) == val {
fmt.Fprintf(f, " %s: %s\n", headers[j], val) fmt.Fprintf(f, " %s: %s\n", headers[j], val)
} else { } else {
fmt.Fprintf(f, " %s: '%s'\n", headers[j], val) fmt.Fprintf(f, " %s: '%s'\n", headers[j], strings.ReplaceAll(val, "'", "''"))
} }
} }
} }

View File

@ -48,4 +48,25 @@ func TestPrint(t *testing.T) {
assert.EqualValues(t, "\\abc", result[4].A) assert.EqualValues(t, "\\abc", result[4].A)
assert.EqualValues(t, "'def\\", result[4].B) assert.EqualValues(t, "'def\\", result[4].B)
} }
buf.Reset()
tData.fprint(buf, "yaml")
assert.Equal(t, `-
A: 'new a'
B: 'some bbbb'
-
A: 'AAAAA'
B: 'b2'
-
A: '"abc'
B: '"def'
-
A: '''abc'
B: 'de''f'
-
A: '\abc'
B: '''def\'
`, buf.String())
} }