-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
🧹 ensure example query packs pass our own lint requirements
- Loading branch information
1 parent
65df0ae
commit 18de21e
Showing
6 changed files
with
81 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
// Copyright (c) Mondoo, Inc. | ||
// SPDX-License-Identifier: BUSL-1.1 | ||
|
||
package examples | ||
|
||
import ( | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
"go.mondoo.com/cnquery/v10/explorer" | ||
"go.mondoo.com/cnquery/v10/internal/bundle" | ||
"testing" | ||
) | ||
|
||
func TestExampleLint(t *testing.T) { | ||
queryPackBundle, err := explorer.BundleFromPaths(".") | ||
require.NoError(t, err) | ||
|
||
lintErr := bundle.Lint(queryPackBundle) | ||
assert.Equal(t, []string{}, lintErr) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,6 +3,7 @@ | |
|
||
packs: | ||
- uid: linux-mixed-queries | ||
name: Linux Mixed Queries | ||
filters: | ||
- asset.family.contains("unix") | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
// Copyright (c) Mondoo, Inc. | ||
// SPDX-License-Identifier: BUSL-1.1 | ||
|
||
package bundle | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"go.mondoo.com/cnquery/v10/explorer" | ||
"go.mondoo.com/cnquery/v10/providers" | ||
"strconv" | ||
) | ||
|
||
func Lint(queryPackBundle *explorer.Bundle) []string { | ||
errors := []string{} | ||
|
||
// check that we have uids for packs and queries | ||
for i := range queryPackBundle.Packs { | ||
pack := queryPackBundle.Packs[i] | ||
packId := strconv.Itoa(i) | ||
|
||
if pack.Uid == "" { | ||
errors = append(errors, fmt.Sprintf("pack %s does not define a uid", packId)) | ||
} else { | ||
packId = pack.Uid | ||
} | ||
|
||
if pack.Name == "" { | ||
errors = append(errors, fmt.Sprintf("pack %s does not define a name", packId)) | ||
} | ||
|
||
for j := range pack.Queries { | ||
query := pack.Queries[j] | ||
queryId := strconv.Itoa(j) | ||
if query.Uid == "" { | ||
errors = append(errors, fmt.Sprintf("query %s/%s does not define a uid", packId, queryId)) | ||
} else { | ||
queryId = query.Uid | ||
} | ||
|
||
if query.Title == "" { | ||
errors = append(errors, fmt.Sprintf("query %s/%s does not define a name", packId, queryId)) | ||
} | ||
} | ||
} | ||
|
||
// we compile after the checks because it removes the uids and replaces it with mrns | ||
schema := providers.DefaultRuntime().Schema() | ||
_, err := queryPackBundle.Compile(context.Background(), schema) | ||
if err != nil { | ||
errors = append(errors, "could not compile the query pack bundle", err.Error()) | ||
} | ||
|
||
return errors | ||
} |