formatFile formats a single SQL file
(filename string)
| 166 | |
| 167 | // formatFile formats a single SQL file |
| 168 | func (f *Formatter) formatFile(filename string) FileFormatterResult { |
| 169 | result := FileFormatterResult{ |
| 170 | Path: filename, |
| 171 | } |
| 172 | |
| 173 | // Use security validation first |
| 174 | if err := ValidateFileAccess(filename); err != nil { |
| 175 | result.Error = fmt.Errorf("file access validation failed: %w", err) |
| 176 | return result |
| 177 | } |
| 178 | |
| 179 | // G304: Path is validated by ValidateFileAccess above |
| 180 | data, err := os.ReadFile(filename) // #nosec G304 |
| 181 | if err != nil { |
| 182 | result.Error = fmt.Errorf("failed to read file: %w", err) |
| 183 | return result |
| 184 | } |
| 185 | |
| 186 | original := string(data) |
| 187 | if len(data) == 0 { |
| 188 | result.Formatted = original |
| 189 | result.Changed = false |
| 190 | return result |
| 191 | } |
| 192 | |
| 193 | formatted, err := f.formatSQL(original) |
| 194 | if err != nil { |
| 195 | result.Error = err |
| 196 | return result |
| 197 | } |
| 198 | |
| 199 | result.Formatted = formatted |
| 200 | result.Changed = (original != formatted) |
| 201 | return result |
| 202 | } |
| 203 | |
| 204 | // formatSQL formats a SQL string |
| 205 | func (f *Formatter) formatSQL(sql string) (string, error) { |