Skip to content

Commit

Permalink
fix: annotation value multi-line escape
Browse files Browse the repository at this point in the history
  • Loading branch information
fgouteroux committed Oct 4, 2022
1 parent bb0f7d6 commit f6f1a55
Showing 1 changed file with 15 additions and 7 deletions.
22 changes: 15 additions & 7 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ func main() {
ruleExpr := strings.TrimSuffix(rule.Expr.Value, "\n")
if strings.Contains(ruleExpr, "\n") {
resource += fmt.Sprintf("expr = <<EOT\n%s\nEOT\n", ruleExpr)
} else if strings.Contains(ruleExpr, "\""){
} else if strings.Contains(ruleExpr, "\"") {
resource += fmt.Sprintf("expr = \"%s\"\n", strings.ReplaceAll(ruleExpr, "\"", "\\\""))
} else {
resource += fmt.Sprintf("expr = \"%s\"\n", ruleExpr)
Expand All @@ -108,7 +108,15 @@ func main() {
if len(rule.Annotations) > 0 {
resource += fmt.Sprintf("annotations = {\n")
for aname, avalue := range rule.Annotations {
resource += fmt.Sprintf("%s = \"%s\"\n", aname, strings.ReplaceAll(avalue, "\"", "\\\""))

annotValue := strings.TrimSuffix(avalue, "\n")
if strings.Contains(annotValue, "\n") {
resource += fmt.Sprintf("%s = <<EOT\n%s\nEOT\n", aname, annotValue)
} else if strings.Contains(annotValue, "\"") {
resource += fmt.Sprintf("%s = \"%s\"\n", aname, strings.ReplaceAll(annotValue, "\"", "\\\""))
} else {
resource += fmt.Sprintf("%s = \"%s\"\n", aname, strings.ReplaceAll(annotValue, "\"", "\\\""))
}
}
resource += fmt.Sprintf("}\n")
}
Expand All @@ -127,7 +135,7 @@ func main() {
var recordingRule bool
var resource string
resource = fmt.Sprintf("resource \"mimir_rule_group_recording\" \"%s\" {\n", group.Name)
resource += fmt.Sprintf("name = \"%s\"\n", group.Name)
resource += fmt.Sprintf("name = \"%s\"\n", group.Name)
if obj.Namespace != "" {
resource += fmt.Sprintf("namespace = \"%s\"\n", obj.Namespace)
}
Expand All @@ -143,11 +151,11 @@ func main() {
resource += fmt.Sprintf("record = \"%s\"\n", ruleName)
ruleExpr := strings.TrimSuffix(rule.Expr.Value, "\n")
if strings.Contains(ruleExpr, "\n") {
resource += fmt.Sprintf("expr = <<EOT\n%s\nEOT\n", ruleExpr)
} else if strings.Contains(ruleExpr, "\""){
resource += fmt.Sprintf("expr = \"%s\"\n", strings.ReplaceAll(ruleExpr, "\"", "\\\""))
resource += fmt.Sprintf("expr = <<EOT\n%s\nEOT\n", ruleExpr)
} else if strings.Contains(ruleExpr, "\"") {
resource += fmt.Sprintf("expr = \"%s\"\n", strings.ReplaceAll(ruleExpr, "\"", "\\\""))
} else {
resource += fmt.Sprintf("expr = \"%s\"\n", ruleExpr)
resource += fmt.Sprintf("expr = \"%s\"\n", ruleExpr)
}
resource += fmt.Sprintf("}\n")
}
Expand Down

0 comments on commit f6f1a55

Please sign in to comment.