#!/bin/bash set -e # Colors for output RED='\033[0;31m' GREEN='\033[0;32m' YELLOW='\033[1;33m' NC='\033[0m' # No Color CONTAINER_NAME="opencode-mountly" IMAGE_NAME="opencode-mountly-dev" BASE_IMAGE="fedora:44" MISSING_TOOLS=( "nodejs" "npm" "cargo" "flatpak" "which" "git" "webkit2gtk4.1" "webkit2gtk4.1-devel" "gtk3-devel" "libsoup3-devel" "openssl-devel" "flatpak-builder" ) # 1. Check if Podman is installed if ! command -v podman &> /dev/null; then echo -e "${RED}Error: Podman is not installed.${NC}" echo "Install it with: sudo dnf install podman" exit 1 fi # 2. Check if we are inside a directory (optional but recommended) if [ ! -d "$(pwd)" ]; then echo -e "${RED}Error: Current directory does not exist.${NC}" exit 1 fi echo -e "${YELLOW}Building custom Podman image with dependencies...${NC}" TEMP_DOCKERFILE=$(mktemp) cat > "$TEMP_DOCKERFILE" << EOF FROM $BASE_IMAGE # Install system dependencies RUN dnf install -y ${MISSING_TOOLS[*]} \ && dnf clean all # Install OpenCode RUN curl -fsSL https://opencode.ai/install | bash ENV PATH="/root/.opencode/bin:$PATH" # Set up workspace WORKDIR /workspace # Default command CMD ["opencode"] EOF # Build the image echo -e "${YELLOW}Downloading Fedora base image and building...${NC}" podman build -t "localhost/$IMAGE_NAME" -f "$TEMP_DOCKERFILE" . # Check if build succeeded if [ $? -ne 0 ]; then echo -e "${RED}Error: Image build failed.${NC}" rm "$TEMP_DOCKERFILE" exit 1 fi # Remove temporary file rm "$TEMP_DOCKERFILE" echo -e "${GREEN}Custom image 'localhost/$IMAGE_NAME' built successfully.${NC}" # Define the config file path on the host HOST_CONFIG="$HOME/.var/app/ai.opencode.opencode/config/opencode/opencode.jsonc" # Check if the config file exists if [ ! -f "$HOST_CONFIG" ]; then echo -e "${YELLOW}Warning: Config file not found at $HOST_CONFIG${NC}" echo "OpenCode will start with default settings." fi echo -e "${GREEN}Starting OpenCode in Podman...${NC}" echo -e "${YELLOW}Working Directory: $(pwd)${NC}" echo -e "${YELLOW}Image: $IMAGE_NAME${NC}" echo -e "${YELLOW}Volume Mount: ./ -> /workspace (SELinux enabled :Z)${NC}" echo -e "${YELLOW}Config File: $HOST_CONFIG -> /tmp/.config/opencode/opencode.jsonc${NC}" echo "----------------------------------------" # 3. Run the Podman command podman run -it --rm \ --name "$CONTAINER_NAME" \ -v "$(pwd)":/workspace:Z \ -w /workspace \ -e HOME=/tmp \ -v "$HOST_CONFIG":/tmp/.config/opencode/opencode.jsonc:Z \ "localhost/$IMAGE_NAME" exit_code=$? # 4. Check exit status if [ $exit_code -eq 0 ]; then echo -e "${GREEN}OpenCode exited successfully.${NC}" else echo -e "${RED}OpenCode exited with error code: $exit_code${NC}" fi exit $exit_code