Skip to content

Commit 5e0349f

Browse files
committed
style: polishing CodeBuilder support + shell helpers
1 parent b7ed85e commit 5e0349f

11 files changed

Lines changed: 72 additions & 62 deletions

File tree

src/helpers/shell/escape.js

Lines changed: 0 additions & 5 deletions
This file was deleted.

src/helpers/shell/index.js

Lines changed: 0 additions & 3 deletions
This file was deleted.

src/helpers/shell/quote.js

Lines changed: 0 additions & 19 deletions
This file was deleted.

src/targets/go/native.js

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,15 @@ module.exports = function (source, options) {
2828

2929
var errorCheck = function () {
3030
if (opts.checkErrors) {
31-
code.push('\tif err != nil {')
32-
.push('\t\tpanic(err)')
33-
.push('\t}')
31+
code.push(1, 'if err != nil {')
32+
.push(2, 'panic(err)')
33+
.push(1, '}')
3434
}
3535
}
3636

3737
// Create boilerplate
38-
code.push('package main\n')
38+
code.push('package main')
39+
.blank()
3940
.push('import (')
4041
.push(1, '"fmt"')
4142

@@ -53,28 +54,35 @@ module.exports = function (source, options) {
5354
code.push(1, '"io/ioutil"')
5455
}
5556

56-
code.push(')\n')
57-
.push('func main() {\n')
57+
code.push(')')
58+
.blank()
59+
.push('func main() {')
60+
.blank()
5861

5962
// Create client
6063
var client
6164
if (opts.timeout > 0) {
6265
client = 'client'
6366
code.push(1, 'client := http.Client{')
6467
.push(2, util.format('Timeout: time.Duration(%s * time.Second),', opts.timeout))
65-
.push(1, '}\n')
68+
.push(1, '}')
69+
.blank()
6670
} else {
6771
client = 'http.DefaultClient'
6872
}
6973

70-
code.push(1, util.format('url := "%s"\n', source.fullUrl))
74+
code.push(1, util.format('url := "%s"', source.fullUrl))
75+
.blank()
7176

7277
// If we have body content or not create the var and reader or nil
7378
if (source.postData.text) {
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))
79+
code.push(1, util.format('payload := strings.NewReader(%s)', JSON.stringify(source.postData.text)))
80+
.blank()
81+
.push(1, util.format('req, %s := http.NewRequest("%s", url, payload)', errorPlaceholder, source.method))
82+
.blank()
7683
} else {
77-
code.push(1, util.format('req, %s := http.NewRequest("%s", url, nil)\n', errorPlaceholder, source.method))
84+
code.push(1, util.format('req, %s := http.NewRequest("%s", url, nil)', errorPlaceholder, source.method))
85+
.blank()
7886
}
7987

8088
errorCheck()

