-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtemplator.go
209 lines (198 loc) · 5.33 KB
/
templator.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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
// Copyright © 2019 Jose Riguera <[email protected]>
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
package actions
import (
"bytes"
"fmt"
"os"
"path/filepath"
"strings"
"text/template"
fs "confinit/pkg/fs"
log "confinit/pkg/log"
tfunc "confinit/pkg/tplfunctions"
)
type Templator struct {
*Replicator
Data interface{}
Env map[string]string
SkipExt bool
}
func NewTemplator(glob, dst string, force, skipext bool, excludes []string) (*Templator, error) {
rpc, err := NewReplicator(glob, dst, fs.FsItemFile, force, excludes)
if err != nil {
return nil, err
}
env := make(map[string]string)
for _, setting := range os.Environ() {
pair := strings.SplitN(setting, "=", 2)
env[pair[0]] = pair[1]
}
r := Templator{
Replicator: rpc,
Data: nil,
Env: env,
SkipExt: skipext,
}
return &r, nil
}
func (ft *Templator) AddEnv(env map[string]string) {
for key, value := range env {
ft.Env[key] = value
}
}
func (ft *Templator) AddData(data interface{}) (err error) {
// convert to map[string]interface{} if
// input is map[interface{}]interface{}
switch y := data.(type) {
case map[interface{}]interface{}:
m := map[string]interface{}{}
for k, v := range y {
switch k2 := k.(type) {
case string:
m[k2] = v
default:
m[fmt.Sprint(k)] = v
}
}
data = m
}
if ft.Data == nil {
// non initialized
ft.Data = data
return
}
switch ft.Data.(type) {
case []interface{}:
// both are list, append
ft.Data = append(ft.Data.([]interface{}), data)
case map[string]interface{}:
switch y := data.(type) {
case map[string]interface{}:
// both are maps
for k, v := range y {
ft.Data.(map[string]interface{})[k] = v
}
default:
// current ft.Data is a map
// and new data is a not a map
err = fmt.Errorf("Cannot add/mix Data source type Map with other Data source(s)")
}
default:
err = fmt.Errorf("Cannot add/mix Data sources with different types")
}
return
}
type TemplateData struct {
IsDir bool
Mode string
SourceBaseDir string
Source string
Filename string
SourceFile string
Path string
SourceFullPath string
SourceAbsPath string
SourcePath string
Ext string
DstBaseDir string
Destination string
DestinationPath string
Data interface{}
Env map[string]string
}
func (ft *Templator) NewTemplateData(basedir, f string, i os.FileMode) *TemplateData {
dstf := f
if ft.SkipExt && !i.IsDir() {
dstf = strings.TrimSuffix(f, filepath.Ext(f))
}
dir, err := os.Getwd()
if err != nil {
dir = ""
}
abspath := filepath.Join(dir, basedir, f)
fullpath := filepath.Join(basedir, f)
dstpath := filepath.Join(ft.DstPath, dstf)
data := TemplateData{
IsDir: i.IsDir(),
Mode: i.String(),
SourceFile: f,
SourceBaseDir: basedir,
Source: filepath.Base(f),
Filename: filepath.Base(dstf),
Ext: filepath.Ext(filepath.Base(dstf)),
SourceFullPath: fullpath,
SourceAbsPath: abspath,
SourcePath: filepath.Dir(fullpath),
DstBaseDir: ft.DstPath,
Destination: dstpath,
DestinationPath: filepath.Dir(dstpath),
Env: ft.Env,
Data: ft.Data,
}
return &data
}
func (ft *Templator) renderTemplateString(name, value string, data *TemplateData) (string, error) {
// A Buffer needs no initialization.
var render bytes.Buffer
tpl, err := template.New(name).Funcs(tfunc.TemplateFuncMap()).Parse(value)
if err != nil {
return "", err
}
if err := tpl.Execute(&render, data); err != nil {
return "", err
}
return render.String(), nil
}
func (ft *Templator) renderTemplate(data *TemplateData, dirmode, filemode os.FileMode) error {
if err := ft.mkdir(filepath.Dir(data.Destination), dirmode); err != nil {
return err
}
if err := os.Truncate(data.Destination, 0); err != nil {
return err
}
tpl, err := template.New(data.Source).Funcs(tfunc.TemplateFuncMap()).ParseFiles(data.SourceFullPath)
if err != nil {
return err
}
if ft.FileMode != 0 {
filemode = ft.FileMode
}
dst, err := os.OpenFile(data.Destination, os.O_TRUNC|os.O_CREATE|os.O_WRONLY, filemode)
if err != nil {
err = fmt.Errorf("Cannot create file %s, %s", data.Destination, err)
return err
}
defer dst.Close()
if err := tpl.Execute(dst, data); err != nil {
return err
}
log.Debugf("Successfully rendered template '%s' to '%s'", data.SourceFullPath, data.Destination)
return nil
}
func (ft *Templator) Function(base string, path string, i os.FileMode) (dst string, err error) {
if i.IsDir() {
// Using always default mode (is not replicate)
err = ft.mkdir(filepath.Join(ft.DstPath, path), i)
} else {
tpldata := ft.NewTemplateData(base, path, i)
dst = tpldata.Destination
err = ft.renderTemplate(tpldata, os.FileMode(0755), i)
if err == nil {
err = ft.applyPermissions(dst)
}
}
return
}