7
7
"strings"
8
8
9
9
"github.com/CrowdStrike/gql/pkg/linter"
10
-
11
10
"github.com/CrowdStrike/gql/utils"
12
-
13
11
"github.com/spf13/cobra"
14
12
)
15
13
@@ -28,7 +26,7 @@ func NewLintCmd() *cobra.Command {
28
26
Run : func (cmd * cobra.Command , args []string ) {
29
27
schemaFileContents := make (map [string ][]byte )
30
28
31
- rulesToApply , err := findTheRulesToApply (passedRules )
29
+ rulesToApply , err := FindTheRulesToApply (passedRules )
32
30
if err != nil {
33
31
fmt .Printf ("failed to parse the rules to apply string=%s, error:%v" , passedRules , err )
34
32
//Exit with error printed to stderr
@@ -51,21 +49,7 @@ func NewLintCmd() *cobra.Command {
51
49
}
52
50
}
53
51
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 )
69
53
70
54
os .Exit (exitStatus ) // success
71
55
},
@@ -75,7 +59,27 @@ func NewLintCmd() *cobra.Command {
75
59
return lintCmd
76
60
}
77
61
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
+ if errorCount == 0 {
74
+ fmt .Printf ("Schema has no lint errors! 🎉\n " )
75
+ } else {
76
+ fmt .Printf ("❌ Total lint errors found: %d\n " , errorCount )
77
+ }
78
+ return exitStatus
79
+ }
80
+
81
+ // FindTheRulesToApply convert matching string rules to LintRuleFunc
82
+ func FindTheRulesToApply (rulesString []string ) ([]linter.LintRuleFunc , error ) {
79
83
rulesToApply := make ([]linter.LintRuleFunc , 0 )
80
84
if len (rulesString ) == 0 {
81
85
for _ , rule := range linter .AllTheRules {
@@ -97,7 +101,6 @@ func findTheRulesToApply(rulesString []string) ([]linter.LintRuleFunc, error) {
97
101
if ! matchFound {
98
102
return nil , fmt .Errorf ("invalid rule[%s] passed" , inputRuleName )
99
103
}
100
-
101
104
}
102
105
return rulesToApply , nil
103
106
}
0 commit comments