FROM golang:1.23.3-bookworm

ARG TARGETARCH

ARG NODE_MAJOR=20
ARG PNPM_VERSION=9.0.3

RUN apt update && apt install -y ca-certificates curl gnupg unzip \
    && mkdir -p /etc/apt/keyrings \
    && curl -fsSL https://deb.nodesource.com/gpgkey/nodesource-repo.gpg.key | gpg --dearmor -o /etc/apt/keyrings/nodesource.gpg \
    && echo "deb [signed-by=/etc/apt/keyrings/nodesource.gpg] https://deb.nodesource.com/node_$NODE_MAJOR.x nodistro main" | tee /etc/apt/sources.list.d/nodesource.list \
    && apt-get update && apt-get install nodejs -y \
    && npm install --global pnpm@${PNPM_VERSION} \
    # Install Git from "trixie" repository to get a more recent version than
    # the one available in "stable". This can be removed once the version in
    # "stable" is updated to >= 2.42.0 (which supports `--orphan` for `git
    # worktree add`).
    && echo "deb http://deb.debian.org/debian trixie main" > /etc/apt/sources.list.d/trixie.list \
    && apt update \
    && apt install -y -t trixie git \
    && apt-get clean \
    && rm -rf /var/lib/apt/lists/*

ARG USER_ID=1000
ARG GROUP_ID=1000

# To ensure mounts end up with the right permissions on Linux systems,
# create a non-root user with the provided UID and GUID.
RUN addgroup --gid ${GROUP_ID} user \
    && adduser --disabled-password --gecos '' --uid ${USER_ID} --gid ${GROUP_ID} user \
    && mkdir -p /workspaces/kargo/ui/node_modules \
    && chown -R ${USER_ID}:${GROUP_ID} /workspaces/kargo \
    && chown -R ${USER_ID}:${GROUP_ID} /go

USER user

# Configure user writable Go Mod cache path and prepare it to ensure
# it does not end up being owned by root when mounted
ENV GOMODCACHE=/home/user/gocache
RUN mkdir -p $GOMODCACHE

# Configure user writable "global" NPM bin directory,
# and add to path
ENV NPM_CONFIG_PREFIX=/home/user/.npm-global
ENV PATH=$NPM_CONFIG_PREFIX/bin:$PATH
