MCPcopy Create free account
hub / github.com/ajitpratap0/GoSQLX / formatFromStdin

Function formatFromStdin

cmd/gosqlx/cmd/format.go:127–196  ·  view source on GitHub ↗

formatFromStdin handles formatting from stdin input

(cmd *cobra.Command)

Source from the content-addressed store, hash-verified

125
126// formatFromStdin handles formatting from stdin input
127func 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

Callers 1

formatRunFunction · 0.85

Calls 10

formatSQLMethod · 0.95
LoadDefaultFunction · 0.92
DefaultConfigFunction · 0.92
trackChangedFlagsFunction · 0.85
ReadFromStdinFunction · 0.70
ValidateStdinInputFunction · 0.70
NewFormatterFunction · 0.70
WriteOutputFunction · 0.70
ErrorfMethod · 0.65

Tested by

no test coverage detected