/
item.go
48 lines (44 loc) · 934 Bytes
/
item.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
47
48
package dummy
import (
"go/format"
"io"
"path/filepath"
"strings"
"gopkg.in/src-d/go-billy.v4"
)
type Item interface {
create(dir, name string, fs billy.Filesystem)
}
type Src string
type Dir map[string]Item
func (s Src) create(dir, name string, fs billy.Filesystem) {
src := string(s)
if strings.HasSuffix(name, ".go") {
// if it's a go file, format first
b, err := format.Source([]byte(s))
if err != nil {
panic(err)
}
src = string(b)
}
if err := fs.MkdirAll(dir, 0777); err != nil {
panic(err)
}
f, err := fs.Create(filepath.Join(dir, name))
if err != nil {
panic(err)
}
defer f.Close()
if _, err := io.Copy(f, strings.NewReader(src)); err != nil {
panic(err)
}
}
func (d Dir) create(dir, name string, fs billy.Filesystem) {
dpath := filepath.Join(dir, name)
if err := fs.MkdirAll(dpath, 0777); err != nil {
panic(err)
}
for name, item := range d {
item.create(dpath, name, fs)
}
}