(cmd *cobra.Command, args []string)
| 62 | } |
| 63 | |
| 64 | func formatRun(cmd *cobra.Command, args []string) error { |
| 65 | // Handle stdin input |
| 66 | if ShouldReadFromStdin(args) { |
| 67 | return formatFromStdin(cmd) |
| 68 | } |
| 69 | |
| 70 | // Validate that we have file arguments if not using stdin |
| 71 | if len(args) == 0 { |
| 72 | return fmt.Errorf("no input provided: specify file paths or pipe SQL via stdin") |
| 73 | } |
| 74 | |
| 75 | // If single argument that looks like inline SQL (not a file), format it directly |
| 76 | if len(args) == 1 { |
| 77 | if _, err := os.Stat(args[0]); err != nil && looksLikeSQL(args[0]) { |
| 78 | return formatInlineSQL(cmd, args[0]) |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | // Load configuration with CLI flag overrides |
| 83 | cfg, err := config.LoadDefault() |
| 84 | if err != nil { |
| 85 | // If config load fails, use defaults |
| 86 | cfg = config.DefaultConfig() |
| 87 | } |
| 88 | |
| 89 | // Track which flags were explicitly set |
| 90 | flagsChanged := trackChangedFlags(cmd) |
| 91 | |
| 92 | // Create formatter options from config and flags |
| 93 | opts := FormatterOptionsFromConfig(cfg, flagsChanged, FormatterFlags{ |
| 94 | InPlace: formatInPlace, |
| 95 | IndentSize: formatIndentSize, |
| 96 | Uppercase: formatUppercase, |
| 97 | Compact: formatCompact, |
| 98 | Check: formatCheck, |
| 99 | MaxLine: formatMaxLine, |
| 100 | Verbose: verbose, |
| 101 | Output: outputFile, |
| 102 | }) |
| 103 | |
| 104 | // Create formatter with injectable output writers |
| 105 | formatter := NewFormatter(cmd.OutOrStdout(), cmd.ErrOrStderr(), opts) |
| 106 | |
| 107 | // Run formatting |
| 108 | result, err := formatter.Format(args) |
| 109 | if err != nil { |
| 110 | return err |
| 111 | } |
| 112 | |
| 113 | // Exit with error code if files need formatting in check mode |
| 114 | if len(result.NeedsFormatting) > 0 { |
| 115 | os.Exit(1) |
| 116 | } |
| 117 | |
| 118 | // Exit with error code if any files failed to format |
| 119 | if result.FailedFiles > 0 { |
| 120 | return fmt.Errorf("%d file(s) failed to format", result.FailedFiles) |
| 121 | } |
nothing calls this directly
no test coverage detected