Skip to content

Commit

Permalink
Merge pull request #90 from bilalcaliskan/devel #major
Browse files Browse the repository at this point in the history
refactor: refactor repository with latest approaches
  • Loading branch information
bilalcaliskan authored Jan 7, 2024
2 parents df03ad7 + 4974397 commit 2848924
Show file tree
Hide file tree
Showing 9 changed files with 77 additions and 143 deletions.
19 changes: 2 additions & 17 deletions .github/workflows/pr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,21 +23,6 @@ jobs:
- name: Clean downloaded binaries
run: make -s clean

fmt:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Install Go
uses: actions/setup-go@v5
with:
go-version-file: "go.mod"
cache: true
- name: Run fmt
run: make -s fmt
- name: Clean downloaded binaries
run: make -s clean

test:
strategy:
matrix:
Expand Down Expand Up @@ -92,8 +77,8 @@ jobs:
# with:
# go-version-file: "go.mod"
# cache: true
# - name: Coverage Test
# run: make -s test-with-coverage
# - name: Test
# run: make -s test
# - name: SonarCloud Scan
# uses: SonarSource/sonarcloud-github-action@master
# env:
Expand Down
20 changes: 2 additions & 18 deletions .github/workflows/push.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,21 +20,6 @@ jobs:
- name: Clean downloaded binaries
run: make -s clean

fmt:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Install Go
uses: actions/setup-go@v5
with:
go-version-file: "go.mod"
cache: true
- name: Run fmt
run: make -s fmt
- name: Clean downloaded binaries
run: make -s clean

test:
strategy:
matrix:
Expand Down Expand Up @@ -89,8 +74,8 @@ jobs:
# with:
# go-version-file: "go.mod"
# cache: true
# - name: Coverage Test
# run: make -s test-with-coverage
# - name: Test
# run: make -s test
# - name: SonarCloud Scan
# uses: SonarSource/sonarcloud-github-action@master
# env:
Expand Down Expand Up @@ -125,7 +110,6 @@ jobs:
runs-on: ubuntu-latest
needs:
- lint
- fmt
- test
- codeql
- staticcheck
Expand Down
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
.idea/
bin/
vendor/
coverage.txt
**coverage.txt
cover.html

# Binaries for programs and plugins
*.exe
Expand Down
10 changes: 10 additions & 0 deletions .mockery.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
with-expecter: true
dir: "{{.InterfaceDir}}"
filename: "mock_{{.InterfaceName | lower}}.go"
inpackage: true
#packages:
# github.com/bilalcaliskan/rss-feed-filterer/internal/feed:
# config:
# interfaces:
# Parser:
# config:
128 changes: 51 additions & 77 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
GOLANGCI_LINT_VERSION = latest
REVIVE_VERSION = latest
GOIMPORTS_VERSION = latest
INEFFASSIGN_VERSION = latest
LOCAL_BIN := $(shell dirname $(realpath $(lastword $(MAKEFILE_LIST))))/.bin

DEFAULT_GO_TEST_CMD ?= go test ./... -race -p 1 -covermode=atomic
DEFAULT_GO_RUN_ARGS ?= ""

GOLANGCI_LINT_VERSION := latest
REVIVE_VERSION := v1.3.4
MOCKERY_VERSION := v2.39.1

LOCAL_BIN := $(shell dirname $(realpath $(lastword $(MAKEFILE_LIST))))/.bin

.PHONY: all
all: clean tools lint fmt test build
all: clean tools lint test build

.PHONY: clean
clean:
Expand All @@ -21,8 +23,11 @@ pre-commit-setup:
pre-commit install -c build/ci/.pre-commit-config.yaml

.PHONY: tools
tools: golangci-lint-install revive-install go-imports-install ineffassign-install
go mod tidy
tools: mockery-install golangci-lint-install revive-install vendor

.PHONY: mockery-install
mockery-install:
GOBIN=$(LOCAL_BIN) go install github.com/vektra/mockery/v2@$(MOCKERY_VERSION)

.PHONY: golangci-lint-install
golangci-lint-install:
Expand All @@ -32,10 +37,6 @@ golangci-lint-install:
revive-install:
GOBIN=$(LOCAL_BIN) go install github.com/mgechev/revive@$(REVIVE_VERSION)

.PHONY: ineffassign-install
ineffassign-install:
GOBIN=$(LOCAL_BIN) go install github.com/gordonklaus/ineffassign@$(INEFFASSIGN_VERSION)

.PHONY: lint
lint: tools run-lint

Expand All @@ -44,19 +45,16 @@ run-lint: lint-golangci-lint lint-revive

.PHONY: lint-golangci-lint
lint-golangci-lint:
#$(info running golangci-lint...)
echo "running golangci-lint..."
$(info running golangci-lint...)
$(LOCAL_BIN)/golangci-lint -v run ./... || (echo golangci-lint returned an error, exiting!; sh -c 'exit 1';)
echo "golangci-lint exited successfully!"

