Skip to content

Commit

Permalink
add pre-commit
Browse files Browse the repository at this point in the history
  • Loading branch information
micpst committed Mar 30, 2024
1 parent 1216dbe commit 62fd3d5
Show file tree
Hide file tree
Showing 4 changed files with 119 additions and 94 deletions.
4 changes: 0 additions & 4 deletions .husky/pre-commit

This file was deleted.

26 changes: 26 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -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
181 changes: 92 additions & 89 deletions Makefile
Original file line number Diff line number Diff line change
@@ -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)
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand All @@ -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
)

0 comments on commit 62fd3d5

Please sign in to comment.