1
0
mirror of https://github.com/OpenNebula/one.git synced 2025-03-21 14:50:08 +03:00

B #5914: fix newline escaping

Signed-off-by: Pierre Lafievre <pierre.lafievre@iguanesolutions.com>
This commit is contained in:
Pierre Lafievre 2022-07-26 15:21:20 +02:00 committed by Ruben S. Montero
parent 9837534bbd
commit 75f241110e
No known key found for this signature in database
GPG Key ID: A0CEA6FA880A1D87

View File

@ -17,6 +17,7 @@
package dynamic
import (
"bytes"
"encoding/xml"
"fmt"
"strconv"
@ -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 {
@ -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}}