formatFromStdin handles formatting from stdin input
(cmd *cobra.Command)
| 125 | |
| 126 | // formatFromStdin handles formatting from stdin input |
| 127 | func formatFromStdin(cmd *cobra.Command) error { |
| 128 | // Read from stdin |
| 129 | content, err := ReadFromStdin() |
| 130 | if err != nil { |
| 131 | return fmt.Errorf("failed to read from stdin: %w", err) |
| 132 | } |
| 133 | |
| 134 | // Validate stdin content |
| 135 | if err := ValidateStdinInput(content); err != nil { |
| 136 | return fmt.Errorf("stdin validation failed: %w", err) |
| 137 | } |
| 138 | |
| 139 | // Note: in-place mode is not supported for stdin (would be no-op) |
| 140 | if formatInPlace { |
| 141 | return fmt.Errorf("in-place mode (-i) is not supported with stdin input") |
| 142 | } |
| 143 | |
| 144 | // Load configuration |
| 145 | cfg, err := config.LoadDefault() |
| 146 | if err != nil { |
| 147 | cfg = config.DefaultConfig() |
| 148 | } |
| 149 | |
| 150 | // Track which flags were explicitly set |
| 151 | flagsChanged := trackChangedFlags(cmd) |
| 152 | |
| 153 | // Create formatter options |
| 154 | opts := FormatterOptionsFromConfig(cfg, flagsChanged, FormatterFlags{ |
| 155 | InPlace: false, // always false for stdin |
| 156 | IndentSize: formatIndentSize, |
| 157 | Uppercase: formatUppercase, |
| 158 | Compact: formatCompact, |
| 159 | Check: formatCheck, |
| 160 | MaxLine: formatMaxLine, |
| 161 | Verbose: verbose, |
| 162 | Output: outputFile, |
| 163 | }) |
| 164 | |
| 165 | // Create formatter |
| 166 | formatter := NewFormatter(cmd.OutOrStdout(), cmd.ErrOrStderr(), opts) |
| 167 | |
| 168 | // Format the SQL content using the internal formatSQL method |
| 169 | formattedSQL, err := formatter.formatSQL(string(content)) |
| 170 | if err != nil { |
| 171 | return fmt.Errorf("formatting failed: %w", err) |
| 172 | } |
| 173 | |
| 174 | // In check mode, compare original and formatted |
| 175 | if formatCheck { |
| 176 | if string(content) != formattedSQL { |
| 177 | fmt.Fprintf(cmd.ErrOrStderr(), "stdin needs formatting\n") |
| 178 | os.Exit(1) |
| 179 | } |
| 180 | |
| 181 | if verbose { |
| 182 | fmt.Fprintf(cmd.OutOrStdout(), "stdin is properly formatted\n") |
| 183 | } |
| 184 | return nil |
no test coverage detected