Skip to content

Commit

Permalink
initial files
Browse files Browse the repository at this point in the history
  • Loading branch information
aldas committed Mar 5, 2021
1 parent 6c72c69 commit 9a9cde6
Show file tree
Hide file tree
Showing 6 changed files with 130 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Text files have auto line endings
* text=auto

# Go source files always have LF line endings
*.go text eol=lf
8 changes: 8 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
.idea

# Binaries for programs and plugins
*.exe
*.exe~
Expand All @@ -13,3 +15,9 @@

# Dependency directories (remove the comment below to include it)
# vendor/

### macOS ###
*.DS_Store
.AppleDouble
.LSOverride
.directory
42 changes: 42 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
PROJECT_NAME := go-modbus-client
PKG := "github.com/aldas/$(PROJECT_NAME)"
PKG_LIST := $(shell go list ${PKG}/...)

.PHONY: init lint test coverage coverhtml

.DEFAULT_GOAL := check

check: lint vet race ## check project

init:
git config core.hooksPath ./scripts/.githooks
@go get -u golang.org/x/lint/golint

lint: ## Lint the files
@golint -set_exit_status ${PKG_LIST}

vet: ## Vet the files
@go vet ${PKG_LIST}

test: ## Run unittests
@go test -short ${PKG_LIST}

goversion ?= "1.16"
test_version: ## Run tests inside Docker with given version (defaults to 1.16 oldest supported). Example: make test_version goversion=1.16
@docker run --rm -it -v $(shell pwd):/project golang:$(goversion) /bin/sh -c "cd /project && make init check"

race: ## Run data race detector
@go test -race -short ${PKG_LIST}

benchmark: ## Run benchmarks
@go test -run="-" -bench=".*" ${PKG_LIST}

coverage: ## Generate global code coverage report
./scripts/coverage.sh;

coverhtml: ## Generate global code coverage report in HTML
./scripts/coverage.sh html;


help: ## Display this help screen
@grep -h -E '^[a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-30s\033[0m %s\n", $$1, $$2}'
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/aldas/go-modbus-client

go 1.16
43 changes: 43 additions & 0 deletions scripts/.githooks/pre-commit
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#!/usr/bin/env bash

if [[ -z "$(which golint)" ]]; then
echo "golint not found, executing: go get github.com/golang/lint/golint"
go get github.com/golang/lint/golint
fi

STAGED_GO_FILES=$(git diff --cached --name-only | grep ".go$")

if [[ "$STAGED_GO_FILES" = "" ]]; then
exit 0
fi

# can not check only changed files as if it references structs etc in same package
# but in different file it reports that as 'route.go:18:11: undefined: Service'
go vet ./...
if [[ $? != 0 ]]; then
printf "go vet FAILED\n"
exit 1
fi

PASS=true

for FILE in ${STAGED_GO_FILES}
do
if [[ ! -f ${FILE} ]]; then
continue
fi

golint -set_exit_status ${FILE}
if [[ $? != 0 ]]; then
PASS=false
fi
done

if ! ${PASS}; then
printf "COMMIT FAILED\n"
exit 1
else
printf "COMMIT SUCCEEDED\n"
fi

exit 0
29 changes: 29 additions & 0 deletions scripts/coverage.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#!/usr/bin/env bash
#
# Code coverage generation

COVERAGE_DIR="${COVERAGE_DIR:-coverage}"
PKG_LIST=$(go list ./...)

# Create the coverage files directory
mkdir -p "$COVERAGE_DIR";

# Create a coverage file for each package
for package in ${PKG_LIST}; do
go test -covermode=count -coverprofile "${COVERAGE_DIR}/${package##*/}.cov" "$package" ;
done ;

# Merge the coverage profile files
echo 'mode: count' > "${COVERAGE_DIR}"/coverage.xcov ;
tail -q -n +2 "${COVERAGE_DIR}"/*.cov >> "${COVERAGE_DIR}"/coverage.xcov ;

# Display the global code coverage
go tool cover -func="${COVERAGE_DIR}"/coverage.xcov ;

# If needed, generate HTML report
if [[ "$1" == "html" ]]; then
go tool cover -html="${COVERAGE_DIR}"/coverage.xcov -o coverage.html ;
fi

# Remove the coverage files directory
rm -rf "$COVERAGE_DIR";

0 comments on commit 9a9cde6

Please sign in to comment.