Last active
December 3, 2021 13:41
-
-
Save malys/c6a7f403f7c36fcf12ffb5fca12d8b7d to your computer and use it in GitHub Desktop.
[Artillery to curl] script to artillery script to curl commands #artillery #curl #nodejs #javascript
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//node convertToCurl.mjs script.yml > test.sh | |
// mv convertToCurl.js convertToCurl.mjs | |
import convertToCurl from './convertToCurlLib.mjs' | |
import yargs from 'yargs' | |
import { | |
hideBin | |
} from 'yargs/helpers' | |
const argv = yargs(hideBin(process.argv)).argv | |
console.log(convertToCurl(argv._[0])) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import YAML from 'yaml' | |
import { | |
parser | |
} from 'fetch2curl' | |
import debug from 'debug'; | |
import fs from 'fs' | |
const logger = debug('curl'); | |
const regex = /\{\{\s?([a-zA-Z_-]+)\s?\}\}/gm; | |
const subst = `\${$1}`; | |
const definition = 'export' | |
export default function (fileName) { | |
const file = fs.readFileSync(fileName, 'utf8') | |
let script = YAML.parse(file) | |
logger(script) | |
let capture = (data, c) => { | |
logger(c) | |
if (c) { | |
bash.push(`${definition} TMP_RESULT=\$(${data}) && echo result \${TMP_RESULT}`) | |
c.forEach(r => { | |
let jq = r.json.replace('$', '.').replace('..', '.') | |
data = `${definition} ${r.as}=\$(echo \$TMP_RESULT | jq -r '${jq}') && echo "${r.as}" \$${r.as}` | |
}) | |
} | |
return data; | |
} | |
let bash = [] | |
bash.push('#!/bin/bash') | |
//Variables | |
Object.keys(script.config.variables).forEach(key => { | |
bash.push(`${definition} ${key}=${script.config.variables[key]}`) | |
}) | |
bash.push(`${definition} BASE_URL="TO SET"`) | |
//Flow | |
logger(script.scenarios[0].flow) | |
script.scenarios[0].flow.forEach(step => { | |
Object.keys(step).forEach(verb => { | |
if (['GET', 'POST', 'PUT', 'DELETE'].includes(verb.toUpperCase() )) { | |
let c = step[verb] | |
let params = { | |
method: verb.toUpperCase(), | |
} | |
if (c.headers) params.headers = c.headers | |
if (c.json) params.body = JSON.stringify(c.json).replace(/"/g, '\\"') | |
if (c.body) params.body = c.body | |
if (c.name) bash.push(`# ${c.name}`) | |
let url = c.url; | |
bash.push(capture(parser(`\${BASE_URL}${url}`, params), c.capture)) | |
} | |
}) | |
}) | |
return bash.join('\n').replace(regex, subst).replace(/'/g, '"').replace(/curl/g, 'curl -k -s') | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment