Files
gitea-cli-utils/publish_to_gitea.sh
2024-02-16 12:19:55 -06:00

61 lines
1.7 KiB
Bash
Executable File

#!/bin/bash
# Variables
GITEA_URL=$1 # Gitea server URL, e.g., https://gitea.example.com
USERNAME=$2 # Your Gitea username
OWNER=$3 # The owner of the package (user or organization)
PROJECT_DIR=$4 # Path to the PHP project directory (not zipped)
VERSION=$5 # Package version (optional)
CONFIG_DIR="$HOME/.config/gitea-cli-tools"
TOKEN_FILE="$CONFIG_DIR/${USERNAME}_token"
echo "$TOKEN_FILE"
# Check if the token file exists and use it
if [ -f "$TOKEN_FILE" ]; then
echo "Using stored token..."
PASSWORD_OR_TOKEN=$(base64 --decode < "$TOKEN_FILE")
else
# Prompt for password securely
read -sp "Enter your Gitea password or token: " PASSWORD
echo
# Ask if the user wants to store the password/token
read -p "Do you want to store the token for future use? (y/n): " STORE_RESPONSE
if [[ $STORE_RESPONSE =~ ^[Yy]$ ]]; then
# Ensure the configuration directory exists
mkdir -p "$CONFIG_DIR"
# Encode and save the token/password
echo "$PASSWORD" | base64 > "$TOKEN_FILE"
echo "Token stored."
fi
PASSWORD_OR_TOKEN=$(printf '%q' "$PASSWORD")
fi
# Create a ZIP file from the project directory
ZIP_NAME="package-$(date +%Y%m%d%H%M%S).zip"
zip -r $ZIP_NAME $PROJECT_DIR
# Check if version is provided
if [ -z "$VERSION" ]; then
URL="$GITEA_URL/api/packages/$OWNER/composer"
else
URL="$GITEA_URL/api/packages/$OWNER/composer?version=$VERSION"
fi
# Publish the package
curl --user $USERNAME:$PASSWORD_OR_TOKEN \
--upload-file $ZIP_NAME \
$URL
# Check for successful upload
if [ $? -eq 0 ]; then
echo "Package uploaded successfully."
else
echo "Failed to upload the package."
fi
# Optionally, remove the ZIP file after upload
rm $ZIP_NAME