|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +set -e |
| 4 | + |
| 5 | +# Default values |
| 6 | +VARIANT="" |
| 7 | +PORT="8080" |
| 8 | +# TELEGRAM_ENABLED can be set via env var (TELEGRAM_ENABLED=1|true|yes) or via --telegram flag. |
| 9 | +# Initialize from environment if present, otherwise default to false. |
| 10 | +if [ -n "${TELEGRAM_ENABLED:-}" ]; then |
| 11 | + case "$(echo "${TELEGRAM_ENABLED}" | tr '[:upper:]' '[:lower:]')" in |
| 12 | + 1|true|yes|on) TELEGRAM_ENABLED=true ;; |
| 13 | + *) TELEGRAM_ENABLED=false ;; |
| 14 | + esac |
| 15 | +else |
| 16 | + TELEGRAM_ENABLED=false |
| 17 | +fi |
| 18 | +ENV_FILE="" |
| 19 | +COMMAND="run" |
| 20 | + |
| 21 | +# Parse command line arguments |
| 22 | +while [[ $# -gt 0 ]]; do |
| 23 | + case $1 in |
| 24 | + python|ts) |
| 25 | + VARIANT="$1" |
| 26 | + shift |
| 27 | + ;; |
| 28 | + -p|--port) |
| 29 | + PORT="$2" |
| 30 | + shift 2 |
| 31 | + ;; |
| 32 | + --telegram) |
| 33 | + TELEGRAM_ENABLED=true |
| 34 | + shift |
| 35 | + ;; |
| 36 | + --env-file) |
| 37 | + ENV_FILE="$2" |
| 38 | + shift 2 |
| 39 | + ;; |
| 40 | + --help|-h) |
| 41 | + echo "jdcode - Simplified interface for jdcode containers" |
| 42 | + echo "" |
| 43 | + echo "Usage:" |
| 44 | + echo " jdcode [python|ts] [options]" |
| 45 | + echo "" |
| 46 | + echo "Options:" |
| 47 | + echo " python|ts Run Python or TypeScript variant" |
| 48 | + echo " -p, --port PORT Port to expose (default: 8080)" |
| 49 | + echo " --telegram Enable Telegram notifications" |
| 50 | + echo " --env-file FILE Use environment file" |
| 51 | + echo " --help, -h Show this help message" |
| 52 | + echo "" |
| 53 | + echo "Examples:" |
| 54 | + echo " jdcode python # Run Python variant with default settings" |
| 55 | + echo " jdcode ts --port 9000 # Run TypeScript variant on port 9000" |
| 56 | + echo " jdcode python --telegram # Run Python with Telegram notifications" |
| 57 | + exit 0 |
| 58 | + ;; |
| 59 | + *) |
| 60 | + echo "Unknown option: $1" |
| 61 | + exit 1 |
| 62 | + ;; |
| 63 | + esac |
| 64 | +done |
| 65 | + |
| 66 | +# Auto-detect variant if not specified |
| 67 | +if [ -z "$VARIANT" ]; then |
| 68 | + if [ -f "package.json" ] || [ -f "tsconfig.json" ]; then |
| 69 | + VARIANT="ts" |
| 70 | + elif [ -f "requirements.txt" ] || [ -f "pyproject.toml" ]; then |
| 71 | + VARIANT="python" |
| 72 | + else |
| 73 | + echo "Error: Could not detect variant. Please specify 'python' or 'ts'" |
| 74 | + exit 1 |
| 75 | + fi |
| 76 | +fi |
| 77 | + |
| 78 | +# Validate variant |
| 79 | +if [ "$VARIANT" != "python" ] && [ "$VARIANT" != "ts" ]; then |
| 80 | + echo "Error: Invalid variant. Must be 'python' or 'ts'" |
| 81 | + exit 1 |
| 82 | +fi |
| 83 | + |
| 84 | +# Build the docker command |
| 85 | +DOCKER_CMD="docker run -it --rm" |
| 86 | + |
| 87 | +# Determine the system time zone (try multiple methods for portability) |
| 88 | +# Prefer a named timezone (e.g. America/Los_Angeles). Fall back to abbreviation if necessary. |
| 89 | +if command -v timedatectl >/dev/null 2>&1; then |
| 90 | + TIMEZONE=$(timedatectl show -p TimeZone --value 2>/dev/null || true) |
| 91 | +elif [ -f /etc/timezone ]; then |
| 92 | + TIMEZONE=$(cat /etc/timezone 2>/dev/null || true) |
| 93 | +elif [ -L /etc/localtime ]; then |
| 94 | + # try to extract zoneinfo path from symlink |
| 95 | + LINK=$(readlink /etc/localtime 2>/dev/null || true) |
| 96 | + if [ -n "$LINK" ]; then |
| 97 | + TIMEZONE=$(echo "$LINK" | sed 's|.*/zoneinfo/||') |
| 98 | + elif command -v realpath >/dev/null 2>&1; then |
| 99 | + TIMEZONE=$(realpath /etc/localtime 2>/dev/null | sed 's|.*/zoneinfo/||' || true) |
| 100 | + else |
| 101 | + TIMEZONE="" |
| 102 | + fi |
| 103 | +else |
| 104 | + TIMEZONE="" |
| 105 | +fi |
| 106 | + |
| 107 | +# Fallback: use timezone abbreviation if we couldn't find a named timezone |
| 108 | +if [ -z "$TIMEZONE" ]; then |
| 109 | + TIMEZONE=$(date +%Z 2>/dev/null || true) |
| 110 | +fi |
| 111 | + |
| 112 | +# Only add TZ env var if we found something |
| 113 | +if [ -n "$TIMEZONE" ]; then |
| 114 | + DOCKER_CMD="$DOCKER_CMD -e TZ=\"$TIMEZONE\"" |
| 115 | +fi |
| 116 | + |
| 117 | +# Add volume mounts |
| 118 | +DOCKER_CMD="$DOCKER_CMD -v \"\$PWD\":/code" |
| 119 | + |
| 120 | +# Add port mapping |
| 121 | +DOCKER_CMD="$DOCKER_CMD -p $PORT:8080" |
| 122 | + |
| 123 | +# Add environment file if specified |
| 124 | +if [ -n "$ENV_FILE" ] && [ -f "$ENV_FILE" ]; then |
| 125 | + DOCKER_CMD="$DOCKER_CMD --env-file $ENV_FILE" |
| 126 | +fi |
| 127 | + |
| 128 | +# Add Telegram environment variables if enabled |
| 129 | +if [ "$TELEGRAM_ENABLED" = true ]; then |
| 130 | + # Check if required environment variables exist |
| 131 | + if [ -z "$TELEGRAM_BOT_TOKEN" ] || [ -z "$TELEGRAM_CHAT_ID" ]; then |
| 132 | + echo "Error: TELEGRAM_BOT_TOKEN and TELEGRAM_CHAT_ID must be set in environment" |
| 133 | + echo "You can set them with:" |
| 134 | + echo " export TELEGRAM_BOT_TOKEN=your_token" |
| 135 | + echo " export TELEGRAM_CHAT_ID=your_chat_id" |
| 136 | + exit 1 |
| 137 | + fi |
| 138 | + DOCKER_CMD="$DOCKER_CMD -e TELEGRAM_ENABLED=true" |
| 139 | + DOCKER_CMD="$DOCKER_CMD -e TELEGRAM_BOT_TOKEN=\"$TELEGRAM_BOT_TOKEN\"" |
| 140 | + DOCKER_CMD="$DOCKER_CMD -e TELEGRAM_CHAT_ID=\"$TELEGRAM_CHAT_ID\"" |
| 141 | +fi |
| 142 | + |
| 143 | +# Add image name |
| 144 | +DOCKER_CMD="$DOCKER_CMD jdcode-$VARIANT" |
| 145 | + |
| 146 | +# Execute the command |
| 147 | +echo "Running: $DOCKER_CMD" |
| 148 | +eval $DOCKER_CMD |
0 commit comments