Skip to content

Commit

Permalink
Pull request #47: Mr/march merge go v1.17.9
Browse files Browse the repository at this point in the history
Merge in AW/go-starter from mr/march-merge-go-v1.17.9 to master

* commit 'e76ccc77fdeffb3736e11f4afd380f825a6435c1':
  update deps, go 1.17.9
  never automatically load .env.local within test envs, but give this option to do so explicitly via test.DotEnvLoadLocalOrSkipTest(t)
  CHANGELOG multiline ENV inject in .hostenv drone fix and VSCode explicit shutdown on window close in devcontainer.json
  this is a Bad = commit potentially
  WIP dotenv test
  tests for .env overwrite, switch to github.com/subosito/gotenv v1.2.0
  godotenv parsing .env.local and overriding ENV wip, still misses tests
  upgrade sqlboiler, mimetype, go-openapi/runtime, go-openapi/strfmt, go-openapi/validate, lib/pq, rogpeppe/go-internal, stretchr/testify, volatiletech/strmangle, google.golang.org/api, golang.org/x/crypto, golang.org/x/sys
  CHANGELOG Bump github.com/rs/zerolog from 1.25.0 to 1.26.1
  enforce useage of errors.Is, errors.As and using %w for wrapping errors
  Bump github.com/labstack/echo/v4 from 4.6.1 to 4.7.2 changelog
  sqlboiler code gen changes v4.6.0 to v4.8.6
  docs CHANGELOG ref update wiki procedure
  Go 1.17.8, tenv enable + fix os.Setenv to t.Setenv, bump pgFormatter, golangci-lint, lichen, watchexec, yq Dockerfile binary dependencies, add tmux
  Bump github.com/labstack/echo/v4 from 4.6.1 to 4.7.2
  Bump github.com/rs/zerolog from 1.25.0 to 1.26.1
  Bump github.com/volatiletech/sqlboiler/v4 from 4.6.0 to 4.8.6
  Bump github.com/nicksnyder/go-i18n/v2 from 2.1.2 to 2.2.0
  • Loading branch information
