Skip to content

Commit

Permalink
B #5914: fix newline escaping
Browse files Browse the repository at this point in the history
Signed-off-by: Pierre Lafievre <[email protected]>
(cherry picked from commit 75f2411)
  • Loading branch information
treywelsh authored and rsmontero committed Jul 27, 2022
1 parent fe126c9 commit 04bbc74
Showing 1 changed file with 21 additions and 1 deletion.
22 changes: 21 additions & 1 deletion src/oca/go/src/goca/dynamic/dyntemplate.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package dynamic

import (
"bytes"
"encoding/xml"
"fmt"
"strconv"
Expand Down Expand Up @@ -74,7 +75,19 @@ func (t *Template) String() string {

// String prints a Pair in OpenNebula syntax
func (p *Pair) String() string {
return fmt.Sprintf("%s=%s", p.XMLName.Local, strconv.Quote(p.Value))

if strconv.CanBackquote(p.Value) {
return fmt.Sprintf("%s=%s", p.XMLName.Local, strconv.Quote(p.Value))
} else {
buf := bytes.NewBufferString(p.XMLName.Local)
buf.WriteString("=\"")
buf.WriteString(strings.ReplaceAll(p.Value, `"`, `\"`))
buf.WriteString(strings.ReplaceAll(p.Value, `\`, `\\`))
buf.WriteByte('"')

return buf.String()

}
}

func (v *Vector) String() string {
Expand Down Expand Up @@ -352,6 +365,13 @@ func NewTemplate() *Template {
return &Template{}
}

// NewTemplate returns a new Template object
func NewVector(name string) *Vector {
return &Vector{
XMLName: xml.Name{Local: name},
}
}

// AddVector creates a new vector in the template
func (t *Template) AddVector(key string) *Vector {
vector := &Vector{XMLName: xml.Name{Local: key}}
Expand Down

0 comments on commit 04bbc74

Please sign in to comment.