-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathparser.go
68 lines (63 loc) · 1.74 KB
/
parser.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
63
64
65
66
67
68
// Copyright (c) 2023 Tiago Melo. All rights reserved.
// Use of this source code is governed by the MIT License that can be found in
// the LICENSE file.
package parser
import (
"html/template"
"io"
"os"
"github.com/pkg/errors"
"gopkg.in/yaml.v2"
)
// For ease of unit testing.
var (
parseFile = template.ParseFiles
openFile = os.Open
createFile = os.Create
ioReadAll = io.ReadAll
yamlUnmarshal = yaml.Unmarshal
executeTemplateFile = func(templateFile *template.Template, wr io.Writer, data any) error {
return templateFile.Execute(wr, data)
}
)
// valuesFromYamlFile extracts values from yaml file.
func valuesFromYamlFile(dataFile string) (map[string]interface{}, error) {
data, err := openFile(dataFile)
if err != nil {
return nil, errors.Wrap(err, "opening data file")
}
defer data.Close()
s, err := ioReadAll(data)
if err != nil {
return nil, errors.Wrap(err, "reading data file")
}
var values map[string]interface{}
err = yamlUnmarshal(s, &values)
if err != nil {
return nil, errors.Wrap(err, "unmarshalling yaml file")
}
return values, nil
}
// Parse replaces values present in the template file
// with values defined in the data file, saving the result
// as an output file.
func Parse(templateFile, dataFile, outputFile string) error {
tmpl, err := parseFile(templateFile)
if err != nil {
return errors.Wrap(err, "parsing template file")
}
values, err := valuesFromYamlFile(dataFile)
if err != nil {
return err
}
output, err := createFile(outputFile)
if err != nil {
return errors.Wrap(err, "creating output file")
}
defer output.Close()
err = executeTemplateFile(tmpl, output, values)
if err != nil {
return errors.Wrap(err, "executing template file")
}
return nil
}