generated from adrienaury/go-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
driver.go
101 lines (84 loc) · 2.65 KB
/
driver.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
// Copyright (C) 2023 CGI France
//
// This file is part of emporte-piece.
//
// Emporte-piece is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Emporte-piece is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with emporte-piece. If not, see <http://www.gnu.org/licenses/>.
package filetree
import (
"fmt"
"io"
"os"
"path"
"github.com/cgi-fr/emporte-piece/pkg/jsonpath"
"github.com/cgi-fr/emporte-piece/pkg/template"
"github.com/rs/zerolog/log"
)
type Driver struct {
fs FileSystem
}
func NewDriver(fsys FileSystem) Driver {
return Driver{
fs: fsys,
}
}
func (d Driver) Develop(templatePath string, targetPath string, contexts ...any) error {
files, _ := d.fs.ReadDir(templatePath)
for _, file := range files {
rs, err := jsonpath.Develop(file.Name(), contexts...)
if err != nil {
return fmt.Errorf("%w", err)
}
for _, devpath := range rs {
subTemplatePath := path.Join(templatePath, file.Name())
subTargetPath := path.Join(targetPath, devpath.Selected)
log.Info().Str("from", subTemplatePath).Msg("generating " + subTargetPath)
if file.IsDir() {
if err := d.developDir(subTargetPath, subTemplatePath, devpath); err != nil {
return err
}
} else {
if err := d.developFile(subTargetPath, subTemplatePath, devpath); err != nil {
return err
}
}
}
}
return nil
}
func (d Driver) developFile(subTargetPath string, subTemplatePath string, devpath jsonpath.ResultString) error {
tmplFile, err := d.fs.Open(subTemplatePath)
if err != nil {
return fmt.Errorf("%w", err)
}
tmplContent, err := io.ReadAll(tmplFile)
if err != nil {
return fmt.Errorf("%w", err)
}
content, err := template.Generate(string(tmplContent), devpath.Stack)
if err != nil {
return fmt.Errorf("%w", err)
}
if err := d.fs.WriteFile(subTargetPath, content, os.ModePerm); err != nil {
return fmt.Errorf("%w", err)
}
return nil
}
func (d Driver) developDir(subTargetPath string, subTemplatePath string, devpath jsonpath.ResultString) error {
if err := d.fs.Mkdir(subTargetPath, os.ModePerm); err != nil && !os.IsExist(err) {
return fmt.Errorf("%w", err)
} else if err := d.Develop(subTemplatePath, subTargetPath, devpath.Stack...); err != nil {
return fmt.Errorf("%w", err)
}
return nil
}