@@ -4,9 +4,12 @@ var util = require('util');
44
55module . exports = function ( options ) {
66
7+ // Let's Go!
8+ var code = [ ] ;
9+
10+ // Define Options
711 var opts = util . _extend ( {
8- errorChecking : false ,
9- timeout : - 1 ,
12+ checkErrors : false ,
1013 printBody : true
1114 } , options ) ;
1215
@@ -22,16 +25,23 @@ module.exports = function (options) {
2225
2326 var bodyPresent = this . source . postData && this . source . postData . text ;
2427
25- // Let's Go!
26- var code = [ ] ;
28+ var errorPlaceholder = opts . checkErrors ? "err" : "_"
29+
30+ var errorCheck = function ( ) {
31+ if ( opts . checkErrors ) {
32+ code . push ( '\tif err != nil {' ) ;
33+ code . push ( '\t\tpanic(err)' ) ;
34+ code . push ( '\t}' ) ;
35+ }
36+ }
2737
2838 // Create boilerplate
2939 code . push ( 'package main\n' ) ;
3040 code . push ( 'import (' ) ;
3141 code . push ( '\t"fmt"' ) ;
3242 if ( bodyPresent ) code . push ( '\t"strings"' ) ;
3343 code . push ( '\t"net/http"' ) ;
34- code . push ( '\t"io/ioutil"' ) ;
44+ if ( opts . printBody ) code . push ( '\t"io/ioutil"' ) ;
3545 code . push ( ')\n' ) ;
3646
3747 code . push ( 'func main() {' ) ;
@@ -42,13 +52,13 @@ module.exports = function (options) {
4252
4353 // If we have body content or not create the var and reader or nil
4454 if ( bodyPresent ) {
45- code . push ( '\tbody := ' + JSON . stringify ( this . source . postData . text ) ) ;
46- req . body = 'strings.NewReader(body )' ;
55+ code . push ( '\tpayload := ' + JSON . stringify ( this . source . postData . text ) ) ;
56+ req . body = 'strings.NewReader(payload )' ;
4757 } else {
4858 req . body = 'nil' ;
4959 }
5060
51- code . push ( '\treq, _ := http.NewRequest("' + req . method + '", url, ' + req . body + ')' ) ;
61+ code . push ( '\treq, ' + errorPlaceholder + ' := http.NewRequest("' + req . method + '", url, ' + req . body + ')' ) ;
5262
5363 // Add headers
5464 var headersPresent = this . source . headers && this . source . headers . length ;
@@ -71,11 +81,12 @@ module.exports = function (options) {
7181 }
7282
7383 // Make request
74- code . push ( '\tres, _ := client.Do(req)' ) ;
84+ code . push ( '\tres, ' + errorPlaceholder + ' := client.Do(req)') ;
7585
7686 // Get Body
77- code . push ( '\tdefer res.Body.Close()' ) ;
78- code . push ( '\tbody, _ := ioutil.ReadAll(res.Body)' ) ;
87+ if ( opts . printBody ) code . push ( '\tdefer res.Body.Close()' ) ;
88+ if ( opts . printBody ) code . push ( '\tbody, ' + errorPlaceholder + ' := ioutil.ReadAll(res.Body)' ) ;
89+ errorCheck ( )
7990
8091 // Print it
8192 code . push ( '\tfmt.Println(res)' ) ;
0 commit comments