-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathrunner_render.go
101 lines (92 loc) · 2.27 KB
/
runner_render.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
package main
import (
"bytes"
"context"
"fmt"
"github.com/acicn/minit/pkg/mlog"
"github.com/acicn/minit/pkg/tmplfuncs"
"io/ioutil"
"os"
"path/filepath"
"strings"
"text/template"
)
const KindRender = "render"
type RenderRunner struct {
Unit
logger *mlog.Logger
}
func (r *RenderRunner) Run(ctx context.Context) {
r.logger.Printf("控制器启动")
defer r.logger.Printf("控制器退出")
env := environ()
for _, filePattern := range r.Files {
var err error
var names []string
if names, err = filepath.Glob(filePattern); err != nil {
r.logger.Errorf("匹配表达式 %s 格式错误: %s", filePattern, err.Error())
continue
}
for _, name := range names {
var buf []byte
if buf, err = ioutil.ReadFile(name); err != nil {
r.logger.Errorf("无法读取文件: %s", name)
continue
}
tmpl := template.New("__main__").Funcs(tmplfuncs.Funcs).Option("missingkey=zero")
if tmpl, err = tmpl.Parse(string(buf)); err != nil {
r.logger.Errorf("无法解析文件 %s: %s", name, err.Error())
continue
}
out := &bytes.Buffer{}
if err = tmpl.Execute(out, map[string]interface{}{
"Env": env,
}); err != nil {
r.logger.Errorf("无法渲染文件 %s: %s", name, err.Error())
continue
}
content := out.Bytes()
if !r.Raw {
content = sanitize(content)
}
if err = ioutil.WriteFile(name, content, 0755); err != nil {
r.logger.Errorf("无法写入文件 %s: %s", name, err.Error())
continue
}
r.logger.Printf("文件渲染完成: %s", name)
}
}
}
func NewRenderRunner(unit Unit, logger *mlog.Logger) (Runner, error) {
if len(unit.Files) == 0 {
return nil, fmt.Errorf("没有指定文件,检查 files 字段")
}
return &RenderRunner{
Unit: unit,
logger: logger,
}, nil
}
func environ() map[string]string {
out := make(map[string]string)
envs := os.Environ()
for _, entry := range envs {
splits := strings.SplitN(entry, "=", 2)
if len(splits) == 2 {
out[strings.TrimSpace(splits[0])] = strings.TrimSpace(splits[1])
}
}
return out
}
func sanitize(s []byte) []byte {
lines := bytes.Split(s, []byte("\n"))
out := &bytes.Buffer{}
for _, line := range lines {
line = bytes.TrimSpace(line)
if len(line) == 0 {
continue
}
out.Write(line)
out.WriteRune('\n')
}
return out.Bytes()
}