mirror of
https://github.com/d0zingcat/gocryptotrader.git
synced 2026-05-13 15:09:42 +00:00
Makefile: Improve existing and add new targets (#1937)
* I've made a comprehensive overhaul of the Makefile for your Go development workflow.
This update brings a significant series of enhancements to the project's Makefile, aimed at modernizing and improving your Go development experience. This includes improvements to linting, formatting, dependency management, Docker integration, and the overall Makefile structure, style, and usability.
Here are the key improvements:
1. **Linting (`golangci-lint`):**
* **Version & Path:** I've upgraded `golangci-lint` to use the v2 path (`github.com/golangci/golangci-lint/cmd/golangci-lint/v2`) and pinned it to a specific version (`v2.1.6`) for consistency.
* **Target Renaming:** The primary linting target was renamed from `linter` to `lint` to align with common conventions.
* **Command Simplification:** The `lint` target's command was streamlined to `$(LINTBIN) run --verbose`, relying directly on `golangci-lint`'s exit code for success/failure reporting.
* **Redundancy Removal:** A superfluous `go install $(GCTPKG)` command was removed from the `lint` target.
2. **Dockerized Linting (`lint_docker`):**
* A new `lint_docker` target (renamed from `lint-docker` for consistent naming) was introduced to execute `golangci-lint` within a Docker container (`golangci/golangci-lint:v2.1.6`).
* This ensures a consistent linting environment across different developer setups and CI.
* The target uses `$(CURDIR)` for reliable Docker volume mounting.
* It includes the `--verbose` flag for detailed linter output and the `-t` flag to allocate a pseudo-TTY, enabling colored output from the linter.
* It incorporates a check for Docker's availability before attempting to run.
* Whitespace in the Docker check command was normalized for readability (single space after `||`).
3. **Code Formatting (`gofmt` & `gofumpt`):**
* **Default Formatter:** The main `fmt` target now consistently uses `gofmt` for standard Go formatting.
* **Stricter Formatting Option:** A new `gofumpt` target has been added, allowing you to use the stricter `gofumpt` formatter. This target includes a command to automatically install `gofumpt` if it's not already present.
* **File Exclusion Control (`GO_FILES_TO_FORMAT`):**
- A Make variable `GO_FILES_TO_FORMAT` was introduced to precisely define the set of Go files to be processed by the formatters.
- This variable is configured to exclude:
- Generated ORM code (e.g., `./database/models/*`).
- Vendored dependencies (`./vendor/*`).
- Protobuf generated files (`*.pb.go`).
- gRPC gateway generated files (`*.pb.gw.go`).
- Both the `fmt` and `gofumpt` targets now utilize this variable, preventing accidental reformatting of generated or third-party code.
4. **Makefile Structure & General Enhancements:**
* **Redundant Target Removal:** The `get` target was removed, as its functionality was fully covered by the more optimized `install` target.
* **`.PHONY` Declarations:**
- The default `all` target was explicitly added to the `.PHONY` list for improved clarity and robustness.
- All relevant Makefile targets, including newly added and renamed ones (`lint`, `gofumpt`, `lint_docker`, `fmt`), are explicitly declared in the `.PHONY` list, adhering to best practices.
- The order of targets in the main `.PHONY` declaration was adjusted to match the sequence of their definitions in the Makefile, enhancing readability.
These comprehensive changes aim to provide you with a more robust, flexible, consistent, and maintainable build and development environment.
* Fix: Ensure GOPATH is defined in Makefile
This commit enhances the Makefile by ensuring that the GOPATH
variable is defined before being used by LINTBIN and GOFUMPTBIN.
The line `GOPATH ?= $(shell go env GOPATH)` has been added.
This means:
- If GOPATH is set in the environment, that value will be used.
- Otherwise, GOPATH will be set to the output of `go env GOPATH`,
which is the canonical way to determine the Go path.
This change makes the Makefile more robust and less dependent on
your shell environment having GOPATH explicitly set.
---------
Co-authored-by: google-labs-jules[bot] <161369871+google-labs-jules[bot]@users.noreply.github.com>
This commit is contained in:
27
Makefile
27
Makefile
@@ -1,26 +1,29 @@
|
|||||||
LDFLAGS = -ldflags "-w -s"
|
LDFLAGS = -ldflags "-w -s"
|
||||||
GCTPKG = github.com/thrasher-corp/gocryptotrader
|
GCTPKG = github.com/thrasher-corp/gocryptotrader
|
||||||
LINTPKG = github.com/golangci/golangci-lint/cmd/golangci-lint@v2.1.6
|
LINTPKG = github.com/golangci/golangci-lint/v2/cmd/golangci-lint@v2.1.6
|
||||||
|
GOPATH ?= $(shell go env GOPATH)
|
||||||
LINTBIN = $(GOPATH)/bin/golangci-lint
|
LINTBIN = $(GOPATH)/bin/golangci-lint
|
||||||
|
GOFUMPTBIN = $(GOPATH)/bin/gofumpt
|
||||||
GCTLISTENPORT=9050
|
GCTLISTENPORT=9050
|
||||||
GCTPROFILERLISTENPORT=8085
|
GCTPROFILERLISTENPORT=8085
|
||||||
|
GO_FILES_TO_FORMAT := $(shell find . -type f -name '*.go' -not -path "./database/models/*" -not -path "./vendor/*" -not -name "*.pb.go" -not -name "*.pb.gw.go")
|
||||||
DRIVER ?= psql
|
DRIVER ?= psql
|
||||||
RACE_FLAG := $(if $(NO_RACE_TEST),,-race)
|
RACE_FLAG := $(if $(NO_RACE_TEST),,-race)
|
||||||
CONFIG_FLAG = $(if $(CONFIG),-config $(CONFIG),)
|
CONFIG_FLAG = $(if $(CONFIG),-config $(CONFIG),)
|
||||||
|
|
||||||
.PHONY: get linter check test build install update_deps
|
.PHONY: all lint lint_docker check test build install fmt gofumpt update_deps
|
||||||
|
|
||||||
all: check build
|
all: check build
|
||||||
|
|
||||||
get:
|
lint:
|
||||||
go install $(GCTPKG)
|
|
||||||
|
|
||||||
linter:
|
|
||||||
go install $(GCTPKG)
|
|
||||||
go install $(LINTPKG)
|
go install $(LINTPKG)
|
||||||
test -z "$$($(LINTBIN) run --verbose | tee /dev/stderr)"
|
$(LINTBIN) run --verbose
|
||||||
|
|
||||||
check: linter test
|
lint_docker:
|
||||||
|
@command -v docker >/dev/null 2>&1 || (echo "Docker not found. Please install Docker to run this target." && exit 1)
|
||||||
|
docker run --rm -t -v $(CURDIR):/app -w /app golangci/golangci-lint:v2.1.6 golangci-lint run --verbose
|
||||||
|
|
||||||
|
check: lint test
|
||||||
|
|
||||||
test:
|
test:
|
||||||
go test $(RACE_FLAG) -coverprofile=coverage.txt -covermode=atomic ./...
|
go test $(RACE_FLAG) -coverprofile=coverage.txt -covermode=atomic ./...
|
||||||
@@ -32,7 +35,11 @@ install:
|
|||||||
go install $(LDFLAGS)
|
go install $(LDFLAGS)
|
||||||
|
|
||||||
fmt:
|
fmt:
|
||||||
gofmt -l -w -s $(shell find . -type f -name '*.go')
|
gofmt -l -w -s $(GO_FILES_TO_FORMAT)
|
||||||
|
|
||||||
|
gofumpt:
|
||||||
|
@command -v gofumpt >/dev/null 2>&1 || go install mvdan.cc/gofumpt@latest
|
||||||
|
$(GOFUMPTBIN) -l -w $(GO_FILES_TO_FORMAT)
|
||||||
|
|
||||||
update_deps:
|
update_deps:
|
||||||
go mod verify
|
go mod verify
|
||||||
|
|||||||
Reference in New Issue
Block a user