@@ -27,11 +27,13 @@ module.exports = function (source, options) {
2727
2828 var errorPlaceholder = opts . checkErrors ? 'err' : '_'
2929
30+ var indent = opts . showBoilerplate ? 1 : 0
31+
3032 var errorCheck = function ( ) {
3133 if ( opts . checkErrors ) {
32- code . push ( 1 , 'if err != nil {' )
33- . push ( 2 , 'panic(err)' )
34- . push ( 1 , '}' )
34+ code . push ( indent , 'if err != nil {' )
35+ . push ( indent + 1 , 'panic(err)' )
36+ . push ( indent , '}' )
3537 }
3638 }
3739
@@ -40,20 +42,20 @@ module.exports = function (source, options) {
4042 code . push ( 'package main' )
4143 . blank ( )
4244 . push ( 'import (' )
43- . push ( 1 , '"fmt"' )
45+ . push ( indent , '"fmt"' )
4446
4547 if ( opts . timeout > 0 ) {
46- code . push ( 1 , '"time"' )
48+ code . push ( indent , '"time"' )
4749 }
4850
4951 if ( source . postData . text ) {
50- code . push ( 1 , '"strings"' )
52+ code . push ( indent , '"strings"' )
5153 }
5254
53- code . push ( 1 , '"net/http"' )
55+ code . push ( indent , '"net/http"' )
5456
5557 if ( opts . printBody ) {
56- code . push ( 1 , '"io/ioutil"' )
58+ code . push ( indent , '"io/ioutil"' )
5759 }
5860
5961 code . push ( ')' )
@@ -66,25 +68,25 @@ module.exports = function (source, options) {
6668 var client
6769 if ( opts . timeout > 0 ) {
6870 client = 'client'
69- code . push ( 1 , 'client := http.Client{' )
70- . push ( 2 , 'Timeout: time.Duration(%s * time.Second),' , opts . timeout )
71- . push ( 1 , '}' )
71+ code . push ( indent , 'client := http.Client{' )
72+ . push ( indent + 1 , 'Timeout: time.Duration(%s * time.Second),' , opts . timeout )
73+ . push ( indent , '}' )
7274 . blank ( )
7375 } else {
7476 client = 'http.DefaultClient'
7577 }
7678
77- code . push ( 1 , 'url := "%s"' , source . fullUrl )
79+ code . push ( indent , 'url := "%s"' , source . fullUrl )
7880 . blank ( )
7981
8082 // If we have body content or not create the var and reader or nil
8183 if ( source . postData . text ) {
82- code . push ( 1 , 'payload := strings.NewReader(%s)' , JSON . stringify ( source . postData . text ) )
84+ code . push ( indent , 'payload := strings.NewReader(%s)' , JSON . stringify ( source . postData . text ) )
8385 . blank ( )
84- . push ( 1 , 'req, %s := http.NewRequest("%s", url, payload)' , errorPlaceholder , source . method )
86+ . push ( indent , 'req, %s := http.NewRequest("%s", url, payload)' , errorPlaceholder , source . method )
8587 . blank ( )
8688 } else {
87- code . push ( 1 , 'req, %s := http.NewRequest("%s", url, nil)' , errorPlaceholder , source . method )
89+ code . push ( indent , 'req, %s := http.NewRequest("%s", url, nil)' , errorPlaceholder , source . method )
8890 . blank ( )
8991 }
9092
@@ -93,30 +95,30 @@ module.exports = function (source, options) {
9395 // Add headers
9496 if ( Object . keys ( source . allHeaders ) . length ) {
9597 Object . keys ( source . allHeaders ) . forEach ( function ( key ) {
96- code . push ( 1 , 'req.Header.Add("%s", "%s")' , key , source . allHeaders [ key ] )
98+ code . push ( indent , 'req.Header.Add("%s", "%s")' , key , source . allHeaders [ key ] )
9799 } )
98100
99101 code . blank ( )
100102 }
101103
102104 // Make request
103- code . push ( 1 , 'res, %s := %s.Do(req)' , errorPlaceholder , client )
105+ code . push ( indent , 'res, %s := %s.Do(req)' , errorPlaceholder , client )
104106 errorCheck ( )
105107
106108 // Get Body
107109 if ( opts . printBody ) {
108110 code . blank ( )
109- . push ( 1 , 'defer res.Body.Close()' )
110- . push ( 1 , 'body, %s := ioutil.ReadAll(res.Body)' , errorPlaceholder )
111+ . push ( indent , 'defer res.Body.Close()' )
112+ . push ( indent , 'body, %s := ioutil.ReadAll(res.Body)' , errorPlaceholder )
111113 errorCheck ( )
112114 }
113115
114116 // Print it
115117 code . blank ( )
116- . push ( 1 , 'fmt.Println(res)' )
118+ . push ( indent , 'fmt.Println(res)' )
117119
118120 if ( opts . printBody ) {
119- code . push ( 1 , 'fmt.Println(string(body))' )
121+ code . push ( indent , 'fmt.Println(string(body))' )
120122 }
121123
122124 // End main block
0 commit comments