-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
287 lines (231 loc) · 6.08 KB
/
main.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
package main
import (
"bufio"
"bytes"
"flag"
"fmt"
"github.com/PuerkitoBio/goquery"
"github.com/microcosm-cc/bluemonday"
"github.com/yuin/goldmark"
"github.com/yuin/goldmark/extension"
goldHtml "github.com/yuin/goldmark/renderer/html"
"html"
"io/ioutil"
"log"
"os"
"strings"
)
var command bool
var debug bool
var variables []string
var tagsToRead = []string{
"code",
"#comment",
"h1",
"h2",
"h3",
}
func main() {
var fileName string
var outputFileName string
flag.StringVar(&fileName, "f", "", "Markdown file to parse.")
flag.StringVar(&outputFileName, "o", "", "Name of the bash output file.")
flag.Parse()
if fileName == "" {
fmt.Println("Please provide md file by using -f option")
return
}
if outputFileName == "" {
fmt.Println("Using default output destination")
outputFileName = "output.sh"
}
if err := convertTutorialIntoBashScript(fileName, outputFileName); err != nil {
log.Fatal(err)
}
}
func convertTutorialIntoBashScript(fileName, outputFileName string) error {
// read content from input file
htmlBytes, err := readInputFile(fileName)
if err != nil {
return err
}
// read commands from html
commands, err := getCommandsAndActions(htmlBytes)
if err != nil {
return err
}
// generate bash script using the commands
if err := generateBashScript(commands, outputFileName); err != nil {
return err
}
return nil
}
func readInputFile(fileName string) ([]byte, error) {
var htmlBytes []byte
var splitFileName = strings.Split(fileName, ".")
if splitFileName[len(splitFileName)-1] == "md" {
mdFile, err := ioutil.ReadFile(fileName)
if err != nil {
return nil, err
}
htmlBytes, err = convertMarkdownToHTML(mdFile)
if err != nil {
return nil, err
}
} else if splitFileName[len(splitFileName)-1] == "html" {
htmlFile, err := ioutil.ReadFile(fileName)
if err != nil {
return nil, err
}
htmlBytes = htmlFile
} else {
log.Fatal("File format not supported. Try again using either and md or html file!")
}
return htmlBytes, nil
}
func convertMarkdownToHTML(markdown []byte) ([]byte, error) {
md := goldmark.New(
goldmark.WithExtensions(extension.GFM),
goldmark.WithParserOptions(),
goldmark.WithRendererOptions(
goldHtml.WithUnsafe(),
),
)
var buf bytes.Buffer
if err := md.Convert(markdown, &buf); err != nil {
return nil, err
}
return buf.Bytes(), nil
}
func getCommandsAndActions(mdFile []byte) ([]string, error) {
byteReader := bytes.NewReader(mdFile)
doc, err := goquery.NewDocumentFromReader(byteReader)
if err != nil {
return nil, err
}
sel := new(goquery.Selection)
tbl := doc.Find("body")
// find all wanted items
sel = visitNodes(sel, tbl)
// process the data
commands := processData(sel)
return commands, nil
}
func processData(sel *goquery.Selection) []string {
var commands []string
// loop through items and process the data
sel.Each(func(i int, s *goquery.Selection) {
// get the content of the selector
htmlString, err := goquery.OuterHtml(s)
if err != nil {
log.Fatal(err)
}
p := bluemonday.StrictPolicy()
var command string
switch goquery.NodeName(s) {
case "#comment":
htmlString = html.UnescapeString(htmlString)
command = processComment(htmlString)
case "code":
htmlString = p.Sanitize(htmlString)
htmlString = html.UnescapeString(htmlString)
command = processCode(htmlString)
case "h1", "h2", "h3":
htmlString = p.Sanitize(htmlString)
htmlString = html.UnescapeString(htmlString)
printHeading := fmt.Sprintf(`echo "%s"`, htmlString)
headingCommands := []string{
`echo "---------------------------------------------------------------------"`,
printHeading,
`echo "---------------------------------------------------------------------"`,
`echo ""`,
}
command = strings.Join(headingCommands, "\n")
default:
fmt.Println("Node name not supported!")
}
if len(command) != 0 {
commands = append(commands, command)
}
})
return commands
}
func processComment(comment string) string {
comment = strings.Replace(comment, "<!--", "", -1)
comment = strings.Replace(comment, "-->", "", -1)
comment = strings.TrimSpace(comment)
// Read the different annotations and then process the comment
if comment == "command" {
command = true
} else if strings.Contains(comment, "bash") {
comment = strings.Replace(comment, "bash", "", -1)
comment = strings.TrimSpace(comment)
return comment
} else if comment == "debug" {
debug = true
command = true
} else if strings.Contains(comment, "var") {
comment = strings.Replace(comment, "var", "", -1)
comment = strings.TrimSpace(comment)
comment = fmt.Sprintf("if [ -z \"$%s\" ]; then\n echo \"Please supply a value for the environment variable %s\"\n exit 1\nfi", comment, comment)
variables = append(variables, comment)
return ""
}
return ""
}
func processCode(code string) string {
code = strings.TrimSpace(code)
if command && debug {
command = false
debug = false
return fmt.Sprintf("if [ \"$DEBUG\" = \"true\" ]; then \n%s \nfi", code)
} else if command {
command = false
return code
}
return ""
}
func visitNodes(dst, src *goquery.Selection) *goquery.Selection {
src.Contents().Each(func(i int, s *goquery.Selection) {
if sliceContains(tagsToRead, goquery.NodeName(s)) {
dst = dst.AddSelection(s)
} else {
dst = visitNodes(dst, s)
}
})
return dst
}
func generateBashScript(commands []string, path string) error {
file, err := os.Create(path)
if err != nil {
return err
}
defer file.Close()
w := bufio.NewWriter(file)
importUtilsPath := `source /dev/stdin <<<"$( curl -sS https://raw.githubusercontent.com/keptn/keptn/master/test/utils.sh)"`
fmt.Fprintln(w, "#!/bin/bash")
fmt.Fprintln(w, "set -e")
fmt.Fprintln(w, importUtilsPath)
fmt.Fprintln(w, "")
for _, line := range variables {
fmt.Fprintln(w, line)
fmt.Fprintln(w, "")
}
for _, line := range commands {
fmt.Fprintln(w, line)
fmt.Fprintln(w, "")
}
err = os.Chmod(path, 0777)
if err != nil {
return err
}
return w.Flush()
}
func sliceContains(slice []string, val string) bool {
for _, item := range slice {
if item == val {
return true
}
}
return false
}