Skip to content

Commit

Permalink
Add initial implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
vearutop committed Sep 26, 2020
1 parent c79839b commit ca2de71
Show file tree
Hide file tree
Showing 20 changed files with 338 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/.idea
50 changes: 50 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
# Go development helpers

## Usage

Add a test file (e.g. `make_test.go`) to your module with unused import.
```go
package mymodule_test

import _ "github.com/bool64/dev" // Include development helpers to project.
```

Add `Makefile` to your module with includes standard targets.
```Makefile
GOLANGCI_LINT_VERSION := "v1.31.0" # Optional.

# The head of Makefile determines location of dev-go to include standard targets.
GO ?= go
export GO111MODULE = on

ifneq "$(GOFLAGS)" ""
$(info GOFLAGS: ${GOFLAGS})
endif

ifneq "$(wildcard ./vendor )" ""
$(info >> using vendor)
modVendor = -mod=vendor
ifeq (,$(findstring -mod,$(GOFLAGS)))
export GOFLAGS := ${GOFLAGS} ${modVendor}
endif
ifneq "$(wildcard ./vendor/github.com/bool64/dev)" ""
DEVGO_PATH := ./vendor/github.com/bool64/dev
endif
endif

ifeq ($(DEVGO_PATH),)
DEVGO_PATH := $(shell GO111MODULE=on $(GO) list ${modVendor} -f '{{.Dir}}' -m github.com/bool64/dev)
ifeq ($(DEVGO_PATH),)
$(info Module github.com/bool64/dev not found, downloading.)
DEVGO_PATH := $(shell export GO111MODULE=on && $(GO) mod tidy && $(GO) list -f '{{.Dir}}' -m github.com/bool64/dev)
endif
endif

-include $(DEVGO_PATH)/makefiles/main.mk
-include $(DEVGO_PATH)/makefiles/lint.mk
-include $(DEVGO_PATH)/makefiles/test-unit.mk
-include $(DEVGO_PATH)/makefiles/github-actions.mk

# Add your custom targets here.

```
11 changes: 11 additions & 0 deletions doc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// Package dev contains reusable development helpers.
package dev

// These imports workaround `go mod vendor` prune.
//
// See https://github.com/golang/go/issues/26366.
import (
_ "github.com/bool64/dev/makefiles"
_ "github.com/bool64/dev/scripts"
_ "github.com/bool64/dev/templates/.github/workflows"
)
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module github.com/bool64/dev

go 1.11
Empty file added go.sum
Empty file.
2 changes: 2 additions & 0 deletions makefiles/doc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// Package makefiles contains Makefile includes.
package makefiles
7 changes: 7 additions & 0 deletions makefiles/github-actions.mk
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
GO ?= go

## Replace GitHub Actions from template
github-actions:
@rm -rf $(PWD)/.github && cp -r $(DEVGO_PATH)/templates/.github $(PWD)/ && rm -f $(PWD)/.github/workflows/doc.go && git add $(PWD)/.github

.PHONY: github-actions
35 changes: 35 additions & 0 deletions makefiles/help.mk
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
.PHONY: help

.DEFAULT_GOAL := help
HELP_WIDTH=" "
help:
@printf "Usage\n";
@awk '{ \
if ($$0 ~ /^.PHONY: [a-zA-Z\-\_0-9]+$$/) { \
helpCommand = substr($$0, index($$0, ":") + 2); \
if (helpMessage) { \
printf " \033[32m%-20s\033[0m %s\n", \
helpCommand, helpMessage; \
helpMessage = ""; \
} \
} else if ($$0 ~ /^[a-zA-Z\-\_0-9.]+:/) { \
helpCommand = substr($$0, 0, index($$0, ":")); \
if (helpMessage) { \
printf " \033[32m%-20s\033[0m %s\n", \
helpCommand, helpMessage; \
helpMessage = ""; \
} \
} else if ($$0 ~ /^##/) { \
if (helpMessage) { \
helpMessage = helpMessage"\n"$(HELP_WIDTH)substr($$0, 3); \
} else { \
helpMessage = substr($$0, 3); \
} \
} else { \
if (helpMessage) { \
print "\n"$(HELP_WIDTH)helpMessage"\n" \
} \
helpMessage = ""; \
} \
}' \
$(MAKEFILE_LIST)
11 changes: 11 additions & 0 deletions makefiles/lint.mk
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
GO ?= go

