Skip to content
22 changes: 18 additions & 4 deletions cmd/gosqlx/cmd/parser_cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"gopkg.in/yaml.v3"

"github.com/ajitpratap0/GoSQLX/cmd/gosqlx/internal/config"
"github.com/ajitpratap0/GoSQLX/cmd/gosqlx/internal/output"
"github.com/ajitpratap0/GoSQLX/pkg/models"
"github.com/ajitpratap0/GoSQLX/pkg/sql/ast"
"github.com/ajitpratap0/GoSQLX/pkg/sql/parser"
Expand Down Expand Up @@ -173,6 +174,11 @@ type StatementDisplay struct {

// displayAST displays AST structure
func (p *Parser) displayAST(astObj *ast.AST) error {
// Use the new JSON output format for consistency
if strings.ToLower(p.Opts.Format) == "json" {
return p.displayASTJSON(astObj)
}

type ASTDisplay struct {
Type string `json:"type" yaml:"type"`
Statements []StatementDisplay `json:"statements" yaml:"statements"`
Expand All @@ -197,10 +203,6 @@ func (p *Parser) displayAST(astObj *ast.AST) error {
}

switch strings.ToLower(p.Opts.Format) {
case "json":
encoder := json.NewEncoder(p.Out)
encoder.SetIndent("", " ")
return encoder.Encode(display)
case "yaml":
encoder := yaml.NewEncoder(p.Out)
defer func() {
Expand All @@ -227,6 +229,18 @@ func (p *Parser) displayAST(astObj *ast.AST) error {
}
}

// displayASTJSON displays AST in JSON format using the standardized output format
func (p *Parser) displayASTJSON(astObj *ast.AST) error {
// Use the standardized JSON output format
jsonData, err := output.FormatParseJSON(astObj, "input", false, nil)
if err != nil {
return fmt.Errorf("failed to format JSON output: %w", err)
}

fmt.Fprint(p.Out, string(jsonData))
return nil
}

// displayTree displays AST in tree format
func (p *Parser) displayTree(astObj *ast.AST) error {
fmt.Fprintf(p.Out, "🌳 AST Tree Structure:\n")
Expand Down
42 changes: 34 additions & 8 deletions cmd/gosqlx/cmd/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,14 +89,14 @@ func validateRun(cmd *cobra.Command, args []string) error {
}

// Create validator options from config and flags
// When outputting SARIF, automatically enable quiet mode to avoid mixing output
quietMode := validateQuiet || validateOutputFormat == "sarif"
// When outputting SARIF or JSON, automatically enable quiet mode to avoid mixing output
quietMode := validateQuiet || validateOutputFormat == "sarif" || validateOutputFormat == "json"

opts := ValidatorOptionsFromConfig(cfg, flagsChanged, ValidatorFlags{
Recursive: validateRecursive,
Pattern: validatePattern,
Quiet: quietMode,
ShowStats: validateStats,
ShowStats: validateStats && validateOutputFormat == "text", // Only show text stats for text output
Dialect: validateDialect,
StrictMode: validateStrict,
Verbose: verbose,
Expand Down Expand Up @@ -131,8 +131,23 @@ func validateRun(cmd *cobra.Command, args []string) error {
fmt.Fprint(cmd.OutOrStdout(), string(sarifData))
}
} else if validateOutputFormat == "json" {
// JSON output format will be implemented later
return fmt.Errorf("JSON output format not yet implemented")
// Generate JSON output
jsonData, err := output.FormatValidationJSON(result, args, validateStats)
if err != nil {
return fmt.Errorf("failed to generate JSON output: %w", err)
}

// Write JSON output to file or stdout
if validateOutputFile != "" {
if err := os.WriteFile(validateOutputFile, jsonData, 0600); err != nil {
return fmt.Errorf("failed to write JSON output: %w", err)
}
if !opts.Quiet {
fmt.Fprintf(cmd.OutOrStdout(), "JSON output written to %s\n", validateOutputFile)
}
} else {
fmt.Fprint(cmd.OutOrStdout(), string(jsonData))
}
}
// Default text output is already handled by the validator

Expand Down Expand Up @@ -189,12 +204,12 @@ func validateFromStdin(cmd *cobra.Command) error {
}

// Create validator options
quietMode := validateQuiet || validateOutputFormat == "sarif"
quietMode := validateQuiet || validateOutputFormat == "sarif" || validateOutputFormat == "json"
opts := ValidatorOptionsFromConfig(cfg, flagsChanged, ValidatorFlags{
Recursive: false, // stdin is always single input
Pattern: "",
Quiet: quietMode,
ShowStats: validateStats,
ShowStats: validateStats && validateOutputFormat == "text", // Only show text stats for text output
Dialect: validateDialect,
StrictMode: validateStrict,
Verbose: verbose,
Expand Down Expand Up @@ -228,7 +243,18 @@ func validateFromStdin(cmd *cobra.Command) error {
fmt.Fprintf(cmd.OutOrStdout(), "SARIF output written to %s\n", validateOutputFile)
}
} else if validateOutputFormat == "json" {
return fmt.Errorf("JSON output format not yet implemented")
jsonData, err := output.FormatValidationJSON(result, []string{"stdin"}, validateStats)
if err != nil {
return fmt.Errorf("failed to generate JSON output: %w", err)
}

if err := WriteOutput(jsonData, validateOutputFile, cmd.OutOrStdout()); err != nil {
return err
}

if validateOutputFile != "" && !opts.Quiet {
fmt.Fprintf(cmd.OutOrStdout(), "JSON output written to %s\n", validateOutputFile)
}
}

// Exit with error code if validation failed
Expand Down
Loading
Loading