validateFromStdin handles validation from stdin input
(cmd *cobra.Command)
| 198 | |
| 199 | // validateFromStdin handles validation from stdin input |
| 200 | func validateFromStdin(cmd *cobra.Command) error { |
| 201 | // Read from stdin |
| 202 | content, err := ReadFromStdin() |
| 203 | if err != nil { |
| 204 | return fmt.Errorf("failed to read from stdin: %w", err) |
| 205 | } |
| 206 | |
| 207 | // Validate stdin content |
| 208 | if err := ValidateStdinInput(content); err != nil { |
| 209 | return fmt.Errorf("stdin validation failed: %w", err) |
| 210 | } |
| 211 | |
| 212 | // Create a temporary file to leverage existing validation logic |
| 213 | tmpFile, err := os.CreateTemp("", "gosqlx-stdin-*.sql") |
| 214 | if err != nil { |
| 215 | return fmt.Errorf("failed to create temporary file: %w", err) |
| 216 | } |
| 217 | defer func() { _ = os.Remove(tmpFile.Name()) }() |
| 218 | defer func() { _ = tmpFile.Close() }() |
| 219 | |
| 220 | // Write stdin content to temp file |
| 221 | if _, err := tmpFile.Write(content); err != nil { |
| 222 | return fmt.Errorf("failed to write to temporary file: %w", err) |
| 223 | } |
| 224 | if err := tmpFile.Close(); err != nil { |
| 225 | return fmt.Errorf("failed to close temporary file: %w", err) |
| 226 | } |
| 227 | |
| 228 | // Load configuration |
| 229 | cfg, err := config.LoadDefault() |
| 230 | if err != nil { |
| 231 | cfg = config.DefaultConfig() |
| 232 | } |
| 233 | |
| 234 | // Track which flags were explicitly set |
| 235 | flagsChanged := trackChangedFlags(cmd) |
| 236 | |
| 237 | // Create validator options |
| 238 | quietMode := validateQuiet || validateOutputFormat == OutputFormatSARIF || validateOutputFormat == OutputFormatJSON |
| 239 | opts := ValidatorOptionsFromConfig(cfg, flagsChanged, ValidatorFlags{ |
| 240 | Recursive: false, // stdin is always single input |
| 241 | Pattern: "", |
| 242 | Quiet: quietMode, |
| 243 | ShowStats: validateStats && validateOutputFormat == OutputFormatText, // Only show text stats for text output |
| 244 | Dialect: validateDialect, |
| 245 | StrictMode: validateStrict, |
| 246 | Verbose: verbose, |
| 247 | }) |
| 248 | |
| 249 | // Create validator |
| 250 | validator := NewValidator(cmd.OutOrStdout(), cmd.ErrOrStderr(), opts) |
| 251 | |
| 252 | // Validate the temporary file |
| 253 | result, err := validator.Validate([]string{tmpFile.Name()}) |
| 254 | if err != nil { |
| 255 | return err |
| 256 | } |
| 257 |
no test coverage detected