## Check with golangci-lint
lint:
@DEVGO_PATH=$(DEVGO_PATH) GOLANGCI_LINT_VERSION=$(GOLANGCI_LINT_VERSION) bash $(DEVGO_SCRIPTS)/lint.sh

## Apply goimports and gofmt
fix-lint:
@DEVGO_PATH=$(DEVGO_PATH) bash $(DEVGO_SCRIPTS)/fix.sh

.PHONY: lint fix-lint
16 changes: 16 additions & 0 deletions makefiles/main.mk
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
GO ?= go

PWD = $(shell pwd)

# Detecting GOPATH and removing trailing "/" if any
GOPATH = $(realpath $(shell $(GO) env GOPATH))

ifneq "$(wildcard ./vendor )" ""
modVendor = -mod=vendor
endif
export MODULE_NAME := $(shell test -f go.mod && GO111MODULE=on $(GO) list $(modVendor) -m)

DEVGO_PATH ?= $(PWD)/vendor/github.com/bool64/dev
DEVGO_SCRIPTS ?= $(DEVGO_PATH)/scripts

-include $(DEVGO_PATH)/makefiles/help.mk
8 changes: 8 additions & 0 deletions makefiles/test-unit.mk
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
GO ?= go

## Run unit tests
test-unit:
@echo "Running unit tests."
@$(GO) test -gcflags=-l -coverprofile=unit.coverprofile -count 5 -covermode=atomic -race ./...

.PHONY: test-unit
15 changes: 15 additions & 0 deletions scripts/.golangci-internal.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# See https://github.com/golangci/golangci-lint/blob/master/.golangci.example.yml
run:
tests: false

linters-settings:
unused:
check-exported: true
unparam:
check-exported: true

linters:
disable-all: true
enable:
- unused
- unparam
29 changes: 29 additions & 0 deletions scripts/.golangci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# See https://github.com/golangci/golangci-lint/blob/master/.golangci.example.yml
run:
tests: true

linters-settings:
errcheck:
check-type-assertions: true
check-blank: true
gocyclo:
min-complexity: 20
dupl:
threshold: 100
misspell:
locale: US
unused:
check-exported: false
unparam:
check-exported: true

linters:
enable-all: true
disable:
- lll
- maligned
- gochecknoglobals
- gomnd

issues:
exclude-use-default: false
2 changes: 2 additions & 0 deletions scripts/doc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// Package scripts contains helper shell scripts.
package scripts
28 changes: 28 additions & 0 deletions scripts/fix.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#!/usr/bin/env bash

[ -z "$GOBIN" ] && GOBIN=$(go env GOPATH)/bin
[[ ":$PATH:" != *":$GOBIN:"* ]] && PATH="${GOBIN}:${PATH}"

echo "Fixing imports and fmt..."

[ -z "$GO" ] && GO=go
SOURCES_TO_LINT=$(find . -name '*.go' -not -path "./vendor/*")

# checking if gogroup is available
# gogroup enforces import grouping: https://github.com/vasi-stripe/gogroup
if ! command -v gogroup > /dev/null ; then \
echo "Installing gogroup..."; \
bash -c "cd /tmp;GO111MODULE=on $GO get github.com/vasi-stripe/gogroup/cmd/[email protected]";
fi

gogroup -order std,other -rewrite ${SOURCES_TO_LINT}

# checking if gofumpt is available
# gofumpt is a drop-in replacement for gofmt with stricter formatting: https://github.com/mvdan/gofumpt
if ! command -v gofumpt > /dev/null ; then \
echo "Installing gofumpt..."; \
bash -c "cd /tmp;GO111MODULE=on $GO get mvdan.cc/[email protected]";
fi

# simplify code
gofumpt -s -w ${SOURCES_TO_LINT} &>/dev/null
32 changes: 32 additions & 0 deletions scripts/lint.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#!/usr/bin/env bash

[ -z "$GO" ] && GO=go
[ -z "$GOLANGCI_LINT_VERSION" ] && GOLANGCI_LINT_VERSION="v1.31.0"

# detecting GOPATH and removing trailing "/" if any
GOPATH="$(go env GOPATH)"
GOPATH=${GOPATH%/}

