~mna/snow unlisted

snow/pkg/codegen/writer.go -rw-r--r-- 638 bytes
424066c5Martin Angers doc: v0.0.5 1 year, 6 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
package codegen

import (
	"bufio"
	goast "go/ast"
	"go/format"
	"go/token"
	"io/ioutil"
	"path/filepath"
)

// The writer renders a given Go file to a file.
type writer struct {
	dir  string
	fset *token.FileSet
}

func (w *writer) writeFile(filename string, fileNode *goast.File) (string, error) {
	base := filepath.Base(filename)
	f, err := ioutil.TempFile(w.dir, base+"*.go")
	if err != nil {
		return "", err
	}
	defer f.Close()

	bw := bufio.NewWriter(f)
	if err := format.Node(bw, w.fset, fileNode); err != nil {
		return f.Name(), err
	}
	if err := bw.Flush(); err != nil {
		return f.Name(), err
	}
	return f.Name(), f.Close()
}