Skip to content

Commit

Permalink
🧹 improve bundle formatter to handle trailing whitespace (#684)
Browse files Browse the repository at this point in the history
This further improves the bundle formatter to handle trailing
whitespace. Trailing whitespace is the main reason the formatter outputs
strings as quoted strings. This addresses this issue for more fields in
the yaml format.
  • Loading branch information
chris-rock authored Aug 7, 2023
1 parent 16b3ea7 commit 3d6d4dc
Showing 1 changed file with 36 additions and 6 deletions.
42 changes: 36 additions & 6 deletions internal/bundle/fmt.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"encoding/json"
"os"
"strings"
"unicode"

"github.com/cockroachdb/errors"
"github.com/rs/zerolog/log"
Expand Down Expand Up @@ -88,6 +89,16 @@ func DeprecatedV7_ToV8(data []byte) ([]byte, error) {
return Format(v8yaci)
}

// sanitizeStringForYaml is here to help generating literal style yaml strings
// if a string has a trailing space in a line, it is automatically converted into quoted style
func sanitizeStringForYaml(s string) string {
lines := strings.Split(s, "\n")
for j := range lines {
lines[j] = strings.TrimRightFunc(lines[j], unicode.IsSpace)
}
return strings.Join(lines, "\n")
}

// Format formats the .mql.yaml bundle
func FormatFile(filename string) error {
log.Info().Str("file", filename).Msg("format file")
Expand All @@ -99,14 +110,33 @@ func FormatFile(filename string) error {
b, err := ParseYaml(data)

// to improve the formatting we need to remove the whitespace at the end of the lines
// this is a bit hacky, but it works
for i := range b.Queries {
mql := b.Queries[i].Mql
lines := strings.Split(mql, "\n")
for j := range lines {
lines[j] = strings.TrimRight(lines[j], " ")
query := b.Queries[i]
query.Title = sanitizeStringForYaml(query.Title)
query.Mql = sanitizeStringForYaml(query.Mql)
if query.Docs != nil {
query.Docs.Desc = sanitizeStringForYaml(query.Docs.Desc)
query.Docs.Audit = sanitizeStringForYaml(query.Docs.Audit)
if query.Docs.Remediation != nil {
for j := range query.Docs.Remediation.Items {
docs := query.Docs.Remediation.Items[j]
docs.Desc = sanitizeStringForYaml(docs.Desc)
}
}
}
}

for i := range b.Frameworks {
for j := range b.Frameworks[i].Groups {
grp := b.Frameworks[i].Groups[j]
grp.Title = sanitizeStringForYaml(grp.Title)
for k := range grp.Controls {
grp.Controls[k].Title = sanitizeStringForYaml(grp.Controls[k].Title)
if grp.Controls[k].Docs != nil {
grp.Controls[k].Docs.Desc = sanitizeStringForYaml(grp.Controls[k].Docs.Desc)
}
}
}
b.Queries[i].Mql = strings.Join(lines, "\n")
}

// we have v7 structs in v8 bundle, so it can happen that v7 parses properly
Expand Down

0 comments on commit 3d6d4dc

Please sign in to comment.