Skip to content

Instantly share code, notes, and snippets.

@buzztaiki
Created April 17, 2023 12:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save buzztaiki/f74468bcab38cb5b68c3398dc742c552 to your computer and use it in GitHub Desktop.
Save buzztaiki/f74468bcab38cb5b68c3398dc742c552 to your computer and use it in GitHub Desktop.
#!/bin/bash
usage_exit() {
cat <<EOF
Usage: $0 <query>
Search text in source code from GitHub.
Options:
-h show this help
Example:
% gh-code-search GitHub CLI org:cli | head -n 5
https://github.com/cli/gh-webhook/blob/b8fb5686ed688da846f68eb2cc96f517d451712f/go.mod
https://github.com/cli/cli/blob/88cc4d2531cc361225c77558ec85799ff4352c58/pkg/cmd/root/root.go
https://github.com/cli/cli/blob/3a228694396e52f99aceb3496da5b025bd1c067f/pkg/cmd/repo/repo.go
https://github.com/cli/cli/blob/c5ce6855a712afe378665785e2a40f5343001b23/pkg/cmd/pr/pr.go
https://github.com/cli/cli/blob/c5ce6855a712afe378665785e2a40f5343001b23/pkg/cmd/issue/issue.go
EOF
exit "$1"
}
accept=application/vnd.github+json
while getopts "hm" opt; do
case $opt in
h)
usage_exit 0
;;
*)
usage_exit 1 1>&2
;;
esac
done
shift $((OPTIND-1))
[[ $# -gt 0 ]] || usage_exit 1 1>&2
gh api \
-H "Accept: $accept" \
--paginate \
--jq '.items[].html_url' \
"/search/code?q=$(jq -Rr '@uri' <<<"$*")"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment