forked from garetht/amanar
-
Notifications
You must be signed in to change notification settings - Fork 0
/
template_source.go
45 lines (36 loc) · 965 Bytes
/
template_source.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
package amanar
import (
"fmt"
"io"
"text/template"
)
type TemplateSource struct {
t *template.Template
writer io.Writer
}
func (t TemplateSource) WriteToDisk(credentials Credentials) error {
return t.t.Execute(t.writer, credentials)
}
func (t TemplateSource) WriteToDiskWithoutContext() error {
return t.t.Execute(t.writer, nil)
}
func NewTemplateSourceFromFile(templateFilepath *string, writer io.Writer) (*TemplateSource, error) {
t, err := template.ParseFiles(*templateFilepath)
if err != nil {
return nil, fmt.Errorf("could not parse template file: %w", err)
}
return &TemplateSource{
t: t,
writer: writer,
}, nil
}
func NewTemplateSourceFromString(templateString *string, writer io.Writer) (*TemplateSource, error) {
t, err := template.New("template_string").Parse(*templateString)
if err != nil {
return nil, fmt.Errorf("could not parse template string: %w", err)
}
return &TemplateSource{
t: t,
writer: writer,
}, nil
}