Skip to content

Commit 091f5e2

Browse files
committed
export linting function
1 parent 32b0bde commit 091f5e2

File tree

1 file changed

+24
-19
lines changed

1 file changed

+24
-19
lines changed

cmd/linter/lint_cmd.go

Lines changed: 24 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,7 @@ import (
77
"strings"
88

99
"github.com/CrowdStrike/gql/pkg/linter"
10-
1110
"github.com/CrowdStrike/gql/utils"
12-
1311
"github.com/spf13/cobra"
1412
)
1513

@@ -28,7 +26,7 @@ func NewLintCmd() *cobra.Command {
2826
Run: func(cmd *cobra.Command, args []string) {
2927
schemaFileContents := make(map[string][]byte)
3028

31-
rulesToApply, err := findTheRulesToApply(passedRules)
29+
rulesToApply, err := FindTheRulesToApply(passedRules)
3230
if err != nil {
3331
fmt.Printf("failed to parse the rules to apply string=%s, error:%v", passedRules, err)
3432
//Exit with error printed to stderr
@@ -51,21 +49,7 @@ func NewLintCmd() *cobra.Command {
5149
}
5250
}
5351

54-
exitStatus := 0
55-
errorCount := 0
56-
for filename, schemaFileContent := range schemaFileContents {
57-
if lintErrors := Lint(filename, string(schemaFileContent), rulesToApply); len(lintErrors) != 0 {
58-
errorCount += len(lintErrors)
59-
errorPresenter(filename, lintErrors)
60-
exitStatus |= 1 // If there's error for any file, exit code should be 1
61-
}
62-
}
63-
64-
if errorCount == 0 {
65-
fmt.Printf("Schema has no lint errors! 🎉\n")
66-
} else {
67-
fmt.Printf("❌ Total lint errors found: %d\n", errorCount)
68-
}
52+
exitStatus := FindLintErrors(schemaFileContents, rulesToApply)
6953

7054
os.Exit(exitStatus) // success
7155
},
@@ -75,7 +59,28 @@ func NewLintCmd() *cobra.Command {
7559
return lintCmd
7660
}
7761

78-
func findTheRulesToApply(rulesString []string) ([]linter.LintRuleFunc, error) {
62+
// FindLintErrors find lint errors in schema files applying the supplied rules
63+
func FindLintErrors(schemaFileContents map[string][]byte, rulesToApply []linter.LintRuleFunc) int {
64+
exitStatus := 0
65+
errorCount := 0
66+
for filename, schemaFileContent := range schemaFileContents {
67+
if lintErrors := Lint(filename, string(schemaFileContent), rulesToApply); len(lintErrors) != 0 {
68+
errorCount += len(lintErrors)
69+
errorPresenter(filename, lintErrors)
70+
exitStatus |= 1 // If there's error for any file, exit code should be 1
71+
}
72+
}
73+
74+
if errorCount == 0 {
75+
fmt.Printf("Schema has no lint errors! 🎉\n")
76+
} else {
77+
fmt.Printf("❌ Total lint errors found: %d\n", errorCount)
78+
}
79+
return exitStatus
80+
}
81+
82+
// FindTheRulesToApply convert matching string rules to LintRuleFunc
83+
func FindTheRulesToApply(rulesString []string) ([]linter.LintRuleFunc, error) {
7984
rulesToApply := make([]linter.LintRuleFunc, 0)
8085
if len(rulesString) == 0 {
8186
for _, rule := range linter.AllTheRules {

0 commit comments

Comments
 (0)