MCPcopy Create free account
hub / github.com/bracesdev/errtrace / processFile

Method processFile

cmd/errtrace/main.go:365–405  ·  view source on GitHub ↗

processFile processes a single file. This operates in two phases: First, it walks the AST to find all the places that need to be modified, extracting other information as needed. The collected information is used to pick a package name, whether we need an import, etc. and *then* the edits are appl

(r fileRequest)

Source from the content-addressed store, hash-verified

363// The collected information is used to pick a package name,
364// whether we need an import, etc. and *then* the edits are applied.
365func (cmd *mainCmd) processFile(r fileRequest) error {
366 src, err := cmd.readFile(r)
367 if err != nil {
368 return errtrace.Wrap(err)
369 }
370
371 parsed, err := cmd.parseFile(r.Filename, src, r.RewriteOpts)
372 if err != nil {
373 return errtrace.Wrap(err)
374 }
375
376 for _, line := range parsed.unusedOptouts {
377 cmd.log.Printf("%s:%d:unused errtrace:skip", r.Filename, line)
378 }
379 if r.List {
380 if len(parsed.inserts) > 0 {
381 _, err = fmt.Fprintf(cmd.Stdout, "%s\n", r.Filename)
382 }
383 return errtrace.Wrap(err)
384 }
385
386 var out bytes.Buffer
387 if err := cmd.rewriteFile(parsed, &out); err != nil {
388 return errtrace.Wrap(err)
389 }
390
391 outSrc := out.Bytes()
392 if r.Format {
393 outSrc, err = gofmt.Source(outSrc)
394 if err != nil {
395 return errtrace.Wrap(fmt.Errorf("format: %w", err))
396 }
397 }
398
399 if r.Write {
400 err = os.WriteFile(r.Filename, outSrc, 0o644)
401 } else {
402 _, err = cmd.Stdout.Write(outSrc)
403 }
404 return errtrace.Wrap(err)
405}
406
407type parsedFile struct {
408 src []byte

Callers 1

RunMethod · 0.95

Calls 5

readFileMethod · 0.95
parseFileMethod · 0.95
rewriteFileMethod · 0.95
WrapFunction · 0.92
WriteMethod · 0.80

Tested by

no test coverage detected