lintInlineSQL lints inline SQL passed as a command argument
(cmd *cobra.Command, sql string)
| 332 | |
| 333 | // lintInlineSQL lints inline SQL passed as a command argument |
| 334 | func 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. |
no test coverage detected