| 40 | } |
| 41 | |
| 42 | func (a *Base) Format(filename string, writer io.Writer, file *ast.File) error { |
| 43 | fileSet := a.FileSet |
| 44 | if fileSet == nil { |
| 45 | fileSet = token.NewFileSet() |
| 46 | } |
| 47 | if writer == nil { |
| 48 | open, err := os.OpenFile(filename, os.O_WRONLY|os.O_TRUNC, 0666) |
| 49 | defer open.Close() |
| 50 | if err != nil { |
| 51 | return errors.Wrapf(err, "[filepath:%s]打开文件失败!", filename) |
| 52 | } |
| 53 | writer = open |
| 54 | } |
| 55 | err := format.Node(writer, fileSet, file) |
| 56 | if err != nil { |
| 57 | return errors.Wrapf(err, "[filepath:%s]注入失败!", filename) |
| 58 | } |
| 59 | return nil |
| 60 | } |
| 61 | |
| 62 | // RelativePath 绝对路径转相对路径 |
| 63 | func (a *Base) RelativePath(filePath string) string { |