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

Method formatFile

cmd/gosqlx/cmd/formatter.go:168–202  ·  view source on GitHub ↗

formatFile formats a single SQL file

(filename string)

Source from the content-addressed store, hash-verified

166
167// formatFile formats a single SQL file
168func (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
205func (f *Formatter) formatSQL(sql string) (string, error) {

Callers 4

TestFormatter_FormatFileFunction · 0.95
FormatMethod · 0.95
formatFileMethod · 0.95

Calls 3

formatSQLMethod · 0.95
ValidateFileAccessFunction · 0.70
ErrorfMethod · 0.65

Tested by 2

TestFormatter_FormatFileFunction · 0.76