MCPcopy Create free account
hub / github.com/ajitpratap0/GoSQLX / lintInlineSQL

Function lintInlineSQL

cmd/gosqlx/cmd/lint.go:334–379  ·  view source on GitHub ↗

lintInlineSQL lints inline SQL passed as a command argument

(cmd *cobra.Command, sql string)

Source from the content-addressed store, hash-verified

332
333// lintInlineSQL lints inline SQL passed as a command argument
334func lintInlineSQL(cmd *cobra.Command, sql string) error {
335 l := createLinter()
336 result := l.LintString(sql, "inline")
337
338 var outputBuf bytes.Buffer
339 outWriter := io.Writer(cmd.OutOrStdout())
340 if outputFile != "" {
341 outWriter = &outputBuf
342 }
343
344 if len(result.Violations) == 0 {
345 fmt.Fprintln(outWriter, "No violations found.")
346 if outputFile != "" {
347 return WriteOutput(outputBuf.Bytes(), outputFile, cmd.OutOrStdout())
348 }
349 return nil
350 }
351
352 fmt.Fprintf(outWriter, "Found %d violation(s):\n\n", len(result.Violations))
353 for i, violation := range result.Violations {
354 fmt.Fprintf(outWriter, "%d. %s\n", i+1, linter.FormatViolation(violation))
355 }
356
357 if outputFile != "" {
358 if err := WriteOutput(outputBuf.Bytes(), outputFile, cmd.OutOrStdout()); err != nil {
359 return err
360 }
361 }
362
363 errorCount := 0
364 warningCount := 0
365 for _, violation := range result.Violations {
366 switch violation.Severity {
367 case linter.SeverityError:
368 errorCount++
369 case linter.SeverityWarning:
370 warningCount++
371 }
372 }
373
374 if errorCount > 0 || (lintFailOnWarn && warningCount > 0) {
375 return fmt.Errorf("%d error(s) and %d warning(s) found", errorCount, warningCount)
376 }
377
378 return nil
379}
380
381// runSecurityScan runs the security scanner on the given SQL and prints findings.
382// Returns the number of security findings.

Callers 1

lintRunFunction · 0.85

Calls 5

FormatViolationFunction · 0.92
createLinterFunction · 0.85
LintStringMethod · 0.80
WriteOutputFunction · 0.70
ErrorfMethod · 0.65

Tested by

no test coverage detected