Files
appfactory-tools/scripts/new-python-app.sh
T
2026-05-20 10:52:10 +02:00

113 lines
2.0 KiB
Bash
Executable File

#!/usr/bin/env bash
set -euo pipefail
CONFIG_FILE="$HOME/.appfactory/env"
if [ ! -f "$CONFIG_FILE" ]; then
echo "Missing config file: $CONFIG_FILE"
exit 1
fi
source "$CONFIG_FILE"
APP_ID="${1:-}"
APP_NAME="${2:-$APP_ID}"
if [ -z "$APP_ID" ]; then
echo "Usage:"
echo " ./new-python-app.sh <app-id> [app-name]"
echo ""
echo "Example:"
echo " ./new-python-app.sh gmail-app \"Gmail App\""
exit 1
fi
TEMPLATE_DIR="$WORKSPACE_DIR/python-fastapi-template"
TARGET_DIR="$WORKSPACE_DIR/$APP_ID"
if [ ! -d "$TEMPLATE_DIR" ]; then
echo "Template not found: $TEMPLATE_DIR"
exit 1
fi
if [ -d "$TARGET_DIR" ]; then
echo "Target already exists: $TARGET_DIR"
exit 1
fi
echo "Creating app from template..."
cp -R "$TEMPLATE_DIR" "$TARGET_DIR"
cd "$TARGET_DIR"
rm -rf .git
sed -i "s/Python FastAPI Template/$APP_NAME/g" app/main.py
cat > app.yml <<EOF
id: $APP_ID
name: $APP_NAME
language: python
version: 1.0.0
base_path: /$APP_ID
port: 8000
status: development
EOF
cat > README.md <<EOF
# $APP_NAME
Generated by AppFactory.
## Local build
\`\`\`bash
docker build -t $APP_ID .
docker run --rm -p 8001:8000 $APP_ID
\`\`\`
Open:
\`\`\`text
http://SERVER_IP:8001/docs
\`\`\`
EOF
echo "Creating Gitea repository..."
CREATE_REPO_RESPONSE="$(curl -sS -X POST \
"$GITEA_URL/api/v1/orgs/$GITEA_ORG/repos" \
-H "Authorization: token $GITEA_TOKEN" \
-H "Content-Type: application/json" \
-d "{
\"name\": \"$APP_ID\",
\"description\": \"$APP_NAME\",
\"private\": false,
\"auto_init\": false
}")"
if echo "$CREATE_REPO_RESPONSE" | grep -q '"message".*"already exists"'; then
echo "Repository already exists in Gitea."
fi
git init
git branch -M main
git remote add origin "$GITEA_URL/$GITEA_ORG/$APP_ID.git"
git add .
git commit -m "Initial $APP_NAME"
git push -u origin main
echo ""
echo "DONE"
echo ""
echo "Repository:"
echo "$GITEA_URL/$GITEA_ORG/$APP_ID"
echo ""
echo "Local path:"
echo "$TARGET_DIR"
echo ""
echo "Test:"
echo "cd $TARGET_DIR"
echo "docker build -t $APP_ID ."
echo "docker run --rm -p 8001:8000 $APP_ID"