Skip to content

Commit

Permalink
chore: use go.makefile
Browse files Browse the repository at this point in the history
  • Loading branch information
elliotxx committed Feb 10, 2022
1 parent 6a889aa commit ad646c5
Show file tree
Hide file tree
Showing 5 changed files with 129 additions and 65 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ jobs:
- name: Send coverage
uses: shogo82148/actions-goveralls@v1
with:
path-to-profile: cover.out
path-to-profile: coverage.out

lint:
name: Lint checks
Expand Down
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
dist/
build/
cover.out
coverage.out
tmp/
78 changes: 17 additions & 61 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,76 +1,32 @@
# Default variable
APP_NAME ?= go-web-prototype
GOFORMATER ?= gofumpt
GOFORMATER_VERSION ?= v0.2.0
GOLINTER ?= golangci-lint
GOLINTER_VERSION ?= v1.41.0
COVER_FILE ?= cover.out
SOURCE_PATHS ?= ./cmd/...
GOSOURCE_PATHS ?= ./cmd/...

.DEFAULT_GOAL := help
include go.makefile

help: ## This help message :)
@grep -E '^[a-zA-Z_-]+:.*?## .*$$' Makefile | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-30s\033[0m %s\n", $$1, $$2}'

# If you encounter an error like "panic: permission denied" on MacOS,
# please visit https://github.com/eisenxp/macos-golink-wrapper to find the solution.
test: ## Run the tests
go test -gcflags=all=-l -timeout=10m `go list $(SOURCE_PATHS)` ${TEST_FLAGS}

cover: ## Generates coverage report
go test -gcflags=all=-l -timeout=10m `go list $(SOURCE_PATHS)` -coverprofile $(COVER_FILE) ${TEST_FLAGS}

cover-html: ## Generates coverage report and displays it in the browser
go tool cover -html=$(COVER_FILE)

format: ## Format source code
@which $(GOFORMATER) > /dev/null || (echo "Installing $(GOFORMATER)@$(GOFORMATER_VERSION) ..."; go install mvdan.cc/gofumpt@$(GOFORMATER_VERSION) && echo -e "Installation complete!\n")
@for path in $(SOURCE_PATHS); do ${GOFORMATER} -l -w -e `echo $${path} | cut -b 3- | rev | cut -b 5- | rev`; done;

lint: ## Lint, will not fix but sets exit code on error
@which $(GOLINTER) > /dev/null || (echo "Installing $(GOLINTER)@$(GOLINTER_VERSION) ..."; go install github.com/golangci/golangci-lint/cmd/golangci-lint@$(GOLINTER_VERSION) && echo -e "Installation complete!\n")
$(GOLINTER) run --deadline=10m $(SOURCE_PATHS)

lint-fix: ## Lint, will try to fix errors and modify code
@which $(GOLINTER) > /dev/null || (echo "Installing $(GOLINTER)@$(GOLINTER_VERSION) ..."; go install github.com/golangci/golangci-lint/cmd/golangci-lint@$(GOLINTER_VERSION) && echo -e "Installation complete!\n")
$(GOLINTER) run --deadline=10m $(SOURCE_PATHS) --fix

doc: ## Start the documentation server with godoc
@which godoc > /dev/null || (echo "Installing godoc@latest ..."; go install golang.org/x/tools/cmd/godoc@latest && echo -e "Installation complete!\n")
godoc -http=:6060

build-changelog: ## Build the changelog with git-chglog
@which git-chglog > /dev/null || (echo "Installing git-chglog@v0.15.1 ..."; go install github.com/git-chglog/git-chglog/cmd/git-chglog@v0.15.1 && echo -e "Installation complete!\n")
mkdir -p ./build
git-chglog -o ./build/CHANGELOG.md

.PHONY: clean
clean: ## Clean build bundles
-rm -rf ./build

gen-version: ## Update version
cd pkg/version/scripts && go run gen.go
.PHONY: build-all
build-all: build-darwin build-linux build-windows ## Build for all platforms

build-all: build-darwin build-linux build-windows ## Build all platforms

build-darwin: gen-version ## Build for MacOS
-rm -rf ./build/darwin/bin
# Build kusion
.PHONY: build-darwin
build-darwin: ## Build for MacOS
-rm -rf ./build/darwin
GOOS=darwin GOARCH=amd64 CGO_ENABLED=0 \
go build -o ./build/darwin/bin/$(APP_NAME) \
go build -o ./build/darwin/$(APPROOT) \
./cmd/main.go

build-linux: gen-version ## Build for Linux
-rm -rf ./build/linux/bin
# Build kusion
.PHONY: build-linux
build-linux: ## Build for Linux
-rm -rf ./build/linux
GOOS=linux GOARCH=amd64 CGO_ENABLED=0 \
go build -o ./build/linux/bin/$(APP_NAME) \
go build -o ./build/linux/$(APPROOT) \
./cmd/main.go

build-windows: gen-version ## Build for Windows
-rm -rf ./build/windows/bin
# Build kusion
.PHONY: build-windows
build-windows: ## Build for Windows
-rm -rf ./build/windows
GOOS=windows GOARCH=amd64 CGO_ENABLED=0 \
go build -o ./build/windows/bin/$(APP_NAME).exe \
go build -o ./build/windows/$(APPROOT).exe \
./cmd/main.go

.PHONY: test cover cover-html format lint lint-fix doc build-changelog clean gen-version build-all build-darwin build-linux build-windows
54 changes: 52 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,60 @@
## Usage
Local startup:
```
make
$ go run cmd/main.go
[GIN-debug] [WARNING] Creating an Engine instance with the Logger and Recovery middleware already attached.
[GIN-debug] [WARNING] Running in "debug" mode. Switch to "release" mode in production.
- using env: export GIN_MODE=release
- using code: gin.SetMode(gin.ReleaseMode)
[GIN-debug] GET /ping --> main.setupRouter.func1 (3 handlers)
[GIN-debug] GET /user/:name --> main.setupRouter.func2 (3 handlers)
[GIN-debug] POST /admin --> main.setupRouter.func3 (4 handlers)
[GIN-debug] Listening and serving HTTP on :8080
$ curl http://127.0.0.1:8080/ping
pong
```

Local build:
```
$ make build-all
$ ./build/darwin/go-web-prototype
[GIN-debug] [WARNING] Creating an Engine instance with the Logger and Recovery middleware already attached.
[GIN-debug] [WARNING] Running in "debug" mode. Switch to "release" mode in production.
- using env: export GIN_MODE=release
- using code: gin.SetMode(gin.ReleaseMode)
[GIN-debug] GET /ping --> main.setupRouter.func1 (3 handlers)
[GIN-debug] GET /user/:name --> main.setupRouter.func2 (3 handlers)
[GIN-debug] POST /admin --> main.setupRouter.func3 (4 handlers)
[GIN-debug] Listening and serving HTTP on :8080
$ curl http://127.0.0.1:8080/ping
pong
```

Local unit test:
Run all unit tests:
```
make test
```

All targets:
```
$ make help
help This help message :)
test Run the tests
cover Generates coverage report
cover-html Generates coverage report and displays it in the browser
format Format source code
lint Lint, will not fix but sets exit code on error
lint-fix Lint, will try to fix errors and modify code
doc Start the documentation server with godoc
clean Clean build bundles
build-all Build for all platforms
build-darwin Build for MacOS
build-linux Build for Linux
build-windows Build for Windows
```
58 changes: 58 additions & 0 deletions go.makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
APPROOT ?= $(shell basename $(PWD))
GOPKG ?= $(shell go list 2>/dev/null)
GOPKGS ?= $(shell go list ./... 2>/dev/null)
GOSOURCES ?= $(shell find . -type f -name '*.go' ! -path '*Godeps/_workspace*')
# You can also customize GOSOURCE_PATHS, e.g. ./pkg/... ./cmd/...
GOSOURCE_PATHS ?= ././...
COVERAGEOUT ?= coverage.out
COVERAGETMP ?= coverage.tmp


# Go tools
GOFORMATER ?= gofumpt
GOFORMATER_VERSION ?= v0.2.0
GOLINTER ?= golangci-lint
GOLINTER_VERSION ?= v1.41.0


# To generate help information
.DEFAULT_GOAL := help
.PHONY: help
help: ## This help message :)
@grep -E '^[a-zA-Z_-]+:.*?## .*$$' go.makefile | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-30s\033[0m %s\n", $$1, $$2}'
@grep -E '^[a-zA-Z_-]+:.*?## .*$$' Makefile | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-30s\033[0m %s\n", $$1, $$2}'


# If you encounter an error like "panic: permission denied" on MacOS,
# please visit https://github.com/eisenxp/macos-golink-wrapper to find the solution.
.PHONY: test
test: ## Run the tests
go test -gcflags=all=-l -timeout=10m `go list $(GOSOURCE_PATHS)` ${TEST_FLAGS}

.PHONY: cover
cover: ## Generates coverage report
go test -gcflags=all=-l -timeout=10m `go list $(GOSOURCE_PATHS)` -coverprofile $(COVERAGEOUT) ${TEST_FLAGS}

.PHONY: cover-html
cover-html: ## Generates coverage report and displays it in the browser
go tool cover -html=$(COVERAGEOUT)

.PHONY: format
format: ## Format source code
@which $(GOFORMATER) > /dev/null || (echo "Installing $(GOFORMATER)@$(GOFORMATER_VERSION) ..."; go install mvdan.cc/gofumpt@$(GOFORMATER_VERSION) && echo -e "Installation complete!\n")
@for path in $(GOSOURCE_PATHS); do ${GOFORMATER} -l -w -e `echo $${path} | cut -b 3- | rev | cut -b 5- | rev`; done;

.PHONY: lint
lint: ## Lint, will not fix but sets exit code on error
@which $(GOLINTER) > /dev/null || (echo "Installing $(GOLINTER)@$(GOLINTER_VERSION) ..."; go install github.com/golangci/golangci-lint/cmd/golangci-lint@$(GOLINTER_VERSION) && echo -e "Installation complete!\n")
$(GOLINTER) run --deadline=10m $(GOSOURCE_PATHS)

.PHONY: lint-fix
lint-fix: ## Lint, will try to fix errors and modify code
@which $(GOLINTER) > /dev/null || (echo "Installing $(GOLINTER)@$(GOLINTER_VERSION) ..."; go install github.com/golangci/golangci-lint/cmd/golangci-lint@$(GOLINTER_VERSION) && echo -e "Installation complete!\n")
$(GOLINTER) run --deadline=10m $(GOSOURCE_PATHS) --fix

.PHONY: doc
doc: ## Start the documentation server with godoc
@which godoc > /dev/null || (echo "Installing godoc@latest ..."; go install golang.org/x/tools/cmd/godoc@latest && echo -e "Installation complete!\n")
godoc -http=:6060

0 comments on commit ad646c5

Please sign in to comment.