Skip to content

Commit

Permalink
gh-actions/jq: Handle using fs for filter arg (#981)
Browse files Browse the repository at this point in the history
Signed-off-by: Ryan Northey <ryan@synca.io>
  • Loading branch information
phlax authored Nov 8, 2023
1 parent 27d8096 commit deb978c
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 4 deletions.
3 changes: 3 additions & 0 deletions gh-actions/jq/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ inputs:
use-tmp-file:
type: boolean
default: true
use-tmp-file-filter:
type: boolean
default: false
input-format:
type: string
default: json
Expand Down
4 changes: 2 additions & 2 deletions gh-actions/jq/dist/index.js

Large diffs are not rendered by default.

18 changes: 16 additions & 2 deletions gh-actions/jq/jq.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ const run = async (): Promise<void> => {
if (!input || input === '') return

const useTmpFile = core.getBooleanInput('use-tmp-file')
const useTmpFileForFilter = core.getBooleanInput('use-tmp-file-filter')
const encode = core.getBooleanInput('encode')
const decode = core.getBooleanInput('decode')
const options = core.getInput('options')
Expand Down Expand Up @@ -47,17 +48,30 @@ const run = async (): Promise<void> => {
}

let tmpFile: tmp.FileResult
let shellCommand = `printf '%s' '${sanitizedInput}' ${decodePipe} | jq ${options} '${filter}' ${encodePipe}`
let tmpFileFilter: tmp.FileResult
let filterArg

if (os.platform() === 'win32' || useTmpFileForFilter) {
tmpFileFilter = tmp.fileSync()
fs.writeFileSync(tmpFileFilter.name, filter)
filterArg = `-f ${tmpFileFilter.name}`
} else {
filterArg = `'${filter}'`
}
let shellCommand = `printf '%s' '${sanitizedInput}' ${decodePipe} | jq ${options} ${filterArg} ${encodePipe}`
if (os.platform() === 'win32' || useTmpFile) {
tmpFile = tmp.fileSync()
fs.writeFileSync(tmpFile.name, sanitizedInput)
shellCommand = `cat ${tmpFile.name} ${decodePipe} | jq ${options} '${filter}' ${encodePipe}`
shellCommand = `cat ${tmpFile.name} ${decodePipe} | jq ${options} ${filterArg} ${encodePipe}`
}
core.debug(`Running shell command: ${shellCommand}`)
const proc = exec(shellCommand, (error, stdout, stderr) => {
if (tmpFile) {
tmpFile.removeCallback()
}
if (tmpFileFilter) {
tmpFileFilter.removeCallback()
}
if (error) {
console.error(`Error: ${error}`)
return
Expand Down

0 comments on commit deb978c

Please sign in to comment.