Created
April 25, 2025 21:42
-
-
Save luiszimmermann/49ab3d7af3398a12476be621fe08cc9a to your computer and use it in GitHub Desktop.
Get the latest version of a Python Package directly from pypi API using only curl and jq.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Verifica se um nome de pacote foi fornecido como argumento | |
if [ -z "$1" ]; then | |
echo "Usage: $0 <python-package-name>" | |
exit 1 | |
fi | |
PACKAGE_NAME=$1 | |
# Verifica se os comandos 'curl' e 'jq' estão disponíveis | |
if ! command -v curl &> /dev/null; then | |
echo "Error: 'curl' not found." | |
exit 1 | |
fi | |
if ! command -v jq &> /dev/null; then | |
echo "Error: 'jq' not found." | |
exit 1 | |
fi | |
# Constrói a URL da API do PyPI | |
PYPI_URL="https://pypi.org/pypi/$PACKAGE_NAME/json" | |
# Usa curl para buscar os dados JSON da API do PyPI | |
# -f: Falha silenciosamente em erros HTTP (como 404 Not Found) | |
# -s: Modo silencioso (não mostra progresso) | |
# -L: Segue redirecionamentos | |
# Usa jq para extrair o campo 'version' dentro do objeto 'info' | |
# -r: Saída "raw" (remove as aspas da string JSON) | |
LATEST_VERSION=$(curl -fsL "$PYPI_URL" | jq -r '.info.version') | |
# Verifica se o comando curl ou jq falhou ou se a versão está vazia/nula | |
# $? contém o código de saída do último comando executado (curl | jq) | |
if [ $? -ne 0 ] || [ -z "$LATEST_VERSION" ] || [ "$LATEST_VERSION" == "null" ]; then | |
echo "Error: Failed to get the version for package '$PACKAGE_NAME'. Check the name or availability on PyPI." >&2 # Envia erro para stderr | |
exit 1 | |
else | |
# Imprime apenas a versão | |
echo "$LATEST_VERSION" | |
fi | |
exit 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment