Skip to content

Commit

Permalink
🧹 Add sortContents to yac bundle struct. Simplify formatting. (#966)
Browse files Browse the repository at this point in the history
* 🧹 Add sortContents to yac bundle struct. Simplify formatting.
* add license header, fix lint issues.
* Extend test with a comment.
  • Loading branch information
preslavgerchev authored Nov 29, 2023
1 parent e8cec1d commit ec17cae
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 30 deletions.
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

0 comments on commit ec17cae

Please sign in to comment.