Skip to content

Instantly share code, notes, and snippets.

@vimishor
Created April 29, 2015 06:50
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 vimishor/e770294b39f4e83a3136 to your computer and use it in GitHub Desktop.
Save vimishor/e770294b39f4e83a3136 to your computer and use it in GitHub Desktop.
Search packagist.org from CLI
#!/usr/bin/env sh
#
# Search packagist.org from CLI with support for sorting results by the number
# of downloads or favorites.
#
# Temporary solution for https://github.com/composer/packagist/issues/365
#
# Dependencies:
# - [curl](http://curl.haxx.se/)
# - [jq](http://stedolan.github.io/jq/)
#
set -o errexit
PER_PAGE=25
MAX_RESULTS=5
SORT_BY="downloads"
JQ_BIN=$(which jq)
CURL_BIN=$(which curl)
_show_usage () {
cat << EOF
usage: $0 [-l|--limit <results>]
[-s|--sort <downloads|favorites>]
[-h|--help]
<search>
EOF
}
while [ $# -gt 1 ]
do
key="$1"
case $key in
-l|--limit)
if [ "$2" -gt 100 ] || [ "$2" -lt 25 ]; then
echo "[Error]: Limit should be between 25 and 100"
exit 1
else
PER_PAGE="$2"
shift
fi
;;
-s|--sort)
if [ "$2" = "downloads" ] || [ "$2" = "favorites" ]; then
SORT_BY="$2"
shift
else
echo "[Error]: Can't sort by $2"
exit 1
fi
;;
-h|--help)
_show_usage
exit 0
;;
*)
_show_usage
exit 0
;;
esac
shift
done
if [ "$1" = "" ]; then
_show_usage
fi
RESULTS_START=$((PER_PAGE - MAX_RESULTS))
$CURL_BIN -s "https://packagist.org/search.json?per_page=$PER_PAGE&q=$1" | $JQ_BIN "[ .results[]] | sort_by(.${SORT_BY}) | .[$RESULTS_START:$PER_PAGE]"
@vimishor
Copy link
Author

Notes:

  • Will sort only the results from the first page, so if you need betterish results, increase the limit of results per page with --limit <no> (max: 100).
  • Will show only the top 5 results for each search. You can change that from MAX_RESULTS=5

@osbre
Copy link

osbre commented Aug 20, 2019

Dont work on MacOS 10.14.5, i run:
./packagist-search.sh -s=downloads laraveld-vote

and its return nothing.

Any updates here?

Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment