Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🧹 Add sortContents to yac bundle struct. Simplify formatting. #966

Merged
merged 3 commits into from
Nov 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 52 additions & 0 deletions internal/bundle/bundle_ext.yac.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
// Copyright (c) Mondoo, Inc.
// SPDX-License-Identifier: BUSL-1.1

package bundle

import "sort"

// Sorts the queries, policies and queries' variants in the bundle.
func (p *Bundle) SortContents() {
sort.SliceStable(p.Queries, func(i, j int) bool {
if p.Queries[i].Mrn == "" || p.Queries[j].Mrn == "" {
return p.Queries[i].Uid < p.Queries[j].Uid
}
return p.Queries[i].Mrn < p.Queries[j].Mrn
})

sort.SliceStable(p.Policies, func(i, j int) bool {
if p.Policies[i].Mrn == "" || p.Policies[j].Mrn == "" {
return p.Policies[i].Uid < p.Policies[j].Uid
}
return p.Policies[i].Mrn < p.Policies[j].Mrn
})

for _, q := range p.Queries {
sort.SliceStable(q.Variants, func(i, j int) bool {
if q.Variants[i].Mrn == "" || q.Variants[j].Mrn == "" {
return q.Variants[i].Uid < q.Variants[j].Uid
}
return q.Variants[i].Mrn < q.Variants[j].Mrn
})
}
for _, pl := range p.Policies {
for _, g := range pl.Groups {
for _, q := range g.Queries {
sort.SliceStable(q.Variants, func(i, j int) bool {
if q.Variants[i].Mrn == "" || q.Variants[j].Mrn == "" {
return q.Variants[i].Uid < q.Variants[j].Uid
}
return q.Variants[i].Mrn < q.Variants[j].Mrn
})
}
for _, c := range g.Checks {
sort.SliceStable(c.Variants, func(i, j int) bool {
if c.Variants[i].Mrn == "" || c.Variants[j].Mrn == "" {
return c.Variants[i].Uid < c.Variants[j].Uid
}
return c.Variants[i].Mrn < c.Variants[j].Mrn
})
}
}
}
}
33 changes: 11 additions & 22 deletions internal/bundle/fmt.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,40 +74,25 @@ func FormatFile(filename string, sort bool) error {
if err != nil {
return err
}

if sort {
b, err := policy.BundleFromYAML(data)
if err != nil {
return err
}

b.SortContents()
data, err = b.ToYAML()
if err != nil {
return err
}
b, err := ParseYaml(data)
if err != nil {
return err
}

data, err = FormatBundleData(data)
fmtData, err := FormatBundle(b, sort)
if err != nil {
return err
}

err = os.WriteFile(filename, data, 0o644)
err = os.WriteFile(filename, fmtData, 0o644)
if err != nil {
return err
}

return nil
}

// Format formats the .mql.yaml bundle
func FormatBundleData(data []byte) ([]byte, error) {
b, err := ParseYaml(data)
if err != nil {
return nil, err
}

// Format formats the Bundle
func FormatBundle(b *Bundle, sort bool) ([]byte, error) {
// to improve the formatting we need to remove the whitespace at the end of the lines
for i := range b.Queries {
query := b.Queries[i]
Expand Down Expand Up @@ -138,5 +123,9 @@ func FormatBundleData(data []byte) ([]byte, error) {
}
}

if sort {
b.SortContents()
}

return Format(b)
}
16 changes: 8 additions & 8 deletions internal/bundle/fmt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ import (

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.mondoo.com/cnspec/v9/policy"
)

func TestBundleFormatter(t *testing.T) {
data := `
# This is a comment
policies:
- uid: sshd-server-policy
authors:
Expand Down Expand Up @@ -43,10 +43,13 @@ queries:
title: Ensure Secure Boot is enabled
`

formatted, err := FormatBundleData([]byte(data))
b, err := ParseYaml([]byte(data))
require.NoError(t, err)
formatted, err := FormatBundle(b, false)
require.NoError(t, err)

expected := `policies:
expected := `# This is a comment
policies:
- uid: sshd-server-policy
name: SSH Server Policy
version: 1.0.0
Expand Down Expand Up @@ -128,12 +131,9 @@ queries:
title: Ensure Secure Boot is enabled
`

b, err := policy.BundleFromYAML([]byte(data))
require.NoError(t, err)
b.SortContents()
byteData, err := b.ToYAML()
b, err := ParseYaml([]byte(data))
require.NoError(t, err)
formatted, err := FormatBundleData(byteData)
formatted, err := FormatBundle(b, true)
require.NoError(t, err)
expected := `policies:
- uid: sshd-server-policy
Expand Down