diff --git a/.husky/pre-commit b/.husky/pre-commit deleted file mode 100755 index a66f10c..0000000 --- a/.husky/pre-commit +++ /dev/null @@ -1,4 +0,0 @@ -#!/usr/bin/env sh -. "$(dirname -- "$0")/_/husky.sh" - -make docker-check diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml new file mode 100644 index 0000000..e2c1a01 --- /dev/null +++ b/.pre-commit-config.yaml @@ -0,0 +1,26 @@ +repos: + - repo: local + hooks: + - id: format + name: format code + entry: make format + language: system + always_run: true + + - id: lint + name: check code style + entry: make lint + language: system + always_run: true + + - id: test + name: run tests + entry: make test + language: system + always_run: true + + - id: build + name: build the project + entry: make build + language: system + always_run: true diff --git a/Makefile b/Makefile index ceffcab..9e8f439 100644 --- a/Makefile +++ b/Makefile @@ -1,89 +1,92 @@ -BINARY_DIR=bin -BINARY_NAME=server -BINARY_PATH=$(BINARY_DIR)/$(BINARY_NAME) -RELEASE_NAME=minisearch -COVERAGE_PROFILE=cover.out -DOCKER_IMAGE=minisearch -DOCKER_IMAGE_DEV=$(DOCKER_IMAGE)-dev -DOCKER_IMAGE_WATCH=$(DOCKER_IMAGE)-watch -DOCKER_PORT=3000 -WATCH_PORT=3001 - -.PHONY: vendor - -all: lint coverage build - -# Build: -setup: - @go mod download - -build: - @go build -buildvcs=false -o $(BINARY_PATH) ./cmd/server - -clean: - @go clean --cache - @rm -rf $(BINARY_DIR) - @rm $(COVERAGE_PROFILE) - -vendor: - @GO111MODULE=on go mod vendor - -watch: - $(eval PACKAGE_NAME=$(shell head -n 1 go.mod | cut -d ' ' -f2)) - @docker run -it --rm \ - -w /go/src/$(PACKAGE_NAME) \ - -v $(shell pwd):/go/src/$(PACKAGE_NAME) \ - -p $(WATCH_PORT):$(WATCH_PORT) \ - --name $(DOCKER_IMAGE_WATCH) \ - cosmtrek/air \ - --build.cmd "go build -buildvcs=false -race -o $(BINARY_PATH) ./cmd/server" \ - --build.bin "./$(BINARY_PATH) -p $(WATCH_PORT)" - -# Test: -test: - @go test -v -race ./... - -coverage: - @go test -v -race -cover -covermode=atomic -coverprofile=$(COVERAGE_PROFILE) ./... - @go tool cover -func $(COVERAGE_PROFILE) - -# Benchmark: -bench: - @go test -bench=. -run=^a -benchtime=5x ./... - -# Lint -lint: - @golangci-lint run --timeout=3m - -# Release: -release: - @go build -buildvcs=false -o $(BINARY_NAME) ./cmd/server - @tar -czvf $(RELEASE_NAME).tar.gz $(BINARY_NAME) README.md LICENSE.md - @rm $(BINARY_NAME) - -# Docker dev: -docker-dev-setup: - @docker build -t $(DOCKER_IMAGE_DEV) --target dev . - @docker run --rm --name $(DOCKER_IMAGE_DEV) -d -v $(shell pwd):/app $(DOCKER_IMAGE_DEV) - -docker-dev-stop: - @docker stop $(DOCKER_IMAGE_DEV) - -docker-check: - @docker exec $(DOCKER_IMAGE_DEV) make - -docker-test: - @docker exec $(DOCKER_IMAGE_DEV) make test - -docker-bench: - @docker exec $(DOCKER_IMAGE_DEV) make bench - -# Docker prod: -docker-build: - @docker build -t $(DOCKER_IMAGE) --target release . - -docker-run: - @docker run --rm --name $(DOCKER_IMAGE) -d -p $(DOCKER_PORT):$(DOCKER_PORT) $(DOCKER_IMAGE) -p $(DOCKER_PORT) - -docker-stop: - @docker stop $(DOCKER_IMAGE) +BINARY_DIR=bin +BINARY_NAME=server +BINARY_PATH=$(BINARY_DIR)/$(BINARY_NAME) +RELEASE_NAME=minisearch +COVERAGE_PROFILE=cover.out +DOCKER_IMAGE=minisearch +DOCKER_IMAGE_DEV=$(DOCKER_IMAGE)-dev +DOCKER_IMAGE_WATCH=$(DOCKER_IMAGE)-watch +DOCKER_PORT=3000 +WATCH_PORT=3001 + +.PHONY: vendor + +all: lint coverage build + +# Build: +setup: + @go mod download + +build: + @go build -buildvcs=false -o $(BINARY_PATH) ./cmd/server + +clean: + @go clean --cache + @rm -rf $(BINARY_DIR) + @rm $(COVERAGE_PROFILE) + +vendor: + @GO111MODULE=on go mod vendor + +watch: + $(eval PACKAGE_NAME=$(shell head -n 1 go.mod | cut -d ' ' -f2)) + @docker run -it --rm \ + -w /go/src/$(PACKAGE_NAME) \ + -v $(shell pwd):/go/src/$(PACKAGE_NAME) \ + -p $(WATCH_PORT):$(WATCH_PORT) \ + --name $(DOCKER_IMAGE_WATCH) \ + cosmtrek/air \ + --build.cmd "go build -buildvcs=false -race -o $(BINARY_PATH) ./cmd/server" \ + --build.bin "./$(BINARY_PATH) -p $(WATCH_PORT)" + +# Test: +test: + @go test -v -race ./... + +coverage: + @go test -v -race -cover -covermode=atomic -coverprofile=$(COVERAGE_PROFILE) ./... + @go tool cover -func $(COVERAGE_PROFILE) + +# Benchmark: +bench: + @go test -bench=. -run=^a -benchtime=5x ./... + +# Lint +lint: + @golangci-lint run --timeout=3m + +format: + @go fmt ./... + +# Release: +release: + @go build -buildvcs=false -o $(BINARY_NAME) ./cmd/server + @tar -czvf $(RELEASE_NAME).tar.gz $(BINARY_NAME) README.md LICENSE.md + @rm $(BINARY_NAME) + +# Docker dev: +docker-dev-setup: + @docker build -t $(DOCKER_IMAGE_DEV) --target dev . + @docker run --rm --name $(DOCKER_IMAGE_DEV) -d -v $(shell pwd):/app $(DOCKER_IMAGE_DEV) + +docker-dev-stop: + @docker stop $(DOCKER_IMAGE_DEV) + +docker-check: + @docker exec $(DOCKER_IMAGE_DEV) make + +docker-test: + @docker exec $(DOCKER_IMAGE_DEV) make test + +docker-bench: + @docker exec $(DOCKER_IMAGE_DEV) make bench + +# Docker prod: +docker-build: + @docker build -t $(DOCKER_IMAGE) --target release . + +docker-run: + @docker run --rm --name $(DOCKER_IMAGE) -d -p $(DOCKER_PORT):$(DOCKER_PORT) $(DOCKER_IMAGE) -p $(DOCKER_PORT) + +docker-stop: + @docker stop $(DOCKER_IMAGE) diff --git a/go.mod b/go.mod index b1067e1..d18d406 100644 --- a/go.mod +++ b/go.mod @@ -7,6 +7,7 @@ require ( github.com/google/uuid v1.3.0 github.com/kljensen/snowball v0.8.0 github.com/stretchr/testify v1.8.1 + golang.org/x/text v0.7.0 ) require ( @@ -33,7 +34,6 @@ require ( golang.org/x/crypto v0.6.0 // indirect golang.org/x/net v0.7.0 // indirect golang.org/x/sys v0.5.0 // indirect - golang.org/x/text v0.7.0 // indirect google.golang.org/protobuf v1.28.1 // indirect gopkg.in/yaml.v3 v3.0.1 // indirect )