.PHONY: lint-revive
lint-revive:
echo "running revive..."
$(info running revive...)
$(LOCAL_BIN)/revive -formatter=stylish -config=build/ci/.revive.toml -exclude ./vendor/... ./... || (echo revive returned an error, exiting!; sh -c 'exit 1';)
echo "revive exited successfully!"

.PHONY: upgrade-direct-deps
upgrade-direct-deps: tidy
.PHONY: upgrade-deps
upgrade-deps: vendor
for item in `grep -v 'indirect' go.mod | grep '/' | cut -d ' ' -f 1`; do \
echo "trying to upgrade direct dependency $$item" ; \
go get -u $$item ; \
Expand All @@ -68,71 +66,43 @@ upgrade-direct-deps: tidy
tidy:
go mod tidy

.PHONY: run-goimports
run-goimports: go-imports-install
for item in `find . -type f -name '*.go' -not -path './vendor/*'`; do \
$(LOCAL_BIN)/goimports -l -w $$item ; \
done

.PHONY: go-imports-install
go-imports-install:
GOBIN=$(LOCAL_BIN) go install golang.org/x/tools/cmd/goimports@$(GOIMPORTS_VERSION)

.PHONY: fmt
fmt: tools run-fmt run-ineffassign run-vet

.PHONY: run-fmt
run-fmt:
echo "running fmt..."
go fmt ./... || (echo fmt returned an error, exiting!; sh -c 'exit 1';)
echo "fmt exited successfully!"

.PHONY: run-ineffassign
run-ineffassign:
echo "running ineffassign..."
$(LOCAL_BIN)/ineffassign ./... || (echo ineffassign returned an error, exiting!; sh -c 'exit 1';)
echo "ineffassign exited successfully!"

.PHONY: run-vet
run-vet:
echo "running vet..."
go vet ./... || (echo vet returned an error, exiting!; sh -c 'exit 1';)
echo "vet exited successfully!"
.PHONY: vendor
vendor: tidy
go mod vendor

.PHONY: test
test: tidy
echo "starting the test for whole module..."
go test -failfast -vet=off -race ./... || (echo an error while testing, exiting!; sh -c 'exit 1';)

.PHONY: test-with-coverage
test-with-coverage: tidy
go test ./... -race -coverprofile=coverage.txt -covermode=atomic

.PHONY: update
update: tidy
go get -u ./...
test: vendor
$(info starting the test for whole module...)
$(DEFAULT_GO_TEST_CMD) -coverprofile=coverage.txt || (echo an error while testing, exiting!; sh -c 'exit 1';)

#.PHONY: test-unit
#test-unit: vendor
# $(info starting the unit test for whole module...)
# $(DEFAULT_GO_TEST_CMD) -tags "unit" -coverprofile=unit_coverage.txt || (echo an error while testing, exiting!; sh -c 'exit 1';)
#
#.PHONY: test-e2e
#test-e2e: vendor
# $(info starting the e2e test for whole module...)
# $(DEFAULT_GO_TEST_CMD) -tags "e2e" -coverprofile=e2e_coverage.txt || (echo an error while testing, exiting!; sh -c 'exit 1';)
#
#.PHONY: test-integration
#test-integration: vendor
# $(info starting the integration test for whole module...)
# $(DEFAULT_GO_TEST_CMD) -tags "integration" -coverprofile=integration_coverage.txt || (echo an error while testing, exiting!; sh -c 'exit 1';)

.PHONY: test-coverage
test-coverage: test
go tool cover -html=coverage.txt -o cover.html
open cover.html

.PHONY: build
build: tidy
echo "building binary..."
build: vendor
$(info building binary...)
go build -o bin/main main.go || (echo an error while building binary, exiting!; sh -c 'exit 1';)
echo "binary built successfully!"

.PHONY: run
run: tidy
go run main.go

.PHONY: cross-compile
cross-compile:
GOOS=freebsd GOARCH=386 go build -o bin/main-freebsd-386 main.go
GOOS=darwin GOARCH=386 go build -o bin/main-darwin-386 main.go
GOOS=linux GOARCH=386 go build -o bin/main-linux-386 main.go
GOOS=windows GOARCH=386 go build -o bin/main-windows-386 main.go
GOOS=freebsd GOARCH=amd64 go build -o bin/main-freebsd-amd64 main.go
GOOS=darwin GOARCH=amd64 go build -o bin/main-darwin-amd64 main.go
GOOS=linux GOARCH=amd64 go build -o bin/main-linux-amd64 main.go
GOOS=windows GOARCH=amd64 go build -o bin/main-windows-amd64 main.go

run: vendor
go run main.go $(DEFAULT_GO_RUN_ARGS)

.PHONY: prepare-initial-project
GITHUB_USERNAME ?= $(shell read -p "Your Github username(ex: bilalcaliskan): " github_username; echo $$github_username)
Expand All @@ -142,3 +112,7 @@ prepare-initial-project:
grep -rl golang-cli-template . --exclude-dir=.git --exclude-dir=.idea | xargs sed -i 's/golang-cli-template/$(PROJECT_NAME)/g'
echo "Please refer to *Additional nice-to-have steps* in README.md for additional features"
echo "Cheers!"

