Reputation: 25
Trying to run command this from a Python script:
gh api "/search/code?q=somecode" --jq ".items[] | { "file": .path, "repo": .repository[\"full_name\"] } "
by way of:
output = subprocess.run( [ 'gh', f'api "/search/code?q={CODE}"', '--jq ".items[] | {{ "file": .path, "repo": .repository[{}] }} "'.format('\\"full_name\\"') ])
to no avail.
I've also tried:
.format(r'\"full_name\"')
which, afaik, is supposed to work, but I get
Command '['gh', 'api "/search/code?q=somecode"', '--jq ".items[] | { "file": .path, "repo": .repository[\\"full_name\\"] } "']' returned non-zero exit status 1.
Using .format(repr('\"full_name\"'))
yields:
Command '['gh', 'api "/search/code?q=staging-procore-tech-docs+org:procore"', '--jq ".items[] | { "file": .path, "repo": .repository[\'"full_name"\'] } "']' returned non-zero exit status 1.
Thanks in advance! I've spent at least 3 hours reading docs, SO and asking friends
Upvotes: 1
Views: 475
Reputation: 11621
I have likewise attempted this on my Mac OS X (M1 chip) and ran into a similar issue on my end. I had originally installed the GitHub CLI (gh
) via homebrew (brew
).
I have few points I would suggest fixing:
shlex.split
to (unequivocally) split multi-word arguments into a list of argumentswhich gh
in a terminalrf''
template- or f- string, where the r
is for raw string so backslashes \
are automatically escapedPutting all this together:
from __future__ import annotations
import shlex
import subprocess
CODE = 'somecode'
# full path to `gh` command
GH_BINARY = '/opt/homebrew/bin/gh'
cmd: list[str] = shlex.split(rf'{GH_BINARY} api "/search/code?q={CODE}" --jq ".items[] | {{ "file": .path, "repo": .repository[\"full_name\"]}} "')
result = subprocess.run(cmd, capture_output=True)
print('Return Code:', result.returncode)
print('Output:', result.stdout.decode())
Output (redacted):
Return Code: 0
Output: {"file":"tuixiangzi-control/Debug/tuixiangzi-control.Build.CppClean.log","repo":"ChaoFeng-alone/tuixiangzi"}
{"file":"tuixiangzi-easyx/Debug/tuixiangzi-easyx.Build.CppClean.log","repo":"ChaoFeng-alone/tuixiangzi"}
...
Upvotes: 1