-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.go
337 lines (256 loc) · 6.47 KB
/
util.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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
package apicupcfg
import (
"encoding/json"
"fmt"
rice "github.com/GeertJohan/go.rice"
"github.com/Masterminds/sprig"
"io/ioutil"
"log"
"os"
"path/filepath"
"strings"
"text/template"
)
func bytesInBox(file string, tbox *rice.Box) []byte {
if tbox == nil {
log.Fatal("uninitialized template box")
}
c, err := tbox.Bytes(file)
if err != nil {
log.Fatal(err)
}
return c
}
func fileName(dir string, file string) string {
dir2 := dir
if len(dir2) == 0 {
dir2 = "."
}
return dir2 + string(os.PathSeparator) + file
}
func isFileExist(file string) (bool, error) {
_, err := os.Stat(file)
if os.IsExist(err) {
return true, nil
} else if os.IsNotExist(err) {
return false, nil
}
// pretend file exists, return error
return true, err
}
func openFile(file string) (*os.File, error) {
return os.OpenFile(file, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0666)
}
func openFileAppend(file string) (*os.File, error) {
return os.OpenFile(file, os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0666)
}
func readFileBytes(file string) []byte {
c, err := ioutil.ReadFile(file)
if err != nil {
log.Fatal(err)
}
return c
}
func writeFileBytes(file string, bytes []byte) {
err := ioutil.WriteFile(file, bytes, os.ModePerm)
if err != nil {
log.Fatal(err)
}
}
func CreateOutputDirectories(outdir, commonCsrSubdir, customCsrSubdir, sharedCsrSubdir, projectSubdir, datapowerSubdir string) error {
basedir, err := filepath.Abs(outdir)
if err != nil {
return err
}
if err = os.MkdirAll(basedir, os.ModePerm); err != nil {
return err
}
commonCsrDir := concatSubdir(basedir, commonCsrSubdir)
if err = os.MkdirAll(commonCsrDir, os.ModePerm); err != nil {
return err
}
customCsrDir := concatSubdir(basedir, customCsrSubdir)
if err = os.MkdirAll(customCsrDir, os.ModePerm); err != nil {
return err
}
//sharedCsrDir := concatSubdir(basedir, sharedCsrSubdir)
//if err = os.MkdirAll(sharedCsrDir, os.ModePerm); err != nil {
// return err
//}
projectDir := concatSubdir(basedir, projectSubdir)
if err = os.MkdirAll(projectDir, os.ModePerm); err != nil {
return err
}
// bin subdirectory
binDir := concatSubdir(basedir, "bin")
if err = os.MkdirAll(binDir, os.ModePerm); err != nil {
return err
}
// datapower subdirectory
if len(datapowerSubdir) > 0 {
datapowerDir := concatSubdir(basedir, datapowerSubdir)
if err = os.MkdirAll(datapowerDir, os.ModePerm); err != nil {
return err
}
// soma subdirectory (this is internal)
const somasubdir = "soma"
somadir := concatSubdir(datapowerDir, somasubdir)
if err = os.MkdirAll(somadir, os.ModePerm); err != nil {
return err
}
}
return err
}
func tpdir(tbox *rice.Box) string {
if tbox != nil {
return ""
}
return "templates" + string(os.PathSeparator)
}
func readBytes(file string, tbox *rice.Box) []byte {
if tbox != nil {
return bytesInBox(file, tbox)
}
return readFileBytes(file)
}
func copyFile(srcfile string, dstfile string) {
bytes := readFileBytes(srcfile)
writeFileBytes(dstfile, bytes)
}
func concatFiles2(srcfiles []string, dstfile string) {
bytes := make([]byte, 0)
for _, srcfile := range srcfiles {
bytes1 := readFileBytes(srcfile)
for _, b := range bytes1 {
bytes = append(bytes, b)
}
// add new-line
bytes = append(bytes, byte('\n'))
}
writeFileBytes(dstfile, bytes)
}
func concatFiles(srcfile1 string, srcfile2 string, dstfile string) {
bytes1 := readFileBytes(srcfile1)
bytes2 := readFileBytes(srcfile2)
bytes := make([]byte, 0, len(bytes1) + len(bytes2))
for _, b := range bytes1 {
bytes = append(bytes, b)
}
for _, b := range bytes2 {
bytes = append(bytes, b)
}
writeFileBytes(dstfile, bytes)
}
func parseTemplate(tbox *rice.Box, file string) *template.Template {
str := string(readBytes(file, tbox))
t := template.New(filepath.Base(file)).Funcs(sprig.TxtFuncMap())
t, err := t.Parse(str)
if err != nil {
log.Fatal(err)
}
return t
}
func parseTemplates(tbox *rice.Box, file string, files ...string) *template.Template {
s := string(readBytes(file, tbox))
t := template.New(filepath.Base(file)).Funcs(sprig.TxtFuncMap())
t, err := t.Parse(s)
if err != nil {
log.Fatal(err)
}
for _, f := range files {
s := string(readBytes(f, tbox))
n := filepath.Base(f)
t1 := t.New(n).Funcs(sprig.TxtFuncMap())
t1, err := t1.Parse(s)
if err != nil {
log.Fatal(err)
}
}
return t
}
// can do better...
func copySlices(dst []string, src []string) []string {
if dst == nil && src != nil {
return src
}
if src == nil {
if dst == nil {
return []string{}
} else {
return dst
}
} else {
for _, v := range src {
// duplicates possible
dst = append(dst, v)
}
}
return dst
}
func executeTemplate(t *template.Template, obj interface{}) []string {
b := executeTemplate2(t, obj)
return strings.Split(b, "\n")
}
func executeTemplate2(t *template.Template, obj interface{}) string {
b := &strings.Builder{}
err := t.Execute(b, obj)
if err != nil {
log.Fatal(err)
}
return b.String()
}
func writeLines(lines []string, file string) {
f, err := openFile(file)
if err == nil {
defer func() {_ = f.Close()}()
} else {
f = os.Stdout
}
spacecount := 0
firstline := true
yaml := strings.HasSuffix(strings.ToLower(file),".yml" ) ||
strings.HasSuffix(strings.ToLower(file), "yaml") ||
strings.HasSuffix(strings.ToLower(file), "json")
for _, line := range lines {
if yaml {
// output yaml file as is...
// matching on the file extension will not work if output to stdout...
// look for the yaml file marker at the first line (todo)
_, _ = fmt.Fprintf(f, "%s\n", line)
continue
}
s := strings.TrimSpace(line)
if len(s) == 0 {
spacecount++
} else if spacecount > 0 {
// collapse blank lines
spacecount = 0
if firstline {
// no blank line before the fist line
firstline = false
_, _ = fmt.Fprintf(f, "%s\n", s)
} else {
// insert blank line in front of aline
_, _ = fmt.Fprintf(f, "\n%s\n", s)
}
} else {
_, _ = fmt.Fprintf(f, "%s\n", s)
if firstline {
firstline = false
}
}
}
}
func writeTemplate(t *template.Template, file string, obj interface{}) {
lines := executeTemplate(t, obj)
writeLines(lines, file)
}
func writeTemplate2(t *template.Template, file string, xf func(buf string) string, obj interface{}) {
buf := executeTemplate2(t, obj)
writeFileBytes(file, []byte(xf(buf)))
}
func unmarshallJsonFile(file string, objptr interface{}) {
if err := json.Unmarshal(readFileBytes(file), objptr); err != nil {
log.Fatalf("json unmarshal failed... %s", err)
}
}