# adding GOBIN to PATH
[[ ":$PATH:" != *"$GOPATH/bin"* ]] && PATH=$PATH:"$GOPATH"/bin

# checking if golangci-lint is available
if ! command -v golangci-lint-"$GOLANGCI_LINT_VERSION" > /dev/null ; then
echo "Installing golangci-lint $GOLANGCI_LINT_VERSION...";
curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b /tmp "$GOLANGCI_LINT_VERSION" && mv /tmp/golangci-lint "$GOPATH"/bin/golangci-lint-"$GOLANGCI_LINT_VERSION"
fi

this_path=$(dirname "$0")

golangci_yml="./.golangci.yml"
if [ ! -f "./.golangci.yml" ]; then
golangci_yml="$this_path"/.golangci.yml
fi

echo "Checking packages."
golangci-lint-"$GOLANGCI_LINT_VERSION" run -c "$golangci_yml" ./... || exit 1

if [[ -d "./internal" && -d "./cmd" ]]; then
echo "Checking unused exported symbols in internal packages."
golangci-lint-"$GOLANGCI_LINT_VERSION" run -c "$this_path"/.golangci-internal.yml ./internal/... ./cmd/... || exit 1
fi
19 changes: 19 additions & 0 deletions scripts/version-ldflags.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#!/usr/bin/env bash

# skip branch/revision with missing git repo.
if [[ -d .git ]] || git rev-parse --git-dir > /dev/null 2>&1; then
branch=$(git symbolic-ref HEAD 2>/dev/null)
[[ -z "$VERSION" ]] && VERSION=$(git symbolic-ref -q --short HEAD || git describe --tags --exact-match)
revision=$(git log -1 --pretty=format:"%H" 2>/dev/null)
fi

build_user="$USER"
build_date=$(date +%FT%T%Z)

#if [[ -d vendor ]] && [[ ! -e go.mod ]]; then
# version_pkg="$MODULE_NAME"/vendor/github.com/bool64/dev-go/version
#else
version_pkg=github.com/bool64/dev-go/version
#fi

echo -X "$version_pkg".version="$VERSION" -X "$version_pkg".branch="$branch" -X "$version_pkg".revision="$revision" -X "$version_pkg".buildUser="$build_user" -X "$version_pkg".buildDate="$build_date"
2 changes: 2 additions & 0 deletions templates/.github/workflows/doc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// Package workflows keeps github actions in vendor.
package workflows
28 changes: 28 additions & 0 deletions templates/.github/workflows/golangci-lint.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
name: lint
on:
push:
tags:
- v*
branches:
- master
pull_request:
jobs:
golangci:
name: golangci-lint
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: golangci-lint
uses: golangci/[email protected]
with:
# Required: the version of golangci-lint is required and must be specified without patch version: we always use the latest patch version.
version: v1.31

# Optional: golangci-lint command line arguments.
# args: ./the-only-dir-to-analyze/...

# Required: the token is used for fetching a list of releases of golangci-lint.
# The secret `GITHUB_TOKEN` is automatically created by GitHub,
# no need to create it manually.
# https://help.github.com/en/actions/configuring-and-managing-workflows/authenticating-with-the-github_token#about-the-github_token-secret
github-token: ${{ secrets.GITHUB_TOKEN }}
39 changes: 39 additions & 0 deletions templates/.github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
name: test
on:
push:
tags:
- v*
branches:
- master
pull_request:
env:
GO111MODULE: "on"
jobs:
test:
strategy:
matrix:
go-version: [ 1.11.x, 1.12.x, 1.13.x, 1.14.x, 1.15.x ]
runs-on: ubuntu-latest
steps:
- name: Install Go
uses: actions/setup-go@v2
with:
go-version: ${{ matrix.go-version }}
- name: Checkout code
uses: actions/checkout@v2
- uses: actions/cache@v1
with:
path: vendor
key: ${{ runner.os }}-go-${{ hashFiles('**/go.mod') }}
restore-keys: |
${{ runner.os }}-go-${{ hashFiles('**/go.mod') }}
- name: Populate dependencies
run: '(test -d vendor && echo vendor found) || go mod vendor'
- name: Test
run: make test-unit
- name: Upload code coverage
if: matrix.go-version == '1.15.x'
uses: codecov/codecov-action@v1
with:
file: ./unit.coverprofile
flags: unittests

0 comments on commit ca2de71

Please sign in to comment.