src/targets/node/unirest.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ module.exports = function (source, options) {
105105
.push('});')
106106
.blank()
107107

108-
return code.join('\n').replace(/"fs\.createReadStream\(\\\"(.+)\\\"\)\"/, 'fs.createReadStream("$1")')
108+
return code.join().replace(/"fs\.createReadStream\(\\\"(.+)\\\"\)\"/, 'fs.createReadStream("$1")')
109109
}
110110

111111
module.exports.info = {

src/targets/php/http1.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ module.exports = function (source, options) {
8585
.push('?>')
8686
}
8787

88-
return code.join('\n')
88+
return code.join()
8989
}
9090

9191
module.exports.info = {

src/targets/python/python3.js

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,18 @@ module.exports = function (source, options) {
2222
// Check which protocol to be used for the client connection
2323
var protocol = source.uriObj.protocol
2424
if (protocol === 'https:') {
25-
code.push(util.format('conn = http.client.HTTPSConnection("%s")\n', source.uriObj.host))
25+
code.push(util.format('conn = http.client.HTTPSConnection("%s")', source.uriObj.host))
26+
.blank()
2627
} else {
27-
code.push(util.format('conn = http.client.HTTPConnection("%s")\n', source.uriObj.host))
28+
code.push(util.format('conn = http.client.HTTPConnection("%s")', source.uriObj.host))
29+
.blank()
2830
}
2931

3032
// Create payload string if it exists
3133
var payload = JSON.stringify(source.postData.text)
3234
if (payload) {
33-
code.push(util.format('payload = %s\n', payload))
35+
code.push(util.format('payload = %s', payload))
36+
.blank()
3437
}
3538

3639
// Create Headers
@@ -39,7 +42,8 @@ module.exports = function (source, options) {
3942
var headerCount = Object.keys(headers).length
4043
if (headerCount === 1) {
4144
for (header in headers) {
42-
code.push(util.format('headers = { \'%s\': "%s" }\n', header, headers[header]))
45+
code.push(util.format('headers = { \'%s\': "%s" }', header, headers[header]))
46+
.blank()
4347
}
4448
} else if (headerCount > 1) {
4549
var headerLine

src/targets/shell/curl.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
'use strict'
1212

1313
var util = require('util')
14-
var shell = require('../../helpers/shell')
14+
var helpers = require('./helpers')
1515
var CodeBuilder = require('../../helpers/code-builder')
1616

1717
module.exports = function (source, options) {
@@ -23,7 +23,7 @@ module.exports = function (source, options) {
2323
var code = new CodeBuilder(opts.indent, opts.indent !== false ? ' \\\n' + opts.indent : ' ')
2424

2525
code.push(util.format('curl %s %s', opts.short ? '-X' : '--request', source.method))
26-
.push(util.format('%s%s', opts.short ? '' : '--url ', shell.quote(source.fullUrl)))
26+
.push(util.format('%s%s', opts.short ? '' : '--url ', helpers.quote(source.fullUrl)))
2727

2828
if (source.httpVersion === 'HTTP/1.0') {
2929
code.push(opts.short ? '-0' : '--http1.0')
@@ -32,23 +32,23 @@ module.exports = function (source, options) {
3232
// construct headers
3333
Object.keys(source.headersObj).sort().map(function (key) {
3434
var header = util.format('%s: %s', key, source.headersObj[key])
35-
code.push(util.format('%s %s', opts.short ? '-H' : '--header', shell.quote(header)))
35+
code.push(util.format('%s %s', opts.short ? '-H' : '--header', helpers.quote(header)))
3636
})
3737

3838
if (source.allHeaders.cookie) {
39-
code.push(util.format('%s %s', opts.short ? '-b' : '--cookie', shell.quote(source.allHeaders.cookie)))
39+
code.push(util.format('%s %s', opts.short ? '-b' : '--cookie', helpers.quote(source.allHeaders.cookie)))
4040
}
4141

4242
// request body
4343
if (source.postData.text) {
44-
code.push(util.format('%s %s', opts.short ? '-d' : '--data', shell.escape(shell.quote(source.postData.text))))
44+
code.push(util.format('%s %s', opts.short ? '-d' : '--data', helpers.escape(helpers.quote(source.postData.text))))
4545
}
4646

4747
// construct post params
4848
if (!source.postData.text && source.postData.params) {
4949
source.postData.params.map(function (param) {
5050
var post = util.format('%s=%s', param.name, param.value)
51-
code.push(util.format('%s %s', opts.short ? '-F' : '--form', shell.quote(post)))
51+
code.push(util.format('%s %s', opts.short ? '-F' : '--form', helpers.quote(post)))
5252
})
5353
}
5454

src/targets/shell/helpers.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
'use strict'
2+
3+
var util = require('util')
4+
5+
module.exports = {
6+
/**
7+
* Use 'strong quoting' using single quotes so that we only need
8+
* to deal with nested single quote characters.
9+
* http://wiki.bash-hackers.org/syntax/quoting#strong_quoting
10+
*/
11+
quote: function (value) {
12+
var safe = /^[a-z0-9-_/.@%^=:]+$/i
13+
14+
// Unless `value` is a simple shell-safe string, quote it.
15+
if (!safe.test(value)) {
16+
return util.format('\'%s\'', value.replace(/'/g, "\'\\'\'"))
17+
}
18+
19+
return value
20+
},
21+
22+
escape: function (value) {
23+
return value.replace(/\r/g, '\\r').replace(/\n/g, '\\n')
24+
}
25+
}

src/targets/shell/httpie.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
'use strict'
1212

1313
var util = require('util')
14-
var quote = require('../../helpers/shell/quote')
14+
var helpers = require('./helpers')
1515
var CodeBuilder = require('../../helpers/code-builder')
1616

1717
module.exports = function (source, options) {
@@ -34,7 +34,7 @@ module.exports = function (source, options) {
3434

3535
// start with body pipe
3636
if (source.postData && source.postData.text) {
37-
code.push(util.format('echo %s | ', quote(source.postData.text)))
37+
code.push(util.format('echo %s | ', helpers.quote(source.postData.text)))
3838
}
3939

4040
var flags = []
@@ -75,7 +75,7 @@ module.exports = function (source, options) {
7575
flags.push(util.format('--timeout=%s', opts.timeout))
7676
}
7777

78-
code.push(util.format('http %s%s %s', flags.length ? flags.join(' ') + ' ' : '', source.method, quote(opts.queryParams ? source.url : source.fullUrl)))
78+
code.push(util.format('http %s%s %s', flags.length ? flags.join(' ') + ' ' : '', source.method, helpers.quote(opts.queryParams ? source.url : source.fullUrl)))
7979

8080
// construct query params
8181
if (opts.queryParams) {
@@ -86,23 +86,23 @@ module.exports = function (source, options) {
8686

8787
if (util.isArray(value)) {
8888
value.map(function (val) {
89-
code.push(util.format('%s==%s', name, quote(val)))
89+
code.push(util.format('%s==%s', name, helpers.quote(val)))
9090
})
9191
} else {
92-
code.push(util.format('%s==%s', name, quote(value)))
92+
code.push(util.format('%s==%s', name, helpers.quote(value)))
9393
}
9494
})
9595
}
9696

9797
// construct headers
9898
Object.keys(source.allHeaders).sort().map(function (key) {
99-
code.push(util.format('%s:%s', key, quote(source.allHeaders[key])))
99+
code.push(util.format('%s:%s', key, helpers.quote(source.allHeaders[key])))
100100
})
101101

102102
// construct post params
103103
if (!source.postData.text && source.postData.params && source.postData.params.length) {
104104
source.postData.params.map(function (param) {
105-
code.push(util.format('%s:%s', param.name, quote(param.value)))
105+
code.push(util.format('%s:%s', param.name, helpers.quote(param.value)))
106106
})
107107
}
108108

0 commit comments

Comments
 (0)