majodev committed Apr 15, 2022
2 parents acf2b26 + e76ccc7 commit 6eddec0
Show file tree
Hide file tree
Showing 54 changed files with 1,120 additions and 572 deletions.
3 changes: 2 additions & 1 deletion .devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,10 @@
// The optional 'workspaceFolder' property is the path VS Code should open by default when
// connected. This is typically a file mount in .devcontainer/docker-compose.yml
"workspaceFolder": "/app",
// All containers should stop if we close / reload the VSCode window.
"shutdownAction": "stopCompose",
// Set *default* container specific settings.json values on container create.
"settings": {
"terminal.integrated.shell.linux": null,
// https://github.com/golang/tools/blob/master/gopls/doc/vscode.md#vscode
"go.useLanguageServer": true,
"[go]": {
Expand Down
2 changes: 1 addition & 1 deletion .drone.yml
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ pipeline:
# into the drone user defined per build docker network!
# https://github.com/drone-plugins/drone-docker/issues/193
# https://jpetazzo.github.io/2015/09/03/do-not-use-docker-in-docker-for-ci/
- (env | grep "=" | grep -v -e "DRONE=" -e "DRONE_" -e "CI_" -e "CI=" -e "HOME=" -e "HOSTNAME=" -e "SHELL=" -e "PWD=" -e "PATH=") > .hostenv
- (env | grep "^\S*=" | grep -v -e "DRONE=" -e "DRONE_" -e "CI_" -e "CI=" -e "HOME=" -e "HOSTNAME=" -e "SHELL=" -e "PWD=" -e "PATH=") > .hostenv
- cat .hostenv
- "docker run --env-file .hostenv $${IMAGE_TAG} help"
- "docker run --env-file .hostenv $${IMAGE_TAG} -v"
Expand Down
30 changes: 30 additions & 0 deletions .env.local.sample
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# This is a dotenv file.
# Syntax Overview: https://hexdocs.pm/dotenvy/dotenv-file-format.html
# Parser: github.com/subosito/gotenv

# !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
# DO NOT USE IN PRODUCTION!
# DO NOT COMMIT INTO VERSION CONTROL!
# !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

# Why?
# Use .env.local to easily inject secrets into your server config.

# When?
# Only use this locally, never in production.

# What?
# .env.local.sample is only a "sample" and should never hold any secret values!
# .env.local is .gitignored.
# .env.local is automatically loaded when running app <command>.
# .env.local is **not** automatically loaded when running go test / make test.
# .env.local ENV variables override OS ENV variables.
# .env.local is applied by /internal/config's DefaultServiceConfigFromEnv().

# How?
# Copy this file to `.env.local` to use it, e.g. with the following command:
# cp .env.local.sample .env.local

# Be kind to your colleagues and show them which typical ENV **keys** they
# have to set (without setting the actual value within .env.local.sample):
SECRET_KEY=
9 changes: 8 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,11 @@ tmp
# /assets/mnt folder should stay gitignored!
assets/mnt/**
!assets/mnt/.gitkeep
dumps
dumps

# go debug / delve
__debug_bin

# we support overloading ENV vars by parsing a dotenv formatted file
# this file can be used for local testing (secrets!) but should never be commited into git
.env.local
12 changes: 11 additions & 1 deletion .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ linters:

# ---
# https://github.com/mgechev/revive
# # replacement for the now deprecated official golint linter, see https://github.com/golang/go/issues/38968
# replacement for the now deprecated official golint linter, see https://github.com/golang/go/issues/38968
- revive

# ---
Expand All @@ -21,3 +21,13 @@ linters:
# https://github.com/securego/gosec
# inspects source code for security problems by scanning the Go AST.
- gosec

# ---
# https://github.com/sivchari/tenv
# prefer t.Setenv instead of os.Setenv within test code.
- tenv

# ---
# https://github.com/polyfloyd/go-errorlint
# ensure we are comparing errors via errors.Is, types/values via errors.As and wrap errors with %w.
- errorlint
59 changes: 55 additions & 4 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,63 @@
- All notable changes to this project will be documented in this file.
- The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
- We do not follow [semantic versioning](https://semver.org/).
- All changes are solely **tracked by date** and have a git tag available (from 2021-10-19 onwards):
- format `go-starter-YYYY-MM-DD`
- e.g. [`go-starter-2021-10-19`](https://github.com/allaboutapps/go-starter/releases/tag/go-starter-2021-10-19)
- The latest `master` is considered **stable** and should be periodically merged into our customer projects.
- All changes are solely **tracked by date** and have a **git tag** available (from 2021-10-19 onwards):
- Git tags are formatted like `go-starter-YYYY-MM-DD`. See [GitHub tags](https://github.com/allaboutapps/go-starter/tags) for all available go-starter git tags.
- The latest `master` is considered **stable** and should be periodically merged into our customer projects.
- Please follow the update process in *[I just want to update / upgrade my project!](https://github.com/allaboutapps/go-starter/wiki/FAQ#i-just-want-to-update--upgrade-my-project)*.

## Unreleased
- ...

## 2022-04-15
- Switch [from Go 1.17.1 to Go 1.17.9](https://go.dev/doc/devel/release#go1.17.minor) (requires `./docker-helper.sh --rebuild`).
- **BREAKING** Add [`tenv`](https://github.com/sivchari/tenv) and [`errorlint`](https://github.com/polyfloyd/go-errorlint) linter to our default `.golangci.yml` configuration.
- We switch from `os.Setenv` to [`t.Setenv`](https://pkg.go.dev/testing#T.Setenv) within our own test code.
- **NOTE**: If you have used `os.Setenv` within your `*_test.go` code previously, simply replace those calls by `t.Setenv`.
- **NOTE**: The go-starter base code now properly uses `errors.Is` and `errors.As` for comparisons (and `%w` wrapping where really needed). For a good overview regarding error handling see [Effective Error Handling in Golang](https://earthly.dev/blog/golang-errors/). For example, if you receive linting errors, you'll need to change your code like this:
- Wrong: `if err == sql.ErrNoRows {`
- Valid: `if errors.Is(err, sql.ErrNoRows) {`
- Wrong: `if err != sql.ErrConnDone {`
- Valid: `if !errors.Is(err, sql.ErrConnDone) {`
- Wrong: `gErr := err.(*googleapi.Error)`, Valid:
- `var gErr *googleapi.Error`
- `ok := errors.As(err, &gErr)`
- `Dockerfile` development stage changes (requires `./docker-helper.sh --rebuild`):
- Bump [golang](https://hub.docker.com/_/golang) base image from `golang:1.17.1-buster` to **`golang:1.17.8-buster`**.
- Bump [pgFormatter](https://github.com/darold/pgFormatter) from v5.0 to [v5.2](https://github.com/darold/pgFormatter/releases/tag/v5.2)
- Bump [golangci-lint](https://github.com/golangci/golangci-lint) from v1.42.1 to [v1.45.2](https://github.com/golangci/golangci-lint/blob/master/CHANGELOG.md#v1452)
- Bump [lichen](https://github.com/uw-labs/lichen) from v0.1.4 to [v0.1.5](https://github.com/uw-labs/lichen/compare/v0.1.4...v0.1.5)
- Bump [watchexec](https://github.com/watchexec/watchexec) from v1.17.0 to [v1.18.11](https://github.com/watchexec/watchexec/releases/tag/cli-v1.18.11) (+ switch from gnu to musl)
- Bump [yq](https://github.com/mikefarah/yq) from v4.16.2 to [v4.24.2](https://github.com/mikefarah/yq/releases/tag/v4.24.2)
- Bump [gotestsum](https://github.com/gotestyourself/gotestsum) from v1.7.0 to [v1.8.0](https://github.com/gotestyourself/gotestsum/releases/tag/v1.8.0)
- Adds [tmux](https://github.com/tmux/tmux) (debian apt managed)
- `go.mod` changes:
- Major: [Bump `github.com/rubenv/sql-migrate` from v0.0.0-20210614095031-55d5740dbbcc to v1.1.1](https://github.com/rubenv/sql-migrate/compare/55d5740dbbccbaa4934009263b37ba52d837241f...v1.1.1) (though this should not lead to any major changes)
- Minor: [Bump github.com/volatiletech/sqlboiler/v4 from 4.6.0 to v4.9.2](https://github.com/volatiletech/sqlboiler/blob/v4.9.2/CHANGELOG.md#v492---2022-04-11) (your generated model might slightly change, minor changes).
- Note that v5 will prefer wrapping errors (e.g. `sql.ErrNoRows`) to retain the stack trace, thus it's about time for us to start to enforce proper `errors.Is` checks in our codebase (see above).
- Minor: [#178: Bump github.com/labstack/echo/v4 from 4.6.1 to 4.7.2](https://github.com/allaboutapps/go-starter/pull/178) (support for HEAD method query params binding, minor changes).
- Minor: [#160: Bump github.com/rs/zerolog from 1.25.0 to 1.26.1](https://github.com/allaboutapps/go-starter/pull/160) (minor changes).
- Minor: [#179: Bump github.com/nicksnyder/go-i18n/v2 from 2.1.2 to 2.2.0](https://github.com/allaboutapps/go-starter/pull/179) (minor changes).
- Minor: [Bump `github.com/gabriel-vasile/mimetype` from v1.3.1 to v1.4.0](https://github.com/gabriel-vasile/mimetype/releases/tag/v1.4.0)
- Minor: [Bump `github.com/go-openapi/runtime` from v0.22.0 to v0.23.3](https://github.com/go-openapi/runtime/compare/v0.22.0...v0.23.3)
- Patch: [Bump `github.com/go-openapi/strfmt` from v0.21.1 to v0.21.2](https://github.com/go-openapi/strfmt/compare/v0.21.1...v0.21.2)
- Patch: [Bump `github.com/go-openapi/validate` from v0.20.3 to v0.21.0](https://github.com/go-openapi/validate/compare/v0.20.3...v0.21.0)
- Patch: [Bump `github.com/lib/pq` from v1.10.3 to v1.10.5](https://github.com/lib/pq/compare/v1.10.3...v1.10.5)
- Patch: [Bump `github.com/rogpeppe/go-internal` from v1.8.0 to v1.8.1](https://github.com/rogpeppe/go-internal/releases/tag/v1.8.1)
- Patch: [Bump `github.com/stretchr/testify` from v1.7.0 to v1.7.1](https://github.com/stretchr/testify/compare/v1.7.0...v1.7.1)
- Patch: [Bump `github.com/volatiletech/strmangle` from v0.0.1 to v0.0.2](https://github.com/volatiletech/strmangle/compare/v0.0.1...v0.0.2)
- Minor: [Bump `google.golang.org/api` from v0.63.0 to v0.74.0](https://github.com/googleapis/google-api-go-client/compare/v0.63.0...v0.74.0)
- Minor: [Bump `github.com/BurntSushi/toml` from v1.0.0 to v1.1.0](https://github.com/BurntSushi/toml/releases/tag/v1.1.0)
- Bump `golang.org/x/crypto` from v0.0.0-20211215165025-cf75a172585e to v0.0.0-20220411220226-7b82a4e95df4
- Bump `golang.org/x/sys` from v0.0.0-20211210111614-af8b64212486 to v0.0.0-20220412211240-33da011f77ad
- We now support overriding `ENV` variables during **local** development through a `.env.local` dotenv file.
- This does not require a development container restart.
- We override the env within the app process through `config.DefaultServiceConfigFromEnv()`, so this does not mess with the actual container ENV.
- See `.env.local.sample` for further instructions to use this.
- Note that `.env.local` is **NEVER automatically** applied during **test runs**. If you really need that, use the specialized `test.DotEnvLoadLocalOrSkipTest` helper before loading up your server within that very test! This ensures that this test is automatically skipped if the `.env.local` file is no longer available.
- VSCode windows closes now explicitly stop Docker containers via [`shutdownAction: "stopCompose"`](https://code.visualstudio.com/docs/remote/devcontainerjson-reference) within `.devcontainer.json`.
- Use `./docker-helper --halt` or other `docker` or `docker-compose` management commands to do this explicitly instead.
- Drone CI specific (minor): Fix multiline ENV variables were messing up our `.hostenv` for `docker run` command testing of the final image.

## 2022-03-28

Expand Down
27 changes: 14 additions & 13 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
# --- https://hub.docker.com/_/golang
# --- https://github.com/microsoft/vscode-remote-try-go/blob/master/.devcontainer/Dockerfile
### -----------------------
FROM golang:1.17.1-buster AS development
FROM golang:1.17.9-buster AS development

# Avoid warnings by switching to noninteractive
ENV DEBIAN_FRONTEND=noninteractive
Expand Down Expand Up @@ -57,6 +57,7 @@ RUN apt-get update \
xz-utils \
postgresql-client-12 \
icu-devtools \
tmux \
# --- END DEVELOPMENT ---
#
&& apt-get clean \
Expand All @@ -77,9 +78,9 @@ ENV LANG en_US.UTF-8
# https://github.com/darold/pgFormatter/releases
RUN mkdir -p /tmp/pgFormatter \
&& cd /tmp/pgFormatter \
&& wget https://github.com/darold/pgFormatter/archive/v5.0.tar.gz \
&& tar xzf v5.0.tar.gz \
&& cd pgFormatter-5.0 \
&& wget https://github.com/darold/pgFormatter/archive/v5.2.tar.gz \
&& tar xzf v5.2.tar.gz \
&& cd pgFormatter-5.2 \
&& perl Makefile.PL \
&& make && make install \
&& rm -rf /tmp/pgFormatter
Expand All @@ -88,16 +89,16 @@ RUN mkdir -p /tmp/pgFormatter \
# https://github.com/gotestyourself/gotestsum/releases
RUN mkdir -p /tmp/gotestsum \
&& cd /tmp/gotestsum \
&& wget https://github.com/gotestyourself/gotestsum/releases/download/v1.7.0/gotestsum_1.7.0_linux_amd64.tar.gz \
&& tar xzf gotestsum_1.7.0_linux_amd64.tar.gz \
&& wget https://github.com/gotestyourself/gotestsum/releases/download/v1.8.0/gotestsum_1.8.0_linux_amd64.tar.gz \
&& tar xzf gotestsum_1.8.0_linux_amd64.tar.gz \
&& cp gotestsum /usr/local/bin/gotestsum \
&& rm -rf /tmp/gotestsum

# go linting: (this package should NOT be installed via go get)
# https://github.com/golangci/golangci-lint#binary
# https://github.com/golangci/golangci-lint/releases
RUN curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh \
| sh -s -- -b $(go env GOPATH)/bin v1.42.1
| sh -s -- -b $(go env GOPATH)/bin v1.45.2

# go swagger: (this package should NOT be installed via go get)
# https://github.com/go-swagger/go-swagger/releases
Expand All @@ -107,8 +108,8 @@ RUN curl -o /usr/local/bin/swagger -L'#' \

# lichen: go license util
# TODO: Install from static binary as soon as it becomes available.
# https://github.com/uw-labs/lichen/releases
RUN go install github.com/uw-labs/lichen@v0.1.4
# https://github.com/uw-labs/lichen/tags
RUN go install github.com/uw-labs/lichen@v0.1.5

# cobra-cli: cobra cmd scaffolding generator
# TODO: Install from static binary as soon as it becomes available.
Expand All @@ -119,16 +120,16 @@ RUN go install github.com/spf13/cobra-cli@v1.3.0
# https://github.com/watchexec/watchexec/releases
RUN mkdir -p /tmp/watchexec \
&& cd /tmp/watchexec \
&& wget https://github.com/watchexec/watchexec/releases/download/cli-v1.17.0/watchexec-1.17.0-x86_64-unknown-linux-gnu.tar.xz \
&& tar xf watchexec-1.17.0-x86_64-unknown-linux-gnu.tar.xz \
&& cp watchexec-1.17.0-x86_64-unknown-linux-gnu/watchexec /usr/local/bin/watchexec \
&& wget https://github.com/watchexec/watchexec/releases/download/cli-v1.18.11/watchexec-1.18.11-x86_64-unknown-linux-musl.tar.xz \
&& tar xf watchexec-1.18.11-x86_64-unknown-linux-musl.tar.xz \
&& cp watchexec-1.18.11-x86_64-unknown-linux-musl/watchexec /usr/local/bin/watchexec \
&& rm -rf /tmp/watchexec

# yq
# https://github.com/mikefarah/yq/releases
RUN mkdir -p /tmp/yq \
&& cd /tmp/yq \
&& wget https://github.com/mikefarah/yq/releases/download/v4.16.2/yq_linux_amd64.tar.gz \
&& wget https://github.com/mikefarah/yq/releases/download/v4.24.2/yq_linux_amd64.tar.gz \
&& tar xzf yq_linux_amd64.tar.gz \
&& cp yq_linux_amd64 /usr/local/bin/yq \
&& rm -rf /tmp/yq
Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ sql-format: ##- (opt) Formats all *.sql files.
@find ${PWD} -path "*/tmp/*" -prune -name ".*" -prune -o -type f -iname "*.sql" -print \
| grep --invert "/app/dumps/" \
| grep --invert "/app/test/" \
| xargs -i pg_format {} -o {}
| xargs -i pg_format --inplace {}

sql-check-files: sql-check-syntax sql-check-migrations-unnecessary-null ##- (opt) Check syntax and unnecessary use of NULL keyword.

Expand Down
5 changes: 3 additions & 2 deletions cmd/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package cmd

import (
"context"
"errors"
"fmt"
"net/http"
"os"
Expand Down Expand Up @@ -110,7 +111,7 @@ func runServer() {

go func() {
if err := s.Start(); err != nil {
if err == http.ErrServerClosed {
if errors.Is(err, http.ErrServerClosed) {
log.Info().Msg("Server closed")
} else {
log.Fatal().Err(err).Msg("Failed to start server")
Expand All @@ -125,7 +126,7 @@ func runServer() {
ctx, cancel = context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()

if err := s.Shutdown(ctx); err != nil && err != http.ErrServerClosed {
if err := s.Shutdown(ctx); err != nil && !errors.Is(err, http.ErrServerClosed) {
log.Fatal().Err(err).Msg("Failed to gracefully shut down server")
}
}
Loading

0 comments on commit 6eddec0

Please sign in to comment.