.PHONY: generate-mocks
generate-mocks: mockery-install tidy vendor
$(LOCAL_BIN)/mockery || (echo mockery returned an error, exiting!; sh -c 'exit 1';)
15 changes: 9 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
[![Coverage](https://sonarcloud.io/api/project_badges/measure?project=bilalcaliskan_golang-cli-template&metric=coverage)](https://sonarcloud.io/summary/new_code?id=bilalcaliskan_golang-cli-template)
[![Release](https://img.shields.io/github/release/bilalcaliskan/golang-cli-template.svg)](https://github.com/bilalcaliskan/golang-cli-template/releases/latest)
[![Go version](https://img.shields.io/github/go-mod/go-version/bilalcaliskan/golang-cli-template)](https://github.com/bilalcaliskan/golang-cli-template)
[![pre-commit](https://img.shields.io/badge/pre--commit-enabled-brightgreen?logo=pre-commit)](https://github.com/pre-commit/pre-commit)
[![License](https://img.shields.io/badge/License-Apache%202.0-blue.svg)](https://opensource.org/licenses/Apache-2.0)

## Required Steps
Expand All @@ -21,32 +22,34 @@
- If you want to build and publish Docker image:
- Ensure `DOCKER_USERNAME` has been added as **repository secret on GitHub**
- Ensure `DOCKER_PASSWORD` has been added as **repository secret on GitHub**
- Uncomment **line 161** to **line 168** in [.github/workflows/push.yml](.github/workflows/push.yml)
- Uncomment **line 145** to **line 152** in [.github/workflows/push.yml](.github/workflows/push.yml)
- Uncomment **line 32** to **line 50** in [build/package/.goreleaser.yaml](build/package/.goreleaser.yaml)
- If you want to enable https://sonarcloud.io/ integration:
- Ensure your created repository from that template has been added to https://sonarcloud.io/
- Ensure `SONAR_TOKEN` has been added as **repository secret** on GitHub
- Ensure `SONAR_TOKEN` has been added as **dependabot secret** on GitHub
- Uncomment **line 84** to **line 109** in [.github/workflows/pr.yml](.github/workflows/pr.yml)
- Uncomment **line 132** in [.github/workflows/push.yml](.github/workflows/push.yml)
- Uncomment **line 81** to **line 106** in [.github/workflows/push.yml](.github/workflows/push.yml)
- Uncomment **line 69** to **line 94** in [.github/workflows/pr.yml](.github/workflows/pr.yml)
- Uncomment **line 116** in [.github/workflows/push.yml](.github/workflows/push.yml)
- Uncomment **line 66** to **line 91** in [.github/workflows/push.yml](.github/workflows/push.yml)
- If you want to create banner:
- Generate a banner from [here](https://devops.datenkollektiv.de/banner.txt/index.html) and place it inside of [build/ci](build/ci) directory into a file **banner.txt**
- Uncomment **line 18** and **line 35** to **line 38** in [cmd/root.go](cmd/root.go)
- Run `go get -u github.com/dimiro1/banner`
- If you want to release as Homebrew Formula:
- At first, you must have a **formula repository** like https://github.com/bilalcaliskan/homebrew-tap
- Create an access token on account that has **formula repository** mentioned above item and ensure that token is added as`TAP_GITHUB_TOKEN` **repository secret** on GitHub
- Uncomment **line 181** in [.github/workflows/push.yml](.github/workflows/push.yml)
- Uncomment **line 165** in [.github/workflows/push.yml](.github/workflows/push.yml)
- Uncomment **line 70** to **line 80** in [build/package/.goreleaser.yaml](build/package/.goreleaser.yaml)
- If you want to mock your interfaces with [mockery](https://github.com/vektra/mockery):
- Add `generate-mocks` target as a prerequisite to all uncommented targets starting with `test` in [Makefile](Makefile)

## Used Libraries
- [spf13/cobra](https://github.com/spf13/cobra)
- [rs/zerolog](https://github.com/rs/zerolog)

## Development
This project requires below tools while developing:
- [Golang 1.20](https://golang.org/doc/go1.20)
- [Golang 1.21](https://golang.org/doc/go1.21)
- [pre-commit](https://pre-commit.com/)
- [golangci-lint](https://golangci-lint.run/usage/install/) - required by [pre-commit](https://pre-commit.com/)
- [gocyclo](https://github.com/fzipp/gocyclo) - required by [pre-commit](https://pre-commit.com/)
Expand Down
18 changes: 0 additions & 18 deletions build/package/Dockerfile

This file was deleted.

2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/bilalcaliskan/golang-cli-template

go 1.20
go 1.21

require (
github.com/rs/zerolog v1.31.0
Expand Down
5 changes: 0 additions & 5 deletions hack/coverage.sh

This file was deleted.

0 comments on commit 2848924

Please sign in to comment.