| 1 |
VERSION 0.7 |
| 2 |
|
| 3 |
# Make sure these are up to date |
| 4 |
ARG goversion=1.21 |
| 5 |
ARG distro=alpine3.17 |
| 6 |
|
| 7 |
FROM golang:${goversion}-${distro} |
| 8 |
WORKDIR /go-workdir |
| 9 |
|
| 10 |
deps: |
| 11 |
# Copying only go.mod and go.sum at first allows earthly to cache the result of |
| 12 |
# `go mod download` unless go.mod or go.sum have changed. |
| 13 |
COPY go.mod go.sum . |
| 14 |
RUN go mod download |
| 15 |
|
| 16 |
# `go mod download` may have changed these files, so we save them locally when |
| 17 |
# `earthly +deps` is called directly. |
| 18 |
SAVE ARTIFACT go.mod AS LOCAL go.mod |
| 19 |
SAVE ARTIFACT go.sum AS LOCAL go.sum |
| 20 |
|
| 21 |
test-base: |
| 22 |
FROM +deps |
| 23 |
# gcc and g++ are required for `go test -race` |
| 24 |
RUN apk add --update gcc g++ |
| 25 |
COPY . . |
| 26 |
|
| 27 |
project-base: |
| 28 |
FROM +deps |
| 29 |
COPY . . |
| 30 |
|
| 31 |
# tidy runs 'go mod tidy' and updates go.mod and go.sum. |
| 32 |
tidy: |
| 33 |
FROM +project-base |
| 34 |
RUN go mod tidy |
| 35 |
SAVE ARTIFACT go.mod AS LOCAL go.mod |
| 36 |
SAVE ARTIFACT go.sum AS LOCAL go.sum |
| 37 |
|
| 38 |
# vet runs 'go vet'. |
| 39 |
vet: |
| 40 |
FROM +project-base |
| 41 |
|
| 42 |
# pkg is the package to run 'go vet' against. |
| 43 |
ARG pkg = ./... |
| 44 |
|
| 45 |
RUN go vet $path |
| 46 |
|
| 47 |
# fmt formats source files. |
| 48 |
fmt: |
| 49 |
FROM +project-base |
| 50 |
|
| 51 |
RUN go install golang.org/x/tools/cmd/goimports@latest |
| 52 |
RUN goimports -w . |
| 53 |
FOR --sep "\n" gofile IN $(find . -name \'*.go\') |
| 54 |
SAVE ARTIFACT $gofile AS LOCAL $gofile |
| 55 |
END |
| 56 |
|
| 57 |
# generate re-generates code and saves the outputs locally. |
| 58 |
generate: |
| 59 |
FROM +project-base |
| 60 |
|
| 61 |
RUN go install git.sr.ht/~nelsam/hel@latest |
| 62 |
RUN go install golang.org/x/tools/cmd/goimports@latest |
| 63 |
|
| 64 |
RUN go generate ./... |
| 65 |
|
| 66 |
# 'go install' can update our go.mod and go.sum, so save them too. |
| 67 |
SAVE ARTIFACT go.mod AS LOCAL go.mod |
| 68 |
SAVE ARTIFACT go.sum AS LOCAL go.sum |
| 69 |
FOR gofile IN $(find . -name helheim_test.go) |
| 70 |
SAVE ARTIFACT $gofile AS LOCAL $gofile |
| 71 |
END |
| 72 |
|
| 73 |
# test runs tests. |
| 74 |
test: |
| 75 |
FROM +test-base |
| 76 |
|
| 77 |
# pkg is the package to run tests against. |
| 78 |
ARG pkg = ./... |
| 79 |
|
| 80 |
RUN go test -race $pkg |