11package cmd
22
33import (
4+ "fmt"
45 "os"
56
67 "github.com/spf13/cobra"
78 "github.com/spf13/pflag"
89
910 "github.com/ajitpratap0/GoSQLX/cmd/gosqlx/internal/config"
11+ "github.com/ajitpratap0/GoSQLX/cmd/gosqlx/internal/output"
1012)
1113
1214var (
13- validateRecursive bool
14- validatePattern string
15- validateQuiet bool
16- validateStats bool
17- validateDialect string
18- validateStrict bool
15+ validateRecursive bool
16+ validatePattern string
17+ validateQuiet bool
18+ validateStats bool
19+ validateDialect string
20+ validateStrict bool
21+ validateOutputFormat string
22+ validateOutputFile string
1923)
2024
2125// validateCmd represents the validate command
@@ -31,6 +35,12 @@ Examples:
3135 gosqlx validate -r ./queries/ # Recursively validate directory
3236 gosqlx validate --quiet query.sql # Quiet mode (exit code only)
3337 gosqlx validate --stats ./queries/ # Show performance statistics
38+ gosqlx validate --output-format sarif --output-file results.sarif queries/ # SARIF output for GitHub Code Scanning
39+
40+ Output Formats:
41+ text - Human-readable output (default)
42+ json - JSON format for programmatic consumption
43+ sarif - SARIF 2.1.0 format for GitHub Code Scanning integration
3444
3545Performance Target: <10ms for typical queries (50-500 characters)
3646Throughput: 100+ files/second in batch mode` ,
@@ -46,6 +56,11 @@ func validateRun(cmd *cobra.Command, args []string) error {
4656 cfg = config .DefaultConfig ()
4757 }
4858
59+ // Validate output format
60+ if validateOutputFormat != "" && validateOutputFormat != "text" && validateOutputFormat != "json" && validateOutputFormat != "sarif" {
61+ return fmt .Errorf ("invalid output format: %s (valid options: text, json, sarif)" , validateOutputFormat )
62+ }
63+
4964 // Track which flags were explicitly set
5065 flagsChanged := make (map [string ]bool )
5166 cmd .Flags ().Visit (func (f * pflag.Flag ) {
@@ -58,10 +73,13 @@ func validateRun(cmd *cobra.Command, args []string) error {
5873 }
5974
6075 // Create validator options from config and flags
76+ // When outputting SARIF, automatically enable quiet mode to avoid mixing output
77+ quietMode := validateQuiet || validateOutputFormat == "sarif"
78+
6179 opts := ValidatorOptionsFromConfig (cfg , flagsChanged , ValidatorFlags {
6280 Recursive : validateRecursive ,
6381 Pattern : validatePattern ,
64- Quiet : validateQuiet ,
82+ Quiet : quietMode ,
6583 ShowStats : validateStats ,
6684 Dialect : validateDialect ,
6785 StrictMode : validateStrict ,
@@ -77,6 +95,31 @@ func validateRun(cmd *cobra.Command, args []string) error {
7795 return err
7896 }
7997
98+ // Handle different output formats
99+ if validateOutputFormat == "sarif" {
100+ // Generate SARIF output
101+ sarifData , err := output .FormatSARIF (result , Version )
102+ if err != nil {
103+ return fmt .Errorf ("failed to generate SARIF output: %w" , err )
104+ }
105+
106+ // Write SARIF output to file or stdout
107+ if validateOutputFile != "" {
108+ if err := os .WriteFile (validateOutputFile , sarifData , 0644 ); err != nil {
109+ return fmt .Errorf ("failed to write SARIF output: %w" , err )
110+ }
111+ if ! opts .Quiet {
112+ fmt .Fprintf (cmd .OutOrStdout (), "SARIF output written to %s\n " , validateOutputFile )
113+ }
114+ } else {
115+ fmt .Fprint (cmd .OutOrStdout (), string (sarifData ))
116+ }
117+ } else if validateOutputFormat == "json" {
118+ // JSON output format will be implemented later
119+ return fmt .Errorf ("JSON output format not yet implemented" )
120+ }
121+ // Default text output is already handled by the validator
122+
80123 // Exit with error code if there were invalid files
81124 if result .InvalidFiles > 0 {
82125 os .Exit (1 )
@@ -94,4 +137,6 @@ func init() {
94137 validateCmd .Flags ().BoolVarP (& validateStats , "stats" , "s" , false , "show performance statistics" )
95138 validateCmd .Flags ().StringVar (& validateDialect , "dialect" , "" , "SQL dialect: postgresql, mysql, sqlserver, oracle, sqlite (config: validate.dialect)" )
96139 validateCmd .Flags ().BoolVar (& validateStrict , "strict" , false , "enable strict validation mode (config: validate.strict_mode)" )
140+ validateCmd .Flags ().StringVar (& validateOutputFormat , "output-format" , "text" , "output format: text, json, sarif" )
141+ validateCmd .Flags ().StringVar (& validateOutputFile , "output-file" , "" , "output file path (default: stdout)" )
97142}
0 commit comments