evcc-io/util/templates/utils.go
2023-02-28 10:28:01 +01:00

43 lines
841 B
Go

package templates
import (
"fmt"
"strings"
"gopkg.in/yaml.v3"
)
func quote(value string) string {
quoted := strings.ReplaceAll(value, `'`, `''`)
return fmt.Sprintf("'%s'", quoted)
}
// yamlQuote quotes strings for yaml if they would otherwise by modified by the unmarshaler
func yamlQuote(value string) string {
input := fmt.Sprintf("key: %s", value)
var res struct {
Value string `yaml:"key"`
}
if err := yaml.Unmarshal([]byte(input), &res); err != nil || value != res.Value {
return quote(value)
}
// fix 0815, but not 0
if strings.HasPrefix(value, "0") && len(value) > 1 {
return quote(value)
}
return value
}
func trimEmptyLines(s string) string {
lines := strings.Split(s, "\n")
for i, line := range lines {
if strings.TrimSpace(line) == "" {
lines[i] = ""
}
}
return strings.Join(lines, "\n")
}