|
1 | 1 | package cmd |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "fmt" |
4 | 5 | "os" |
5 | 6 |
|
6 | 7 | "github.com/spf13/cobra" |
@@ -34,12 +35,29 @@ Examples: |
34 | 35 | gosqlx format "*.sql" # Format all SQL files |
35 | 36 | gosqlx format -o formatted.sql query.sql # Save to specific file |
36 | 37 |
|
| 38 | +Pipeline/Stdin Examples: |
| 39 | + echo "SELECT * FROM users" | gosqlx format # Format from stdin (auto-detect) |
| 40 | + cat query.sql | gosqlx format # Pipe file contents |
| 41 | + gosqlx format - # Explicit stdin marker |
| 42 | + gosqlx format < query.sql # Input redirection |
| 43 | + cat query.sql | gosqlx format > formatted.sql # Full pipeline |
| 44 | +
|
37 | 45 | Performance: 100x faster than SQLFluff for equivalent operations`, |
38 | | - Args: cobra.MinimumNArgs(1), |
| 46 | + Args: cobra.MinimumNArgs(0), // Changed to allow stdin with no args |
39 | 47 | RunE: formatRun, |
40 | 48 | } |
41 | 49 |
|
42 | 50 | func formatRun(cmd *cobra.Command, args []string) error { |
| 51 | + // Handle stdin input |
| 52 | + if ShouldReadFromStdin(args) { |
| 53 | + return formatFromStdin(cmd) |
| 54 | + } |
| 55 | + |
| 56 | + // Validate that we have file arguments if not using stdin |
| 57 | + if len(args) == 0 { |
| 58 | + return fmt.Errorf("no input provided: specify file paths or pipe SQL via stdin") |
| 59 | + } |
| 60 | + |
43 | 61 | // Load configuration with CLI flag overrides |
44 | 62 | cfg, err := config.LoadDefault() |
45 | 63 | if err != nil { |
@@ -87,6 +105,83 @@ func formatRun(cmd *cobra.Command, args []string) error { |
87 | 105 | return nil |
88 | 106 | } |
89 | 107 |
|
| 108 | +// formatFromStdin handles formatting from stdin input |
| 109 | +func formatFromStdin(cmd *cobra.Command) error { |
| 110 | + // Read from stdin |
| 111 | + content, err := ReadFromStdin() |
| 112 | + if err != nil { |
| 113 | + return fmt.Errorf("failed to read from stdin: %w", err) |
| 114 | + } |
| 115 | + |
| 116 | + // Validate stdin content |
| 117 | + if err := ValidateStdinInput(content); err != nil { |
| 118 | + return fmt.Errorf("stdin validation failed: %w", err) |
| 119 | + } |
| 120 | + |
| 121 | + // Note: in-place mode is not supported for stdin (would be no-op) |
| 122 | + if formatInPlace { |
| 123 | + return fmt.Errorf("in-place mode (-i) is not supported with stdin input") |
| 124 | + } |
| 125 | + |
| 126 | + // Load configuration |
| 127 | + cfg, err := config.LoadDefault() |
| 128 | + if err != nil { |
| 129 | + cfg = config.DefaultConfig() |
| 130 | + } |
| 131 | + |
| 132 | + // Track which flags were explicitly set |
| 133 | + flagsChanged := make(map[string]bool) |
| 134 | + cmd.Flags().Visit(func(f *pflag.Flag) { |
| 135 | + flagsChanged[f.Name] = true |
| 136 | + }) |
| 137 | + if cmd.Parent() != nil && cmd.Parent().PersistentFlags() != nil { |
| 138 | + cmd.Parent().PersistentFlags().Visit(func(f *pflag.Flag) { |
| 139 | + flagsChanged[f.Name] = true |
| 140 | + }) |
| 141 | + } |
| 142 | + |
| 143 | + // Create formatter options |
| 144 | + opts := FormatterOptionsFromConfig(cfg, flagsChanged, FormatterFlags{ |
| 145 | + InPlace: false, // always false for stdin |
| 146 | + IndentSize: formatIndentSize, |
| 147 | + Uppercase: formatUppercase, |
| 148 | + Compact: formatCompact, |
| 149 | + Check: formatCheck, |
| 150 | + MaxLine: formatMaxLine, |
| 151 | + Verbose: verbose, |
| 152 | + Output: outputFile, |
| 153 | + }) |
| 154 | + |
| 155 | + // Create formatter |
| 156 | + formatter := NewFormatter(cmd.OutOrStdout(), cmd.ErrOrStderr(), opts) |
| 157 | + |
| 158 | + // Format the SQL content using the internal formatSQL method |
| 159 | + formattedSQL, err := formatter.formatSQL(string(content)) |
| 160 | + if err != nil { |
| 161 | + return fmt.Errorf("formatting failed: %w", err) |
| 162 | + } |
| 163 | + |
| 164 | + // In check mode, compare original and formatted |
| 165 | + if formatCheck { |
| 166 | + if string(content) != formattedSQL { |
| 167 | + fmt.Fprintf(cmd.ErrOrStderr(), "stdin needs formatting\n") |
| 168 | + os.Exit(1) |
| 169 | + } else { |
| 170 | + if verbose { |
| 171 | + fmt.Fprintf(cmd.OutOrStdout(), "stdin is properly formatted\n") |
| 172 | + } |
| 173 | + } |
| 174 | + return nil |
| 175 | + } |
| 176 | + |
| 177 | + // Write formatted output |
| 178 | + if err := WriteOutput([]byte(formattedSQL), outputFile, cmd.OutOrStdout()); err != nil { |
| 179 | + return err |
| 180 | + } |
| 181 | + |
| 182 | + return nil |
| 183 | +} |
| 184 | + |
90 | 185 | func init() { |
91 | 186 | rootCmd.AddCommand(formatCmd) |
92 | 187 |
|
|
0 commit comments