VERSION 0.7

# Make sure these are up to date
ARG goversion=1.20
ARG distro=alpine3.17

FROM golang:${goversion}-${distro}
WORKDIR /go-workdir

deps:
    # Copying only go.mod and go.sum at first allows earthly to cache the result of
    # `go mod download` unless go.mod or go.sum have changed.
    COPY go.mod go.sum .
    RUN go mod download

    # `go mod download` may have changed these files, so we save them locally when
    # `earthly +deps` is called directly.
    SAVE ARTIFACT go.mod AS LOCAL go.mod
    SAVE ARTIFACT go.sum AS LOCAL go.sum

test-base:
    FROM +deps

    # gcc and g++ are required for `go test -race`
    RUN apk add --update gcc g++
    COPY . .


project-base:
    FROM +deps
    COPY . .

# tidy runs go mod tidy and saves go.mod and go.sum.
tidy:
    FROM +project-base
    RUN go mod tidy
    SAVE ARTIFACT go.mod AS LOCAL go.mod
    SAVE ARTIFACT go.sum AS LOCAL go.sum

# vet runs go vet.
vet:
    FROM +project-base

    # pkg is the package to run 'go vet' against
    ARG pkg = ./...

    RUN go vet $pkg

# fmt formats source files.
fmt:
    FROM +project-base

    RUN go install golang.org/x/tools/cmd/goimports@latest
    RUN goimports -w .
    FOR --sep "\n" gofile IN $(find . -name \'*.go\')
      SAVE ARTIFACT $gofile AS LOCAL $gofile
    END

# build runs 'go build' and saves the binary artifact locally.
build:
    FROM +project-base

    # main-pkg is the path to the main package, which will be used to build a binary artifact.
    ARG mainpkg="."

    # binname is the name to use for the final binary artifact.
    #
    # TODO: with earthly 0.7.3, these can be LET instead of ARG
    ARG projname=$(go list)
    ARG binname=$(basename ${projname})

    RUN go build -o output/${binname} ${mainpkg}

    # build-dir is the local directory to store build artifacts
    ARG builddir="build"

    SAVE ARTIFACT output/${binname} AS LOCAL ${builddir}/${binname}

# test runs tests.
test:
    FROM +test-base

    # pkg is the package to run tests against.
    ARG pkg = ./...

    RUN go test -race $pkg
