Skip to content

Commit d1e6a1e

Browse files
author
Montana Flynn
committed
Add option to include boilerplate
1 parent d50be25 commit d1e6a1e

1 file changed

Lines changed: 31 additions & 26 deletions

File tree

‎src/targets/go/native.js‎

Lines changed: 31 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ module.exports = function (source, options) {
1919

2020
// Define Options
2121
var opts = util._extend({
22+
showBoilerplate: true,
2223
checkErrors: false,
2324
printBody: true,
2425
timeout: -1
@@ -29,60 +30,62 @@ module.exports = function (source, options) {
2930
var errorCheck = function () {
3031
if (opts.checkErrors) {
3132
code.push(1, 'if err != nil {')
32-
.push(2, 'panic(err)')
33-
.push(1, '}')
33+
.push(2, 'panic(err)')
34+
.push(1, '}')
3435
}
3536
}
3637

3738
// Create boilerplate
38-
code.push('package main')
39+
if (opts.showBoilerplate) {
40+
code.push('package main')
3941
.blank()
4042
.push('import (')
4143
.push(1, '"fmt"')
4244

43-
if (opts.timeout > 0) {
44-
code.push(1, '"time"')
45-
}
45+
if (opts.timeout > 0) {
46+
code.push(1, '"time"')
47+
}
4648

47-
if (source.postData.text) {
48-
code.push(1, '"strings"')
49-
}
49+
if (source.postData.text) {
50+
code.push(1, '"strings"')
51+
}
5052

51-
code.push(1, '"net/http"')
53+
code.push(1, '"net/http"')
5254

53-
if (opts.printBody) {
54-
code.push(1, '"io/ioutil"')
55-
}
55+
if (opts.printBody) {
56+
code.push(1, '"io/ioutil"')
57+
}
5658

57-
code.push(')')
59+
code.push(')')
5860
.blank()
5961
.push('func main() {')
6062
.blank()
63+
}
6164

6265
// Create client
6366
var client
6467
if (opts.timeout > 0) {
6568
client = 'client'
6669
code.push(1, 'client := http.Client{')
67-
.push(2, 'Timeout: time.Duration(%s * time.Second),', opts.timeout)
68-
.push(1, '}')
69-
.blank()
70+
.push(2, 'Timeout: time.Duration(%s * time.Second),', opts.timeout)
71+
.push(1, '}')
72+
.blank()
7073
} else {
7174
client = 'http.DefaultClient'
7275
}
7376

7477
code.push(1, 'url := "%s"', source.fullUrl)
75-
.blank()
78+
.blank()
7679

7780
// If we have body content or not create the var and reader or nil
7881
if (source.postData.text) {
7982
code.push(1, 'payload := strings.NewReader(%s)', JSON.stringify(source.postData.text))
80-
.blank()
81-
.push(1, 'req, %s := http.NewRequest("%s", url, payload)', errorPlaceholder, source.method)
82-
.blank()
83+
.blank()
84+
.push(1, 'req, %s := http.NewRequest("%s", url, payload)', errorPlaceholder, source.method)
85+
.blank()
8386
} else {
8487
code.push(1, 'req, %s := http.NewRequest("%s", url, nil)', errorPlaceholder, source.method)
85-
.blank()
88+
.blank()
8689
}
8790

8891
errorCheck()
@@ -103,22 +106,24 @@ module.exports = function (source, options) {
103106
// Get Body
104107
if (opts.printBody) {
105108
code.blank()
106-
.push(1, 'defer res.Body.Close()')
107-
.push(1, 'body, %s := ioutil.ReadAll(res.Body)', errorPlaceholder)
109+
.push(1, 'defer res.Body.Close()')
110+
.push(1, 'body, %s := ioutil.ReadAll(res.Body)', errorPlaceholder)
108111
errorCheck()
109112
}
110113

111114
// Print it
112115
code.blank()
113-
.push(1, 'fmt.Println(res)')
116+
.push(1, 'fmt.Println(res)')
114117

115118
if (opts.printBody) {
116119
code.push(1, 'fmt.Println(string(body))')
117120
}
118121

119122
// End main block
120-
code.blank()
123+
if (opts.showBoilerplate) {
124+
code.blank()
121125
.push('}')
126+
}
122127

123128
return code.join()
124129
}

0 commit comments

Comments
 (0)