serverless icon indicating copy to clipboard operation
serverless copied to clipboard

Allow raw output from `sls invoke` without JSON encoding

Open dnicolson opened this issue 3 years ago • 1 comments

Is there an existing issue for this?

  • [X] I have searched existing issues, it hasn't been reported yet

Use case description

It would be useful to be able to able to get the raw output from the invoke command, similar to how the --raw switch works for input. The invoke and the invoke local commands stringify the output.

At the moment to generate a CSV file the output must be unstringified:

sls invoke local -f export --data XXX | node -e 'console.log(JSON.parse(fs.readFileSync("/dev/stdin", "utf-8")));' > out.csv

Proposed solution (optional)

Adding a --rawOutput command switch.

dnicolson avatar Sep 06 '22 18:09 dnicolson

Actually, because Serverless uses STDERR for its own output, the only way to differentiate errors from normal output is to try and parse the JSON:

OUTPUT=$(sls invoke local -f export --data XXX)

if CSV=$(echo $OUTPUT | node -e 'console.log(JSON.parse(fs.readFileSync("/dev/stdin", "utf-8")));' 2>&1); then
    echo "$CSV"
else
    echo "$OUTPUT"
fi

dnicolson avatar Sep 21 '22 09:09 dnicolson