@@ -19,106 +19,113 @@ 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
2526 } , options )
2627
2728 var errorPlaceholder = opts . checkErrors ? 'err' : '_'
2829
30+ var indent = opts . showBoilerplate ? 1 : 0
31+
2932 var errorCheck = function ( ) {
3033 if ( opts . checkErrors ) {
31- code . push ( 1 , 'if err != nil {' )
32- . push ( 2 , 'panic(err)' )
33- . push ( 1 , '}' )
34+ code . push ( indent , 'if err != nil {' )
35+ . push ( indent + 1 , 'panic(err)' )
36+ . push ( indent , '}' )
3437 }
3538 }
3639
3740 // Create boilerplate
38- code . push ( 'package main' )
41+ if ( opts . showBoilerplate ) {
42+ code . push ( 'package main' )
3943 . blank ( )
4044 . push ( 'import (' )
41- . push ( 1 , '"fmt"' )
45+ . push ( indent , '"fmt"' )
4246
43- if ( opts . timeout > 0 ) {
44- code . push ( 1 , '"time"' )
45- }
47+ if ( opts . timeout > 0 ) {
48+ code . push ( indent , '"time"' )
49+ }
4650
47- if ( source . postData . text ) {
48- code . push ( 1 , '"strings"' )
49- }
51+ if ( source . postData . text ) {
52+ code . push ( indent , '"strings"' )
53+ }
5054
51- code . push ( 1 , '"net/http"' )
55+ code . push ( indent , '"net/http"' )
5256
53- if ( opts . printBody ) {
54- code . push ( 1 , '"io/ioutil"' )
55- }
57+ if ( opts . printBody ) {
58+ code . push ( indent , '"io/ioutil"' )
59+ }
5660
57- code . push ( ')' )
61+ code . push ( ')' )
5862 . blank ( )
5963 . push ( 'func main() {' )
6064 . blank ( )
65+ }
6166
6267 // Create client
6368 var client
6469 if ( opts . timeout > 0 ) {
6570 client = 'client'
66- code . push ( 1 , 'client := http.Client{' )
67- . push ( 2 , 'Timeout: time.Duration(%s * time.Second),' , opts . timeout )
68- . push ( 1 , '}' )
69- . blank ( )
71+ code . push ( indent , 'client := http.Client{' )
72+ . push ( indent + 1 , 'Timeout: time.Duration(%s * time.Second),' , opts . timeout )
73+ . push ( indent , '}' )
74+ . blank ( )
7075 } else {
7176 client = 'http.DefaultClient'
7277 }
7378
74- code . push ( 1 , 'url := "%s"' , source . fullUrl )
75- . blank ( )
79+ code . push ( indent , 'url := "%s"' , source . fullUrl )
80+ . blank ( )
7681
7782 // If we have body content or not create the var and reader or nil
7883 if ( source . postData . text ) {
79- 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 ( )
84+ code . push ( indent , 'payload := strings.NewReader(%s)' , JSON . stringify ( source . postData . text ) )
85+ . blank ( )
86+ . push ( indent , 'req, %s := http.NewRequest("%s", url, payload)' , errorPlaceholder , source . method )
87+ . blank ( )
8388 } else {
84- code . push ( 1 , 'req, %s := http.NewRequest("%s", url, nil)' , errorPlaceholder , source . method )
85- . blank ( )
89+ code . push ( indent , 'req, %s := http.NewRequest("%s", url, nil)' , errorPlaceholder , source . method )
90+ . blank ( )
8691 }
8792
8893 errorCheck ( )
8994
9095 // Add headers
9196 if ( Object . keys ( source . allHeaders ) . length ) {
9297 Object . keys ( source . allHeaders ) . forEach ( function ( key ) {
93- 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 ] )
9499 } )
95100
96101 code . blank ( )
97102 }
98103
99104 // Make request
100- code . push ( 1 , 'res, %s := %s.Do(req)' , errorPlaceholder , client )
105+ code . push ( indent , 'res, %s := %s.Do(req)' , errorPlaceholder , client )
101106 errorCheck ( )
102107
103108 // Get Body
104109 if ( opts . printBody ) {
105110 code . blank ( )
106- . push ( 1 , 'defer res.Body.Close()' )
107- . 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 )
108113 errorCheck ( )
109114 }
110115
111116 // Print it
112117 code . blank ( )
113- . push ( 1 , 'fmt.Println(res)' )
118+ . push ( indent , 'fmt.Println(res)' )
114119
115120 if ( opts . printBody ) {
116- code . push ( 1 , 'fmt.Println(string(body))' )
121+ code . push ( indent , 'fmt.Println(string(body))' )
117122 }
118123
119124 // End main block
120- code . blank ( )
125+ if ( opts . showBoilerplate ) {
126+ code . blank ( )
121127 . push ( '}' )
128+ }
122129
123130 return code . join ( )
124131}
0 commit comments