1111'use strict'
1212
1313var util = require ( 'util' )
14+ var CodeBuilder = require ( '../../helpers/code-builder' )
1415
1516module . exports = function ( source , options ) {
1617 // Let's Go!
17- var code = [ ]
18+ var code = new CodeBuilder ( '\t' )
1819
1920 // Define Options
2021 var opts = util . _extend ( {
@@ -28,87 +29,89 @@ module.exports = function (source, options) {
2829 var errorCheck = function ( ) {
2930 if ( opts . checkErrors ) {
3031 code . push ( '\tif err != nil {' )
31- code . push ( '\t\tpanic(err)' )
32- code . push ( '\t}' )
32+ . push ( '\t\tpanic(err)' )
33+ . push ( '\t}' )
3334 }
3435 }
3536
3637 // Create boilerplate
3738 code . push ( 'package main\n' )
38- code . push ( 'import (' )
39- code . push ( '\t "fmt"')
39+ . push ( 'import (' )
40+ . push ( 1 , ' "fmt"')
4041
4142 if ( opts . timeout > 0 ) {
42- code . push ( '\t "time"')
43+ code . push ( 1 , ' "time"')
4344 }
4445
4546 if ( source . postData . text ) {
46- code . push ( '\t "strings"')
47+ code . push ( 1 , ' "strings"')
4748 }
4849
49- code . push ( '\t "net/http"')
50+ code . push ( 1 , ' "net/http"')
5051
5152 if ( opts . printBody ) {
52- code . push ( '\t "io/ioutil"')
53+ code . push ( 1 , ' "io/ioutil"')
5354 }
5455
5556 code . push ( ')\n' )
56-
57- code . push ( 'func main() {\n' )
57+ . push ( 'func main() {\n' )
5858
5959 // Create client
6060 var client
6161 if ( opts . timeout > 0 ) {
6262 client = 'client'
63- code . push ( '\tclient := http.Client{')
64- code . push ( util . format ( '\t\tTimeout : time.Duration(%s * time.Second),' , opts . timeout ) )
65- code . push ( '\t }\n')
63+ code . push ( 1 , 'client := http.Client{')
64+ . push ( 2 , util . format ( 'Timeout : time.Duration(%s * time.Second),' , opts . timeout ) )
65+ . push ( 1 , ' }\n')
6666 } else {
6767 client = 'http.DefaultClient'
6868 }
6969
70- code . push ( util . format ( '\turl := "%s"\n' , source . fullUrl ) )
70+ code . push ( 1 , util . format ( 'url := "%s"\n' , source . fullUrl ) )
7171
7272 // If we have body content or not create the var and reader or nil
7373 if ( source . postData . text ) {
74- code . push ( util . format ( '\tpayload := strings.NewReader(%s)\n' , JSON . stringify ( source . postData . text ) ) )
75- code . push ( util . format ( '\treq , %s := http.NewRequest("%s", url, payload)\n' , errorPlaceholder , source . method ) )
74+ code . push ( 1 , util . format ( 'payload := strings.NewReader(%s)\n' , JSON . stringify ( source . postData . text ) ) )
75+ . push ( 1 , util . format ( 'req , %s := http.NewRequest("%s", url, payload)\n' , errorPlaceholder , source . method ) )
7676 } else {
77- code . push ( util . format ( '\treq , %s := http.NewRequest("%s", url, nil)\n' , errorPlaceholder , source . method ) )
77+ code . push ( 1 , util . format ( 'req , %s := http.NewRequest("%s", url, nil)\n' , errorPlaceholder , source . method ) )
7878 }
7979
8080 errorCheck ( )
8181
8282 // Add headers
8383 if ( Object . keys ( source . allHeaders ) . length ) {
8484 Object . keys ( source . allHeaders ) . map ( function ( key ) {
85- code . push ( util . format ( '\treq .Header.Add("%s", "%s")' , key , source . allHeaders [ key ] ) )
85+ code . push ( 1 , util . format ( 'req .Header.Add("%s", "%s")' , key , source . allHeaders [ key ] ) )
8686 } )
87- code . push ( null )
87+ code . blank ( )
8888 }
8989
9090 // Make request
91- code . push ( util . format ( '\tres , %s := %s.Do(req)' , errorPlaceholder , client ) )
91+ code . push ( 1 , util . format ( 'res , %s := %s.Do(req)' , errorPlaceholder , client ) )
9292 errorCheck ( )
9393
9494 // Get Body
9595 if ( opts . printBody ) {
96- code . push ( '\n\tdefer res.Body.Close()' )
97- code . push ( util . format ( '\tbody, %s := ioutil.ReadAll(res.Body)' , errorPlaceholder ) )
96+ code . blank ( )
97+ . push ( 1 , 'defer res.Body.Close()' )
98+ . push ( 1 , util . format ( 'body, %s := ioutil.ReadAll(res.Body)' , errorPlaceholder ) )
9899 errorCheck ( )
99100 }
100101
101102 // Print it
102- code . push ( '\n\tfmt.Println(res)' )
103+ code . blank ( )
104+ . push ( 1 , 'fmt.Println(res)' )
103105
104106 if ( opts . printBody ) {
105- code . push ( '\tfmt .Println(string(body))')
107+ code . push ( 1 , 'fmt .Println(string(body))')
106108 }
107109
108110 // End main block
109- code . push ( '\n}' )
111+ code . blank ( )
112+ . push ( '}' )
110113
111- return code . join ( '\n' )
114+ return code . join ( )
112115}
113116
114117module . exports . info = {
0 commit comments