-
Notifications
You must be signed in to change notification settings - Fork 15
/
gen.go
47 lines (40 loc) · 929 Bytes
/
gen.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
package gg
import (
"fmt"
"io"
"os"
)
type Generator struct {
g *group
}
// New will create a new generator which hold the group reference.
func New() *Generator {
return &Generator{g: NewGroup()}
}
func (g *Generator) NewGroup() (ng *group) {
ng = NewGroup()
g.g.append(ng)
return ng
}
// Write will write the group into the given writer.
func (g *Generator) Write(w io.Writer) {
g.g.render(w)
}
// WriteFile will write the group into the given path.
func (g *Generator) WriteFile(path string) error {
file, err := os.Create(path)
if err != nil {
return fmt.Errorf("create file %s: %s", path, err)
}
g.g.render(file)
return nil
}
// AppendFile will append the group after the give path.
func (g *Generator) AppendFile(path string) error {
file, err := os.OpenFile(path, os.O_APPEND|os.O_RDWR, 0644)
if err != nil {
return fmt.Errorf("create file %s: %s", path, err)
}
g.g.render(file)
return nil
}