Skip to content

Commit

Permalink
Add also double quotes in CSV if string contains linebreak or double …
Browse files Browse the repository at this point in the history
…quotes
  • Loading branch information
fxaguessy committed Jun 21, 2018
1 parent d72913b commit 65d86e9
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 2 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@

### Fixes

- [#220](https://github.com/wallix/awless/issues/220): Add double quotes to CSV output if needed thanks to @lllama
- Fix compilation error in templates with concatenation and reference (c.f. for example in [this template](https://gist.githubusercontent.com/fxaguessy/ef9511bf5ed8f3312904cccb96b818e8/raw/75c0f808220665441055b589be133cf711c64f37/ManageOwnMFA.aws))
- Parse integer beginning with '0' as string (preventing the deletion of the initial '0' for example in `... account.id=0123456789`)


## v0.1.10 [2018-04-13]

### Features
Expand Down
3 changes: 2 additions & 1 deletion console/displayer.go
Original file line number Diff line number Diff line change
Expand Up @@ -406,7 +406,8 @@ func (d *csvDisplayer) Print(w io.Writer) error {
var props []string
for j, h := range d.columnDefinitions {
val := h.format(values[i][j])
if strings.Contains(val, ",") {
if strings.ContainsAny(val, ",\n\"") {
val = strings.Replace(val, "\"", "\"\"", -1) // Replace " in val by "" (cf https://tools.ietf.org/html/rfc4180)
val = "\"" + val + "\""
}
props = append(props, val)
Expand Down
27 changes: 27 additions & 0 deletions console/displayer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -681,6 +681,33 @@ func TestCompareInterface(t *testing.T) {
}
}

func TestCSVDisplayWithCommaAndQuotes(t *testing.T) {
g := graph.NewGraph()
g.AddResource(
resourcetest.Instance("inst_1").Prop(p.Name, "with,comma").Build(),
resourcetest.Instance("inst_2").Prop(p.Name, "with\nlinebreak").Build(),
resourcetest.Instance("inst_3").Prop(p.Name, "with\"quote").Build(),
)

displayer, _ := BuildOptions(
WithRdfType("instance"),
WithColumns([]string{"ID", "Name"}),
WithFormat("csv"),
).SetSource(g).Build()

expected := "ID,Name\n" +
"inst_1,\"with,comma\"\n" +
"inst_2,\"with\nlinebreak\"\n" +
"inst_3,\"with\"\"quote\"\n"
var w bytes.Buffer
if err := displayer.Print(&w); err != nil {
t.Fatal(err)
}
if got, want := w.String(), expected; got != want {
t.Fatalf("got \n[%q]\n\nwant\n\n[%q]\n", got, want)
}
}

func createInfraGraph() *graph.Graph {
g := graph.NewGraph()
g.AddResource(resourcetest.Region("eu-west-1").Build(),
Expand Down

0 comments on commit 65d86e9

Please sign in to comment.