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,27 @@ 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+ 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 ) {
7983 rulesToApply := make ([]linter.LintRuleFunc , 0 )
8084 if len (rulesString ) == 0 {
8185 for _ , rule := range linter .AllTheRules {
@@ -97,7 +101,6 @@ func findTheRulesToApply(rulesString []string) ([]linter.LintRuleFunc, error) {
97101 if ! matchFound {
98102 return nil , fmt .Errorf ("invalid rule[%s] passed" , inputRuleName )
99103 }
100-
101104 }
102105 return rulesToApply , nil
103106}
0 commit comments