# syntax=docker/dockerfile:1.7
# HODA image, built from the repository root.
#
#   docker build -t hoda .                 # production image (default target)
#   docker build --target check .          # CI: gofmt, go vet, go test

ARG GO_VERSION=1

FROM golang:${GO_VERSION}-alpine AS source
WORKDIR /src
# HODA uses only the standard library; go.sum appears once a dependency does.
COPY go.mod go.sum* ./
RUN --mount=type=cache,target=/go/pkg/mod go mod download
COPY . .

# CI checks: the build fails when formatting, vet or a test fails.
FROM source AS check
RUN --mount=type=cache,target=/go/pkg/mod --mount=type=cache,target=/root/.cache/go-build \
    test -z "$(gofmt -l .)" || { echo "Run gofmt on:"; gofmt -l .; exit 1; } \
    && go vet ./... \
    && go test ./...

FROM source AS build
ARG HODA_VERSION=dev
RUN --mount=type=cache,target=/go/pkg/mod --mount=type=cache,target=/root/.cache/go-build \
    CGO_ENABLED=0 GOOS=linux GOARCH=amd64 \
    go build -trimpath -ldflags="-s -w -X main.version=${HODA_VERSION}" -o /out/hoda ./cmd/hoda \
    && mkdir /out/data

FROM gcr.io/distroless/static:nonroot AS runner
ARG HODA_VERSION=dev
COPY --from=build /out/hoda /hoda
COPY --from=build --chown=65532:65532 /out/data /data
ENV DATA_DIR=/data
EXPOSE 8080
HEALTHCHECK --interval=30s --timeout=5s --start-period=10s --retries=3 CMD ["/hoda", "healthcheck"]
LABEL org.opencontainers.image.title="HODA" \
      org.opencontainers.image.description="Homelab dashboard built from Docker labels" \
      org.opencontainers.image.source="https://git.mariosem.es/msemes/HODA" \
      org.opencontainers.image.version="${HODA_VERSION}"
ENTRYPOINT ["/hoda"]
