-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathformat.go
More file actions
102 lines (85 loc) · 3.21 KB
/
format.go
File metadata and controls
102 lines (85 loc) · 3.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
package cmd
import (
"os"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
"github.com/ajitpratap0/GoSQLX/cmd/gosqlx/internal/config"
)
var (
formatInPlace bool
formatIndentSize int
formatUppercase bool
formatCompact bool
formatCheck bool
formatMaxLine int
)
// formatCmd represents the format command
var formatCmd = &cobra.Command{
Use: "format [file...]",
Short: "High-performance SQL formatting",
Long: `Format SQL queries with high-performance processing.
Examples:
gosqlx format query.sql # Format to stdout
gosqlx format -i query.sql # Format in-place
gosqlx format --indent 4 query.sql # Use 4-space indentation
gosqlx format --no-uppercase query.sql # Keep original keyword case
gosqlx format --compact query.sql # Compact format (minimal whitespace)
gosqlx format --check query.sql # Check if formatting is needed (CI mode)
gosqlx format "*.sql" # Format all SQL files
gosqlx format -o formatted.sql query.sql # Save to specific file
Performance: 100x faster than SQLFluff for equivalent operations`,
Args: cobra.MinimumNArgs(1),
RunE: formatRun,
}
func formatRun(cmd *cobra.Command, args []string) error {
// Load configuration with CLI flag overrides
cfg, err := config.LoadDefault()
if err != nil {
// If config load fails, use defaults
cfg = config.DefaultConfig()
}
// Track which flags were explicitly set
flagsChanged := make(map[string]bool)
cmd.Flags().Visit(func(f *pflag.Flag) {
flagsChanged[f.Name] = true
})
if cmd.Parent() != nil && cmd.Parent().PersistentFlags() != nil {
cmd.Parent().PersistentFlags().Visit(func(f *pflag.Flag) {
flagsChanged[f.Name] = true
})
}
// Create formatter options from config and flags
opts := FormatterOptionsFromConfig(cfg, flagsChanged, FormatterFlags{
InPlace: formatInPlace,
IndentSize: formatIndentSize,
Uppercase: formatUppercase,
Compact: formatCompact,
Check: formatCheck,
MaxLine: formatMaxLine,
Verbose: verbose,
Output: outputFile,
})
// Create formatter with injectable output writers
formatter := NewFormatter(cmd.OutOrStdout(), cmd.ErrOrStderr(), opts)
// Run formatting
result, err := formatter.Format(args)
if err != nil {
return err
}
// Exit with error code if files need formatting in check mode
if len(result.NeedsFormatting) > 0 {
os.Exit(1)
}
return nil
}
func init() {
rootCmd.AddCommand(formatCmd)
formatCmd.Flags().BoolVarP(&formatInPlace, "in-place", "i", false, "edit files in place")
formatCmd.Flags().IntVar(&formatIndentSize, "indent", 2, "indentation size in spaces (config: format.indent)")
formatCmd.Flags().BoolVar(&formatUppercase, "uppercase", true, "uppercase SQL keywords (config: format.uppercase_keywords)")
formatCmd.Flags().BoolVar(&formatCompact, "compact", false, "compact format (config: format.compact)")
formatCmd.Flags().BoolVar(&formatCheck, "check", false, "check if files need formatting (CI mode)")
formatCmd.Flags().IntVar(&formatMaxLine, "max-line", 80, "maximum line length (config: format.max_line_length)")
// Add negation flags
formatCmd.Flags().BoolVar(&formatUppercase, "no-uppercase", false, "keep original keyword case")
}