From bf0f72a533d5c1868b9819f769836ea22d5fa583 Mon Sep 17 00:00:00 2001 From: Leszek CimaƂa Date: Wed, 8 Jan 2020 21:44:18 +0100 Subject: template: add exec and wrap --- lib/templates/template.go | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) (limited to 'lib/templates/template.go') diff --git a/lib/templates/template.go b/lib/templates/template.go index 5402472..f2765e8 100644 --- a/lib/templates/template.go +++ b/lib/templates/template.go @@ -5,6 +5,7 @@ import ( "errors" "net/mail" "os" + "os/exec" "path" "strings" "text/template" @@ -72,6 +73,11 @@ func parseAddressList(list string) []*mail.Address { return addrs } +// wrap allows to chain wrapText +func wrap(lineWidth int, text string) string { + return wrapText(text, lineWidth) +} + func wrapLine(text string, lineWidth int) string { words := strings.Fields(text) if len(words) == 0 { @@ -135,10 +141,27 @@ func quote(text string) string { return quoted.String() } +// cmd allow to parse reply by shell command +// text have to be passed by cmd param +// if there is error, original string is returned +func cmd(cmd, text string) string { + var out bytes.Buffer + c := exec.Command("sh", "-c", cmd) + c.Stdin = strings.NewReader(text) + c.Stdout = &out + err := c.Run() + if err != nil { + return text + } + return out.String() +} + var templateFuncs = template.FuncMap{ "quote": quote, "wrapText": wrapText, + "wrap": wrap, "dateFormat": time.Time.Format, + "exec": cmd, } func findTemplate(templateName string, templateDirs []string) (string, error) { -- cgit 1.4.1-2-gfad0 b96be23ca2d66c65a63a5b91154c1ccc56b5'>diff stats
path: root/doc/uml/134530
blob: 9f91d1049bc2053dbcaca66e6bccd11d87c56b73 (plain) (blame)
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