-
Notifications
You must be signed in to change notification settings - Fork 8
/
writer.go
62 lines (49 loc) · 1.14 KB
/
writer.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
package writer
import (
"fmt"
"io/ioutil"
"log"
"os"
"path/filepath"
"github.com/solo-io/skv2/codegen/render"
)
var commentPrefixes = map[string]string{
".go": "//",
".proto": "//",
".js": "//",
".ts": "//",
}
type FileWriter interface {
WriteFiles(files []render.OutFile) error
}
// writes to the filesystem
type DefaultFileWriter struct {
Root string
Header string // prepended to files before write
}
func (w *DefaultFileWriter) WriteFiles(files []render.OutFile) error {
for _, file := range files {
name := filepath.Join(w.Root, file.Path)
content := file.Content
if err := os.MkdirAll(filepath.Dir(name), 0777); err != nil {
return err
}
perm := file.Permission
if perm == 0 {
perm = 0644
}
log.Printf("Writing %v", name)
commentPrefix := commentPrefixes[filepath.Ext(name)]
if commentPrefix == "" {
// set default comment char to "#" as this is the most common
commentPrefix = "#"
}
if w.Header != "" {
content = fmt.Sprintf("%s %s\n\n", commentPrefix, w.Header) + content
}
if err := ioutil.WriteFile(name, []byte(content), perm); err != nil {
return err
}
}
return nil
}