forked from dave/dst
-
Notifications
You must be signed in to change notification settings - Fork 0
/
util_test.go
46 lines (42 loc) · 909 Bytes
/
util_test.go
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
35
36
37
38
39
40
41
42
43
44
45
46
package dst_test
import (
"fmt"
"go/format"
"io/ioutil"
"os"
"path/filepath"
"strings"
)
func tempDir(m map[string]string) (dir string, err error) {
if dir, err = ioutil.TempDir("", ""); err != nil {
return
}
for fpathrel, src := range m {
if strings.HasSuffix(fpathrel, "/") {
// just a dir
if err = os.MkdirAll(filepath.Join(dir, fpathrel), 0777); err != nil {
return
}
} else {
fpath := filepath.Join(dir, fpathrel)
fdir, _ := filepath.Split(fpath)
if err = os.MkdirAll(fdir, 0777); err != nil {
return
}
var formatted []byte
if strings.HasSuffix(fpath, ".go") {
formatted, err = format.Source([]byte(src))
if err != nil {
err = fmt.Errorf("formatting %s: %v", fpathrel, err)
return
}
} else {
formatted = []byte(src)
}
if err = ioutil.WriteFile(fpath, formatted, 0666); err != nil {
return
}
}
}
return
}