diff --git a/.dockerignore b/.dockerignore index d0e83c33..e1087be9 100644 --- a/.dockerignore +++ b/.dockerignore @@ -2,15 +2,7 @@ /*.iml .dev -/.git -/.github -/.res /bin /dist -/docs -/.editorconfig -/.gitignore -/.goreleaser.yml -/CHANGELOG.md -/LICENSE -/README.md +/site +/coverage.txt diff --git a/.github/CONTRIBUTING.md b/.github/CONTRIBUTING.md index 2007f466..bae5cca8 100644 --- a/.github/CONTRIBUTING.md +++ b/.github/CONTRIBUTING.md @@ -2,18 +2,20 @@ Hi there! I'm thrilled that you'd like to contribute to this project. Your help is essential for keeping it great. -Contributions to this project are [released](https://help.github.com/articles/github-terms-of-service/#6-contributions-under-repository-license) to the public under the [project's open source license](../LICENSE). +Contributions to this project are [released](https://help.github.com/articles/github-terms-of-service/#6-contributions-under-repository-license) +to the public under the [project's open source license](../LICENSE). ## Submitting a pull request 1. [Fork](https://github.com/crazy-max/diun/fork) and clone the repository 2. Configure and install the dependencies: `go mod download` 3. Create a new branch: `git checkout -b my-branch-name` -4. Make your change -5. Test your code: `go test -covermode=atomic ./...` -6. Build with [GoReleaser](https://goreleaser.com/): `goreleaser release --skip-publish --skip-validate --rm-dist` -7. Push to your fork and [submit a pull request](https://github.com/crazy-max/diun/compare) -8. Pat your self on the back and wait for your pull request to be reviewed and merged. +4. Make your changes +5. Validate: `docker buildx bake validate` +6. Test your code: `docker buildx bake test` +7. Build the project: `docker buildx bake artifact-all image-all` +8. Push to your fork and [submit a pull request](https://github.com/crazy-max/diun/compare) +9. Pat your self on the back and wait for your pull request to be reviewed and merged. Here are a few things you can do that will increase the likelihood of your pull request being accepted: diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 65fe18b2..dbd3636c 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -20,62 +20,45 @@ on: - 'docs/**' - 'mkdocs.yml' +env: + DOCKERHUB_SLUG: crazymax/diun + GHCR_SLUG: ghcr.io/crazy-max/diun + jobs: - go: + test: runs-on: ubuntu-latest steps: - name: Checkout uses: actions/checkout@v2 - - - name: Prepare - id: prep - run: | - if [[ $GITHUB_REF == refs/tags/* ]]; then - echo ::set-output name=tag_name::${GITHUB_REF#refs/tags/} - fi - - - name: Set up Go - uses: actions/setup-go@v2 with: - go-version: 1.15 + fetch-depth: 0 - - name: Cache Go modules - uses: actions/cache@v2 - with: - path: ~/go/pkg/mod - key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }} - restore-keys: | - ${{ runner.os }}-go- - - - name: GoReleaser - uses: goreleaser/goreleaser-action@v2 + name: Set up Docker Buildx + uses: docker/setup-buildx-action@v1 with: version: latest - args: release --skip-publish --rm-dist + buildkitd-flags: --debug - - name: GitHub Release - uses: softprops/action-gh-release@v1 - if: startsWith(github.ref, 'refs/tags/') + name: Test + uses: crazy-max/ghaction-docker-buildx-bake@v1 with: - draft: true - files: | - dist/checksums.txt - dist/*.tar.gz - dist/*.zip - name: ${{ steps.prep.outputs.tag_name }} - env: - GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + targets: test + - + name: Upload coverage + uses: codecov/codecov-action@v1 + with: + file: ./coverage.txt - docker: + build: runs-on: ubuntu-latest - env: - DOCKERHUB_SLUG: crazymax/diun - GHCR_SLUG: ghcr.io/crazy-max/diun + needs: [ test ] steps: - name: Checkout uses: actions/checkout@v2 + with: + fetch-depth: 0 - name: Docker meta id: docker_meta @@ -84,9 +67,13 @@ jobs: images: | ${{ env.DOCKERHUB_SLUG }} ${{ env.GHCR_SLUG }} + tag-semver: | + {{version}} tag-edge: true - tag-match: ^v(\d{1,3}.\d{1,3}.\d{1,3})$ - tag-match-group: 1 + label-custom: | + org.opencontainers.image.title=Diun + org.opencontainers.image.description=Docker image update notifier + org.opencontainers.image.vendor=CrazyMax - name: Set up QEMU uses: docker/setup-qemu-action@v1 @@ -94,7 +81,8 @@ jobs: name: Set up Docker Buildx uses: docker/setup-buildx-action@v1 with: - buildkitd-flags: "--debug" + version: latest + buildkitd-flags: --debug - name: Login to DockerHub if: github.event_name != 'pull_request' @@ -111,21 +99,41 @@ jobs: username: ${{ secrets.GHCR_USERNAME }} password: ${{ secrets.GHCR_TOKEN }} - - name: Build - uses: docker/build-push-action@v2 + name: Build artifacts + uses: crazy-max/ghaction-docker-buildx-bake@v1 with: - context: . - file: ./Dockerfile - platforms: linux/amd64,linux/arm/v6,linux/arm/v7,linux/arm64,linux/386,linux/ppc64le + targets: artifact-all + - + name: Move artifacts + run: | + mv ./dist/**/* ./dist/ + - + name: Upload artifacts + uses: actions/upload-artifact@v2 + with: + name: diun + path: ./dist/* + if-no-files-found: error + - + name: Build image + uses: crazy-max/ghaction-docker-buildx-bake@v1 + with: + files: | + ./docker-bake.hcl + ${{ steps.docker_meta.outputs.bake-file }} + targets: image-all push: ${{ github.event_name != 'pull_request' }} - tags: ${{ steps.docker_meta.outputs.tags }} - build-args: | - VERSION=${{ steps.docker_meta.outputs.version }} - labels: | - ${{ steps.docker_meta.outputs.labels }} - org.opencontainers.image.title=Diun - org.opencontainers.image.description=Docker image update notifier - org.opencontainers.image.vendor=CrazyMax + - + name: GitHub Release + uses: softprops/action-gh-release@v1 + if: startsWith(github.ref, 'refs/tags/') + with: + draft: true + files: | + dist/*.tar.gz + dist/*.zip + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} - name: Check manifest if: github.event_name != 'pull_request' diff --git a/.github/workflows/docs.yml b/.github/workflows/docs.yml index d03ee09b..5aa7e9ee 100644 --- a/.github/workflows/docs.yml +++ b/.github/workflows/docs.yml @@ -30,27 +30,15 @@ jobs: with: fetch-depth: 0 - - name: Prepare - id: prep - run: | - VERSION=edge - RELEASE=false - if [[ $GITHUB_REF == refs/tags/* ]]; then - VERSION=${GITHUB_REF#refs/tags/v} - fi - if [[ $VERSION =~ ^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$ ]]; then - RELEASE=true - fi - echo ::set-output name=release::${RELEASE} - - - name: Build mkdocs Docker image - run: | - docker build -t mkdocs -f ./docs/Dockerfile ./ + name: Set up Docker Buildx + uses: docker/setup-buildx-action@v1 + with: + version: latest - name: Build docs - run: | - docker run --rm -v "$(pwd):/docs" mkdocs build --strict - sudo chown -R $(id -u):$(id -g) ./site + uses: crazy-max/ghaction-docker-buildx-bake@v1 + with: + targets: docs - name: Check GitHub Pages status uses: crazy-max/ghaction-github-status@v2 @@ -58,7 +46,7 @@ jobs: pages_threshold: major_outage - name: Deploy - if: github.event_name != 'pull_request' && (endsWith(github.ref, github.event.repository.default_branch) || steps.prep.outputs.release == 'true') + if: github.event_name != 'pull_request' uses: crazy-max/ghaction-github-pages@v2 with: target_branch: gh-pages diff --git a/.github/workflows/gosum.yml b/.github/workflows/gosum.yml deleted file mode 100644 index d68cc27a..00000000 --- a/.github/workflows/gosum.yml +++ /dev/null @@ -1,44 +0,0 @@ -name: gosum - -on: - push: - branches: - - 'master' - paths: - - '.github/workflows/gosum.yml' - - 'go.mod' - - 'go.sum' - -jobs: - fix: - runs-on: ubuntu-latest - steps: - - - name: Checkout - uses: actions/checkout@v2 - - - name: Set up Go - uses: actions/setup-go@v2 - with: - go-version: 1.15 - - - name: Tidy - run: | - rm -f go.sum - go mod tidy - - - name: Set up Git - env: - GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} - run: | - git config user.name GitHub - git config user.email noreply@github.com - git remote set-url origin https://x-access-token:${GITHUB_TOKEN}@github.com/${GITHUB_REPOSITORY}.git - - - name: Commit and push changes - run: | - git add . - if output=$(git status --porcelain) && [ ! -z "$output" ]; then - git commit --author "github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>" --message "Fix go modules" - git push - fi diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml deleted file mode 100644 index f91b743e..00000000 --- a/.github/workflows/test.yml +++ /dev/null @@ -1,57 +0,0 @@ -name: test - -on: - push: - branches: - - 'master' - tags: - - 'v*' - paths-ignore: - - '**.md' - - '.github/workflows/docs.yml' - - 'docs/**' - - 'mkdocs.yml' - pull_request: - branches: - - 'master' - paths-ignore: - - '**.md' - - '.github/workflows/docs.yml' - - 'docs/**' - - 'mkdocs.yml' - -jobs: - test: - runs-on: ubuntu-latest - steps: - - - name: Checkout - uses: actions/checkout@v2 - - - name: Set up Go - uses: actions/setup-go@v2 - with: - go-version: 1.15 - - - name: Cache Go modules - uses: actions/cache@v2 - with: - path: ~/go/pkg/mod - key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }} - restore-keys: | - ${{ runner.os }}-go- - - - name: Go mod - run: | - go mod download - - - name: Test - run: | - go test -coverprofile=coverage.txt -covermode=atomic -race ./... - - - name: Upload coverage - uses: codecov/codecov-action@v1 - if: success() - with: - token: ${{ secrets.CODECOV_TOKEN }} - file: ./coverage.txt diff --git a/.github/workflows/validate.yml b/.github/workflows/validate.yml new file mode 100644 index 00000000..981d3cb9 --- /dev/null +++ b/.github/workflows/validate.yml @@ -0,0 +1,30 @@ +name: validate + +on: + push: + branches: + - 'master' + tags: + - 'v*' + - 'dockerfile/*' + pull_request: + branches: + - 'master' + +jobs: + validate: + runs-on: ubuntu-latest + steps: + - + name: Checkout + uses: actions/checkout@v2 + - + name: Set up Docker Buildx + uses: docker/setup-buildx-action@v1 + with: + version: latest + - + name: Validate + uses: crazy-max/ghaction-docker-buildx-bake@v1 + with: + targets: validate diff --git a/.gitignore b/.gitignore index bf0cf150..e1087be9 100644 --- a/.gitignore +++ b/.gitignore @@ -4,3 +4,5 @@ .dev /bin /dist +/site +/coverage.txt diff --git a/.goreleaser.yml b/.goreleaser.yml deleted file mode 100644 index bd4e04bb..00000000 --- a/.goreleaser.yml +++ /dev/null @@ -1,45 +0,0 @@ -env: - - GO111MODULE=on - - GOPROXY=https://goproxy.io,direct - -before: - hooks: - - go mod download - -builds: - - - main: ./cmd/main.go - ldflags: - - -s -w -X main.version={{.Version}} - env: - - CGO_ENABLED=0 - goos: - - darwin - - linux - - windows - goarch: - - 386 - - amd64 - - arm - - arm64 - - ppc64le - - s390x - goarm: - - 6 - - 7 - -archives: - - - replacements: - 386: i386 - amd64: x86_64 - format_overrides: - - goos: windows - format: zip - files: - - LICENSE - - README.md - - CHANGELOG.md - -checksum: - name_template: 'checksums.txt' diff --git a/Dockerfile b/Dockerfile index c3afc249..a9b7e069 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,27 +1,35 @@ +# syntax=docker/dockerfile:1.2 ARG GO_VERSION=1.15 -ARG VERSION=dev +ARG GORELEASER_VERSION=0.149.0 -FROM --platform=${BUILDPLATFORM:-linux/amd64} tonistiigi/xx:golang AS xgo - -FROM --platform=${BUILDPLATFORM:-linux/amd64} golang:${GO_VERSION}-alpine AS base -RUN apk add --no-cache curl gcc git musl-dev -COPY --from=xgo / / +FROM --platform=$BUILDPLATFORM golang:${GO_VERSION}-alpine AS base +ARG GORELEASER_VERSION +RUN apk add --no-cache ca-certificates curl gcc file git musl-dev tar +RUN wget -qO- https://github.com/goreleaser/goreleaser/releases/download/v${GORELEASER_VERSION}/goreleaser_Linux_x86_64.tar.gz | tar -zxvf - goreleaser \ + && mv goreleaser /usr/local/bin/goreleaser WORKDIR /src FROM base AS gomod -COPY . . -RUN go mod download +RUN --mount=type=bind,target=.,rw \ + --mount=type=cache,target=/go/pkg/mod \ + go mod tidy && go mod download FROM gomod AS build ARG TARGETPLATFORM ARG TARGETOS ARG TARGETARCH -ARG VERSION -ENV CGO_ENABLED 0 -ENV GOPROXY https://goproxy.io,direct -RUN go build -ldflags "-w -s -X 'main.version=${VERSION}'" -v -o /opt/diun cmd/main.go +ARG TARGETVARIANT +ARG GIT_REF +RUN --mount=type=bind,target=/src,rw \ + --mount=type=cache,target=/root/.cache/go-build \ + --mount=target=/go/pkg/mod,type=cache \ + ./hack/goreleaser.sh -FROM --platform=${TARGETPLATFORM:-linux/amd64} alpine:latest +FROM scratch AS artifacts +COPY --from=build /out/*.tar.gz / +COPY --from=build /out/*.zip / + +FROM --platform=$TARGETPLATFORM alpine LABEL maintainer="CrazyMax" RUN apk --update --no-cache add \ @@ -29,7 +37,7 @@ RUN apk --update --no-cache add \ libressl \ && rm -rf /tmp/* /var/cache/apk/* -COPY --from=build /opt/diun /usr/local/bin/diun +COPY --from=build /usr/local/bin/diun /usr/local/bin/diun RUN diun --version ENV DIUN_DB_PATH="/data/diun.db" diff --git a/cmd/main.go b/cmd/main.go index 5cf6bf39..da83bbe4 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -47,7 +47,7 @@ func main() { kong.Description(fmt.Sprintf("%s. More info: %s", meta.Desc, meta.URL)), kong.UsageOnError(), kong.Vars{ - "version": fmt.Sprintf("%s", version), + "version": version, }, kong.ConfigureHelp(kong.HelpOptions{ Compact: true, diff --git a/docker-bake.hcl b/docker-bake.hcl new file mode 100644 index 00000000..006351e1 --- /dev/null +++ b/docker-bake.hcl @@ -0,0 +1,88 @@ +// Go version +variable "GO_VERSION" { + default = "1.15" +} + +// GitHub reference as defined in GitHub Actions (eg. refs/head/master)) +variable "GITHUB_REF" { + default = "" +} + +target "go-version" { + args = { + GO_VERSION = GO_VERSION + } +} + +// Special target: https://github.com/crazy-max/ghaction-docker-meta#bake-definition +target "ghaction-docker-meta" { + tags = ["crazymax/diun:local"] +} + +group "default" { + targets = ["image-local"] +} + +group "validate" { + targets = ["lint", "vendor-validate"] +} + +target "lint" { + inherits = ["go-version"] + dockerfile = "./hack/lint.Dockerfile" + target = "lint" +} + +target "vendor-validate" { + inherits = ["go-version"] + dockerfile = "./hack/vendor.Dockerfile" + target = "validate" +} + +target "vendor-update" { + inherits = ["go-version"] + dockerfile = "./hack/vendor.Dockerfile" + target = "update" + output = ["."] +} + +target "test" { + inherits = ["go-version"] + dockerfile = "./hack/test.Dockerfile" + target = "test-coverage" + output = ["."] +} + +target "docs" { + dockerfile = "./hack/docs.Dockerfile" + target = "release" + output = ["./site"] +} + +target "artifact" { + args = { + GIT_REF = GITHUB_REF + } + inherits = ["go-version"] + target = "artifacts" + output = ["./dist"] +} + +target "artifact-all" { + inherits = ["artifact"] + platforms = ["linux/amd64", "linux/arm/v5", "linux/arm/v6", "linux/arm/v7", "linux/arm64", "linux/386", "linux/ppc64le", "linux/s390x", "windows/amd64", "windows/386", "darwin/amd64"] +} + +target "image" { + inherits = ["go-version", "ghaction-docker-meta"] +} + +target "image-local" { + inherits = ["image"] + output = ["type=docker"] +} + +target "image-all" { + inherits = ["image"] + platforms = ["linux/amd64", "linux/arm/v6", "linux/arm/v7", "linux/arm64", "linux/386", "linux/ppc64le"] +} diff --git a/docs/install/binary.md b/docs/install/binary.md index a584cf61..6251dcd4 100644 --- a/docs/install/binary.md +++ b/docs/install/binary.md @@ -8,6 +8,7 @@ Choose the archive matching the destination platform: * [diun_{{ git.tag | trim('v') }}_darwin_x86_64.tar.gz]({{ config.repo_url }}/releases/download/v{{ git.tag | trim('v') }}/diun_{{ git.tag | trim('v') }}_darwin_x86_64.tar.gz) * [diun_{{ git.tag | trim('v') }}_linux_arm64.tar.gz]({{ config.repo_url }}/releases/download/v{{ git.tag | trim('v') }}/diun_{{ git.tag | trim('v') }}_linux_arm64.tar.gz) +* [diun_{{ git.tag | trim('v') }}_linux_armv5.tar.gz]({{ config.repo_url }}/releases/download/v{{ git.tag | trim('v') }}/diun_{{ git.tag | trim('v') }}_linux_armv5.tar.gz) * [diun_{{ git.tag | trim('v') }}_linux_armv6.tar.gz]({{ config.repo_url }}/releases/download/v{{ git.tag | trim('v') }}/diun_{{ git.tag | trim('v') }}_linux_armv6.tar.gz) * [diun_{{ git.tag | trim('v') }}_linux_armv7.tar.gz]({{ config.repo_url }}/releases/download/v{{ git.tag | trim('v') }}/diun_{{ git.tag | trim('v') }}_linux_armv7.tar.gz) * [diun_{{ git.tag | trim('v') }}_linux_i386.tar.gz]({{ config.repo_url }}/releases/download/v{{ git.tag | trim('v') }}/diun_{{ git.tag | trim('v') }}_linux_i386.tar.gz) diff --git a/docs/Dockerfile b/hack/docs.Dockerfile similarity index 57% rename from docs/Dockerfile rename to hack/docs.Dockerfile index 46b5f934..c3613dac 100644 --- a/docs/Dockerfile +++ b/hack/docs.Dockerfile @@ -1,7 +1,7 @@ -FROM squidfunk/mkdocs-material:6.1.7 +# syntax=docker/dockerfile:1.2 -RUN \ - apk add --no-cache \ +FROM squidfunk/mkdocs-material:6.1.7 AS base +RUN apk add --no-cache \ git \ git-fast-import \ openssh \ @@ -14,3 +14,10 @@ RUN \ 'mkdocs-macros-plugin' \ && apk del .build gcc musl-dev \ && rm -rf /tmp/* + +FROM base AS generate +RUN --mount=type=bind,target=. \ + mkdocs build --strict --site-dir /tmp/site + +FROM scratch AS release +COPY --from=generate /tmp/site/ / diff --git a/hack/goreleaser.sh b/hack/goreleaser.sh new file mode 100755 index 00000000..0551c6b2 --- /dev/null +++ b/hack/goreleaser.sh @@ -0,0 +1,164 @@ +#!/usr/bin/env sh + +: ${TARGETPLATFORM=} +: ${TARGETOS=} +: ${TARGETARCH=} +: ${TARGETVARIANT=} +: ${CGO_ENABLED=} +: ${GOARCH=} +: ${GOOS=} +: ${GOARM=} +: ${GOBIN=} +: ${GIT_REF=} + +set -eu + +if [ ! -z "$TARGETPLATFORM" ]; then + os="$(echo $TARGETPLATFORM | cut -d"/" -f1)" + arch="$(echo $TARGETPLATFORM | cut -d"/" -f2)" + if [ ! -z "$os" ] && [ ! -z "$arch" ]; then + export GOOS="$os" + export GOARCH="$arch" + if [ "$arch" = "arm" ]; then + case "$(echo $TARGETPLATFORM | cut -d"/" -f3)" in + "v5") + export GOARM="5" + ;; + "v6") + export GOARM="6" + ;; + *) + export GOARM="7" + ;; + esac + fi + fi +fi + +if [ ! -z "$TARGETOS" ]; then + export GOOS="$TARGETOS" +fi + +if [ ! -z "$TARGETARCH" ]; then + export GOARCH="$TARGETARCH" +fi + +if [ "$TARGETARCH" = "arm" ]; then + if [ ! -z "$TARGETVARIANT" ]; then + case "$TARGETVARIANT" in + "v5") + export GOARM="5" + ;; + "v6") + export GOARM="6" + ;; + *) + export GOARM="7" + ;; + esac + else + export GOARM="7" + fi +fi + +if [ "$CGO_ENABLED" = "1" ]; then + case "$GOARCH" in + "amd64") + export CC="x86_64-linux-gnu-gcc" + ;; + "ppc64le") + export CC="powerpc64le-linux-gnu-gcc" + ;; + "s390x") + export CC="s390x-linux-gnu-gcc" + ;; + "arm64") + export CC="aarch64-linux-gnu-gcc" + ;; + "arm") + case "$GOARM" in + "5") + export CC="arm-linux-gnueabi-gcc" + ;; + *) + export CC="arm-linux-gnueabihf-gcc" + ;; + esac + ;; + esac +fi + +if [ "$GOOS" = "wasi" ]; then + export GOOS="js" +fi + +if [ -z "$GOBIN" ] && [ -n "$GOPATH" ] && [ -n "$GOARCH" ] && [ -n "$GOOS" ]; then + export PATH=${GOPATH}/bin/${GOOS}_${GOARCH}:${PATH} +fi + +cat > ./.goreleaser.yml </dev/null 2>&1; then + gitDirty="false" +fi +echo "git dirty: ${gitDirty}" + +flags="" +if [ "$gitDirty" = "true" ]; then + flags="--snapshot" +fi + +set -x +/usr/local/bin/goreleaser release $flags diff --git a/hack/lint.Dockerfile b/hack/lint.Dockerfile new file mode 100644 index 00000000..5ed6926b --- /dev/null +++ b/hack/lint.Dockerfile @@ -0,0 +1,15 @@ +# syntax=docker/dockerfile:1.2 +ARG GO_VERSION=1.15 + +FROM golang:${GO_VERSION}-alpine AS base +RUN apk add --no-cache gcc musl-dev +WORKDIR /src + +FROM golangci/golangci-lint:v1.27-alpine AS golangci-lint + +FROM base AS lint +RUN --mount=type=bind,target=. \ + --mount=type=cache,target=/root/.cache/go-build \ + --mount=type=cache,target=/root/.cache/golangci-lint \ + --mount=from=golangci-lint,source=/usr/bin/golangci-lint,target=/usr/bin/golangci-lint \ + golangci-lint run --timeout 10m0s ./... diff --git a/hack/test.Dockerfile b/hack/test.Dockerfile new file mode 100644 index 00000000..08c33a9a --- /dev/null +++ b/hack/test.Dockerfile @@ -0,0 +1,20 @@ +# syntax=docker/dockerfile:1.2 +ARG GO_VERSION=1.15 + +FROM golang:${GO_VERSION}-alpine AS base +RUN apk add --no-cache gcc musl-dev +WORKDIR /src + +FROM base AS gomod +RUN --mount=type=bind,target=.,rw \ + --mount=type=cache,target=/go/pkg/mod \ + go mod tidy && go mod download + +FROM gomod AS test +RUN --mount=type=bind,target=. \ + --mount=type=cache,target=/go/pkg/mod \ + --mount=type=cache,target=/root/.cache/go-build \ + go test -v -coverprofile=/tmp/coverage.txt -covermode=atomic -race ./... + +FROM scratch AS test-coverage +COPY --from=test /tmp/coverage.txt /coverage.txt diff --git a/hack/vendor.Dockerfile b/hack/vendor.Dockerfile new file mode 100644 index 00000000..d21997ad --- /dev/null +++ b/hack/vendor.Dockerfile @@ -0,0 +1,24 @@ +# syntax=docker/dockerfile:1.2 +ARG GO_VERSION=1.15 + +FROM golang:${GO_VERSION}-alpine AS base +RUN apk add --no-cache git +WORKDIR /src + +FROM base AS vendored +RUN --mount=type=bind,target=.,rw \ + --mount=type=cache,target=/go/pkg/mod \ + go mod tidy && go mod download && \ + mkdir /out && cp go.mod go.sum /out + +FROM scratch AS update +COPY --from=vendored /out / + +FROM vendored AS validate +RUN --mount=type=bind,target=.,rw \ + git add -A && cp -rf /out/* .; \ + if [ -n "$(git status --porcelain -- go.mod go.sum)" ]; then \ + echo >&2 'ERROR: Vendor result differs. Please vendor your package with "docker buildx bake update-vendor"'; \ + git status --porcelain -- go.mod go.sum; \ + exit 1; \ + fi diff --git a/internal/app/diun.go b/internal/app/diun.go index 70193f11..e6b5335d 100644 --- a/internal/app/diun.go +++ b/internal/app/diun.go @@ -101,7 +101,7 @@ func (di *Diun) Start() error { // Start scheduler di.cron.Start() log.Info().Msgf("Next run in %s (%s)", - durafmt.ParseShort(di.cron.Entry(di.jobID).Next.Sub(time.Now())).String(), + durafmt.ParseShort(time.Until(di.cron.Entry(di.jobID).Next)).String(), di.cron.Entry(di.jobID).Next) select {} @@ -116,7 +116,7 @@ func (di *Diun) Run() { defer atomic.StoreUint32(&di.locker, 0) if di.jobID > 0 { defer log.Info().Msgf("Next run in %s (%s)", - durafmt.ParseShort(di.cron.Entry(di.jobID).Next.Sub(time.Now())).String(), + durafmt.ParseShort(time.Until(di.cron.Entry(di.jobID).Next)).String(), di.cron.Entry(di.jobID).Next) } diff --git a/internal/config/config_test.go b/internal/config/config_test.go index 46458486..fa37412f 100644 --- a/internal/config/config_test.go +++ b/internal/config/config_test.go @@ -516,7 +516,7 @@ func TestValidation(t *testing.T) { dec, err := env.Encode("DIUN_", cfg) require.NoError(t, err) for _, value := range dec { - fmt.Println(fmt.Sprintf(`%s=%s`, value.Name, value.Default)) + fmt.Printf(`%s=%s\n`, value.Name, value.Default) } }) } diff --git a/internal/db/migrate.go b/internal/db/migrate.go index bfbe0c7b..5a35fa79 100644 --- a/internal/db/migrate.go +++ b/internal/db/migrate.go @@ -55,7 +55,11 @@ func (c *Client) migration2() error { if err != nil { return err } - defer tx.Rollback() + defer func() { + if err := tx.Rollback(); err != nil { + log.Error().Err(err).Msg("Cannot rollback") + } + }() bucket := tx.Bucket([]byte(bucketManifest)) curs := bucket.Cursor() diff --git a/internal/notif/matrix/client.go b/internal/notif/matrix/client.go index 84bb7910..8e132ddd 100644 --- a/internal/notif/matrix/client.go +++ b/internal/notif/matrix/client.go @@ -11,6 +11,7 @@ import ( "github.com/matrix-org/gomatrix" "github.com/microcosm-cc/bluemonday" "github.com/pkg/errors" + "github.com/rs/zerolog/log" "github.com/russross/blackfriday/v2" ) @@ -42,7 +43,11 @@ func (c *Client) Send(entry model.NotifEntry) error { if err != nil { return errors.Wrap(err, "failed to initialize Matrix client") } - defer m.Logout() + defer func() { + if _, err := m.Logout(); err != nil { + log.Error().Err(err).Msg("Cannot logout") + } + }() user, err := utl.GetSecret(c.cfg.User, c.cfg.UserFile) if err != nil { diff --git a/mkdocs.yml b/mkdocs.yml index 61a645a4..fb54cc53 100644 --- a/mkdocs.yml +++ b/mkdocs.yml @@ -59,7 +59,6 @@ plugins: - exclude: glob: - "_overrides/*" - - "Dockerfile" - git-revision-date-localized: type: iso_datetime - macros