mirror of
				https://gitea.com/gitea/tea.git
				synced 2025-10-31 17:25:27 +01:00 
			
		
		
		
	Fix yaml output single quote (#814)
Fix #659 Reviewed-on: https://gitea.com/gitea/tea/pulls/814
This commit is contained in:
		| @@ -104,15 +104,17 @@ func (t *table) fprint(f io.Writer, output string) { | ||||
| } | ||||
|  | ||||
| // 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) | ||||
| 	if len(headers) > 0 { | ||||
| 		table.Header(headers) | ||||
| 	} | ||||
| 	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 | ||||
| @@ -145,9 +147,9 @@ func outputYaml(f io.Writer, headers []string, values [][]string) { | ||||
| 		for j, val := range value { | ||||
| 			intVal, _ := strconv.Atoi(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 { | ||||
| 				fmt.Fprintf(f, "    %s: '%s'\n", headers[j], val) | ||||
| 				fmt.Fprintf(f, "  %s: '%s'\n", headers[j], strings.ReplaceAll(val, "'", "''")) | ||||
| 			} | ||||
| 		} | ||||
| 	} | ||||
|   | ||||
| @@ -48,4 +48,25 @@ func TestPrint(t *testing.T) { | ||||
| 		assert.EqualValues(t, "\\abc", result[4].A) | ||||
| 		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()) | ||||
| } | ||||
|   | ||||
		Reference in New Issue
	
	Block a user
	 Lunny Xiao
					Lunny Xiao