From d6e014b2587851fcf21d53e5a7de52840b669939 Mon Sep 17 00:00:00 2001 From: Robert Pajak Date: Sat, 2 May 2020 13:35:15 +0000 Subject: [PATCH] Init - Go 1.14.2 - Mage 1.9.0 - GolangCI-Lint 1.26.0 - Add Visual Studio Code with Remote Containers support - Add GitHub Actions workflow using Docker Compose --- .devcontainer/devcontainer.json | 27 +++++++++++++ .gitattributes | 3 ++ .github/workflows/build.yml | 9 +++++ .gitignore | 26 +++++++++++++ .golangci.yml | 68 +++++++++++++++++++++++++++++++++ .vscode/extensions.json | 5 +++ .vscode/launch.json | 17 +++++++++ .vscode/settings.json | 9 +++++ .vscode/tasks.json | 43 +++++++++++++++++++++ Dockerfile | 6 +++ LICENSE | 21 ++++++++++ README.md | 53 +++++++++++++++++++++++++ docker-compose.yml | 10 +++++ foo.go | 7 ++++ foo_test.go | 14 +++++++ go.mod | 5 +++ go.sum | 2 + magefile.go | 28 ++++++++++++++ 18 files changed, 353 insertions(+) create mode 100644 .devcontainer/devcontainer.json create mode 100644 .gitattributes create mode 100644 .github/workflows/build.yml create mode 100644 .gitignore create mode 100644 .golangci.yml create mode 100644 .vscode/extensions.json create mode 100644 .vscode/launch.json create mode 100644 .vscode/settings.json create mode 100644 .vscode/tasks.json create mode 100644 Dockerfile create mode 100644 LICENSE create mode 100644 README.md create mode 100644 docker-compose.yml create mode 100644 foo.go create mode 100644 foo_test.go create mode 100644 go.mod create mode 100644 go.sum create mode 100644 magefile.go diff --git a/.devcontainer/devcontainer.json b/.devcontainer/devcontainer.json new file mode 100644 index 0000000..d3ef9dd --- /dev/null +++ b/.devcontainer/devcontainer.json @@ -0,0 +1,27 @@ +// For format details, see https://aka.ms/vscode-remote/devcontainer.json or +// https://github.com/microsoft/vscode-dev-containers +{ + "name": "Go", + "context": "..", + "dockerFile": "../Dockerfile", + "runArgs": [ + "--cap-add=SYS_PTRACE", + "--security-opt", + "seccomp=unconfined" + ], + // Set *default* container specific settings.json values on container create. + "settings": { + "terminal.integrated.shell.linux": "/bin/bash", + "go.gopath": "/go" + }, + // Add the IDs of extensions you want installed when the container is created. + "extensions": [ + "ms-vscode.go" + ], + // Use 'forwardPorts' to make a list of ports inside the container available locally. + // "forwardPorts": [], + // Use 'postCreateCommand' to run commands after the container is created. + "postCreateCommand": "apt-get update && apt-get install -y git" + // Uncomment to connect as a non-root user. See https://aka.ms/vscode-remote/containers/non-root. + // "remoteUser": "vscode" +} \ No newline at end of file diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 0000000..314766e --- /dev/null +++ b/.gitattributes @@ -0,0 +1,3 @@ +* text=auto eol=lf +*.{cmd,[cC][mM][dD]} text eol=crlf +*.{bat,[bB][aA][tT]} text eol=crlf diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml new file mode 100644 index 0000000..77d743c --- /dev/null +++ b/.github/workflows/build.yml @@ -0,0 +1,9 @@ +name: build +on: push +jobs: + docker-compose: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v2 + - name: Build + run: docker-compose up --abort-on-container-exit diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..9f313ce --- /dev/null +++ b/.gitignore @@ -0,0 +1,26 @@ +# Binaries for programs and plugins +*.exe +*.exe~ +*.dll +*.so +*.dylib + +# Test binary, built with `go test -c` +*.test + +# Output of the go coverage tool, specifically when used with LiteIDE +*.out + +# Dependency directories (remove the comment below to include it) +# vendor/ + +# Visual Studio Code files +.vscode/* +!.vscode/settings.json +!.vscode/tasks.json +!.vscode/launch.json +!.vscode/extensions.json +*.code-workspace + +# Local History for Visual Studio Code +.history/ diff --git a/.golangci.yml b/.golangci.yml new file mode 100644 index 0000000..36afccc --- /dev/null +++ b/.golangci.yml @@ -0,0 +1,68 @@ +linters-settings: + goimports: + local-prefixes: github.com/golang-templates/library + golint: + min-confidence: 0 + misspell: + locale: US + nolintlint: + allow-leading-space: false # don't require machine-readable nolint directives (i.e. with no leading space) + allow-unused: false # report any unused nolint directives + require-explanation: true # don't require an explanation for nolint directives + require-specific: false # don't require nolint directives to be specific about which linter is being skipped + +linters: + # please, do not use `enable-all`: it's deprecated and will be removed soon. + # inverted configuration with `enable-all` and `disable` is not scalable during updates of golangci-lint + disable-all: true + enable: + # default linters: + - deadcode + - errcheck + - gosimple + - govet + - ineffassign + - staticcheck + - structcheck + - typecheck + - unused + - varcheck + # linters disabled by default: + - asciicheck + - bodyclose + - depguard + - dogsled + - dupl + - funlen + - gochecknoglobals + - gochecknoinits + - gocognit + - goconst + - gocritic + - gocyclo + - godot + - godox + - goerr113 + - gofmt + - goimports + - golint + - gomnd + - gomodguard + - goprintffuncname + - gosec + - interfacer + - lll + - maligned + - misspell + - nakedret + - nestif + - nolintlint + - prealloc + - rowserrcheck + - scopelint + - stylecheck + - testpackage + - unconvert + - unparam + - whitespace + - wsl diff --git a/.vscode/extensions.json b/.vscode/extensions.json new file mode 100644 index 0000000..35e2713 --- /dev/null +++ b/.vscode/extensions.json @@ -0,0 +1,5 @@ +{ + "recommendations": [ + "ms-vscode.go" + ] +} \ No newline at end of file diff --git a/.vscode/launch.json b/.vscode/launch.json new file mode 100644 index 0000000..c23774c --- /dev/null +++ b/.vscode/launch.json @@ -0,0 +1,17 @@ +{ + // Use IntelliSense to learn about possible attributes. + // Hover to view descriptions of existing attributes. + // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 + "version": "0.2.0", + "configurations": [ + { + "name": "Launch", + "type": "go", + "request": "launch", + "mode": "auto", + "program": "${fileDirname}", + "env": {}, + "args": [] + } + ] +} \ No newline at end of file diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..370dc89 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,9 @@ +{ + // gopls + "go.useLanguageServer": true, + // golangci-lint + "go.lintTool": "golangci-lint", + "go.lintFlags": [ + "--fast" + ] +} \ No newline at end of file diff --git a/.vscode/tasks.json b/.vscode/tasks.json new file mode 100644 index 0000000..1404991 --- /dev/null +++ b/.vscode/tasks.json @@ -0,0 +1,43 @@ +{ + // See https://go.microsoft.com/fwlink/?LinkId=733558 + // for the documentation about the tasks.json format + "version": "2.0.0", + "tasks": [ + { + "label": "All", + "type": "shell", + "command": "mage -v all", + "problemMatcher": [ + "$go" + ], + "group": "build" + }, + { + "label": "Build", + "type": "shell", + "command": "mage -v build", + "problemMatcher": [ + "$go" + ], + "group": "build" + }, + { + "label": "Lint", + "type": "shell", + "command": "mage -v lint", + "problemMatcher": [ + "$go" + ], + "group": "build" + }, + { + "label": "Test", + "type": "shell", + "command": "mage -v test", + "problemMatcher": [ + "$go" + ], + "group": "build" + } + ] +} \ No newline at end of file diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..27049ba --- /dev/null +++ b/Dockerfile @@ -0,0 +1,6 @@ +# Go +FROM golang:1.14.2 +# mage +RUN GO111MODULE=on go get github.com/magefile/mage@v1.9.0 +# golangci-lint +RUN curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(go env GOPATH)/bin v1.26.0 diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..0476870 --- /dev/null +++ b/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2020 Robert Pająk + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/README.md b/README.md new file mode 100644 index 0000000..ba7ebf4 --- /dev/null +++ b/README.md @@ -0,0 +1,53 @@ +[![Build Status](https://github.com/golang-templates/library/workflows/build/badge.svg)](https://github.com/golang-templates/library/actions?query=workflow%3Abuild) +[![GoDoc](https://godoc.org/github.com/golang-templates/library?status.svg)](https://godoc.org/github.com/golang-templates/library) +[![GitHub Release](https://img.shields.io/github/release/golang-templates/library.svg)](https://github.com/golang-templates/library/releases) +[![Go Report Card](https://goreportcard.com/badge/github.com/golang-templates/library)](https://goreportcard.com/report/github.com/golang-templates/library) + +# Go Library Template + +This is GitHub project template for a Go library. It has been created for ease-of-use for anyone who wants to: + +- quickly get into Go without losing too much time on environment setup, +- create a new repoisitory with basic Continous Integration, +- write Go code on Linux, MacOS, Windows, +- use free tools. + +It includes: + +- editor config: [Visual Studio Code](https://code.visualstudio.com) with [Go](https://code.visualstudio.com/docs/languages/go) and [Remote Container](https://code.visualstudio.com/docs/remote/containers) support, +- dependency management: [Go Modules](https://github.com/golang/go/wiki/Modules), +- linter: [GolangCI-Lint](https://github.com/golangci/golangci-lint), +- build automation: [Mage](https://magefile.org), [Docker](https://docs.docker.com/engine), [Docker Compose](https://docs.docker.com/compose), [GitHub Actions](https://github.com/features/actions). + +`Star` this project if you find it valuable and worth maintaining. + +## Usage + +1. Click the `Use this template` button (alt. clone or download this repository). +1. Replace all occurences of `golang-templates/library` to `your_org/repo_name` in all files. +1. Change [LICENSE](LICENSE) and [README.md](README.md). + +### Setup Development Environment + +Take notice that this project is build in a way that gives the developer a lot of freedom on the development environment setup. Below you can find proposals when using Visual Studio Code. + +- **Bare metal:** Install Go, Visual Studio Code, Mage and GolangCI-Lint (see [Dockerfile](Dockerfile) for Mage and GolangCI-Lint installation commands). +- **Containers:** Install Docker, Visual Studio Code with Remote - Container extension. [Instructions](https://code.visualstudio.com/docs/remote/containers). +- **Remote via SSH**: [Instructions](https://code.visualstudio.com/docs/remote/ssh). + +### Build + +- Terminal: `mage -v all`. +- Visual Studio Code: `Terminal` → `Run Build Task... (CTRL+ALT+B)` → Select `All`. +- Terminal: `docker-compose up --abort-on-container-exit`. This command is executed by CI build (GitHub Action workflow). + +### Maintainance + +1. `Watch` this project to get notified about new releases, issues, etc. +1. Update Go, Mage and GolangCI-Lint version in [Dockerfile](Dockerfile). Take notice that when working on bare metal or via SSH, then you should also to do it manually on your machine. +1. Configure linters via [.golangci.yml](.golangci.yml). +1. Develop tasks/targets in [magefile.go](magefile.go) and [.vscode/tasks.json](.vscode/tasks.json). + +## Contributing + +Simply create an issue or a pull request. diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..14fc5d3 --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,10 @@ +version: "3.7" +services: + builder: + container_name: builder + build: . + working_dir: /app + command: mage -v all + volumes: + - $PWD:/app + \ No newline at end of file diff --git a/foo.go b/foo.go new file mode 100644 index 0000000..a85407e --- /dev/null +++ b/foo.go @@ -0,0 +1,7 @@ +// Package library has a dummy function covered by a unit test. +package library + +// Foo returns "Bar". +func Foo() string { + return "Bar" +} diff --git a/foo_test.go b/foo_test.go new file mode 100644 index 0000000..152cf49 --- /dev/null +++ b/foo_test.go @@ -0,0 +1,14 @@ +package library_test + +import ( + "testing" + + . "github.com/golang-templates/library" +) + +func TestFoo(t *testing.T) { + want := "Bar" + if got := Foo(); got != want { + t.Errorf("Foo() = %v, want %v", got, want) + } +} diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..3a1cf93 --- /dev/null +++ b/go.mod @@ -0,0 +1,5 @@ +module github.com/golang-templates/library + +go 1.14 + +require github.com/magefile/mage v1.9.0 diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..7eb0fc9 --- /dev/null +++ b/go.sum @@ -0,0 +1,2 @@ +github.com/magefile/mage v1.9.0 h1:t3AU2wNwehMCW97vuqQLtw6puppWXHO+O2MHo5a50XE= +github.com/magefile/mage v1.9.0/go.mod h1:z5UZb/iS3GoOSn0JgWuiw7dxlurVYTu+/jHXqQg881A= diff --git a/magefile.go b/magefile.go new file mode 100644 index 0000000..99ca5ee --- /dev/null +++ b/magefile.go @@ -0,0 +1,28 @@ +//+build mage + +package main + +import ( + "github.com/magefile/mage/mg" + "github.com/magefile/mage/sh" +) + +// All full build: build, lint, test. +func All() { + mg.SerialDeps(Build, Lint, Test) +} + +// Build go build. +func Build() error { + return sh.RunV("go", "build", "./...") +} + +// Lint golangci-lint. +func Lint() error { + return sh.RunV("golangci-lint", "run") +} + +// Test go test with race detector and code covarage. +func Test() error { + return sh.RunV("go", "test", "-race", "-covermode=atomic") +}