/
multilines.go
48 lines (42 loc) · 1.3 KB
/
multilines.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
package tools
import (
"strings"
"text/template"
)
// DecorateWithMultilineHandlingFunctions adds necessary functions to escape multiline fields in exported terraform files.
func DecorateWithMultilineHandlingFunctions(additionalFuncs map[string]any) template.FuncMap {
multiLineFuncs := map[string]any{
"Escape": Escape,
"IsMultiline": IsMultiline,
"NoNewlineAtTheEnd": NoNewlineAtTheEnd,
"RemoveLastNewline": RemoveLastNewline,
"GetEOT": GetEOT,
}
for k, v := range additionalFuncs {
multiLineFuncs[k] = v
}
return multiLineFuncs
}
// IsMultiline returns true if the input string contains at least one new line character
func IsMultiline(str string) bool {
return strings.LastIndex(str, "\n") >= 0
}
// NoNewlineAtTheEnd returns true if there is no new line character at the end of the string
func NoNewlineAtTheEnd(str string) bool {
if str == "" {
return true
}
return str[len(str)-1:] != "\n"
}
// RemoveLastNewline removes the new line character if this is the last character in the string
func RemoveLastNewline(str string) string {
return strings.TrimSuffix(str, "\n")
}
// GetEOT generates unique delimiter word for heredoc, by default it is EOT
func GetEOT(str string) string {
eot := "EOT"
for strings.LastIndex(str, eot) >= 0 {
eot += "A"
}
return eot
}