-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexporter.go
More file actions
73 lines (65 loc) · 1.54 KB
/
exporter.go
File metadata and controls
73 lines (65 loc) · 1.54 KB
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package main
import (
"bytes"
"io/ioutil"
"os"
"path/filepath"
)
type Exporter interface {
Export(node StructureNode) error
}
func NewExporter(b Bedrock) Exporter {
if b.OutputsFiles() {
return &fileExporter{b}
}
return &stdoutExporter{b}
}
type stdoutExporter struct {
bedrock Bedrock
}
func (e *stdoutExporter) Export(node StructureNode) error {
conf := e.bedrock.Config
generator, err := NewStructGenerator(conf.StructureTemplate, conf.ChildStructuresNesting, conf.TypeTranslateMap)
if err != nil {
return err
}
str, err := generator.Generate(node)
if err != nil {
return err
}
_, err = os.Stdout.WriteString(str)
return err
}
type fileExporter struct {
bedrock Bedrock
}
func (e *fileExporter) Export(node StructureNode) error {
conf := e.bedrock.Config
generator, err := NewStructGenerator(conf.StructureTemplate, conf.ChildStructuresNesting, conf.TypeTranslateMap)
if err != nil {
return err
}
str, err := generator.Generate(node)
if err != nil {
return err
}
if err := os.MkdirAll(e.bedrock.OutputDirPath, os.ModePerm); err != nil {
return err
}
filename, err := e.getFileName(node)
if err != nil {
return err
}
return ioutil.WriteFile(filepath.Join(e.bedrock.OutputDirPath, filename), []byte(str), os.ModePerm)
}
func (e *fileExporter) getFileName(node StructureNode) (string, error) {
tmpl, err := NewTemplate(node.Name).Parse(e.bedrock.Config.OutputFilename)
if err != nil {
return "", err
}
var buf bytes.Buffer
if err := tmpl.Execute(&buf, node); err != nil {
return "", err
}
return buf.String(), nil
}