Skip to content

Commit 799351f

Browse files
committed
chore: update makefile and workflow for accounts-v3
Signed-off-by: krishna2803 <kpandey81930@gmail.com>
1 parent 11be074 commit 799351f

File tree

2 files changed

+84
-63
lines changed

2 files changed

+84
-63
lines changed

.github/workflows/verify_format.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ jobs:
88
runs-on: ubuntu-latest
99
steps:
1010

11-
- uses: actions/checkout@v3
12-
- uses: actions/setup-go@v3
11+
- uses: actions/checkout@v4
12+
- uses: actions/setup-go@v4
1313
with:
1414
go-version-file: './go.mod'
1515

Makefile

Lines changed: 82 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -1,96 +1,117 @@
1+
PROJECT_NAME := nymeria
2+
PACKAGE_BASE := github.com/sdslabs/nymeria
3+
BINARY_NAME := $(PROJECT_NAME)
4+
CMD_DIR := ./cmd/$(PROJECT_NAME)
5+
16
GO := go
27
GOPATH := $(shell go env GOPATH)
38
GOPATH_BIN := $(GOPATH)/bin
9+
10+
BUILD_DIR := build
11+
VERSION := $(shell git describe --tags --always --dirty)
12+
COMMIT := $(shell git rev-parse --short HEAD)
13+
BUILD_TIME := $(shell date -u '+%Y-%m-%d_%H:%M:%S')
14+
LDFLAGS := -ldflags "-X main.version=$(VERSION) -X main.commit=$(COMMIT) -X main.buildTime=$(BUILD_TIME) -s -w"
15+
416
GOLANGCI_LINT := $(GOPATH_BIN)/golangci-lint
5-
SRC = $(shell find . -type f -name '*.go' -not -path "./vendor/*")
617
GOIMPORTS := $(GOPATH_BIN)/goimports
7-
GO_PACKAGES = $(shell go list ./... | grep -v vendor)
8-
PACKAGE_BASE := github.com/sdslabs/nymeria
18+
AIR := $(GOPATH_BIN)/air
919

10-
DB_HOST = $(shell grep -A6 "^db:" config.yaml | grep "host:" | head -1 | cut -d'"' -f2)
11-
DB_PORT = $(shell grep -A6 "^db:" config.yaml | grep "port:" | head -1 | awk '{print $$2}')
12-
DB_USER = $(shell grep -A6 "^db:" config.yaml | grep "user:" | head -1 | cut -d'"' -f2)
13-
DB_PASS = $(shell grep -A6 "^db:" config.yaml | grep "password:" | head -1 | cut -d'"' -f2)
14-
DB_NAME = $(shell grep -A6 "^db:" config.yaml | grep "db_name:" | head -1 | cut -d'"' -f2)
20+
SRC := $(shell find . -type f -name '*.go' -not -path "./vendor/*" -not -path "./build/*")
21+
GO_PACKAGES := $(shell go list ./... | grep -v vendor)
1522

16-
UP_MIGRATION_FILE = db/migrations/000001_init_schema.up.sql
17-
DOWN_MIGRATION_FILE = db/migrations/000001_init_schema.down.sql
23+
DB_HOST := $(shell grep -A6 "^db:" config.yaml | grep "host:" | head -1 | cut -d'"' -f2)
24+
DB_PORT := $(shell grep -A6 "^db:" config.yaml | grep "port:" | head -1 | awk '{print $$2}')
25+
DB_USER := $(shell grep -A6 "^db:" config.yaml | grep "user:" | head -1 | cut -d'"' -f2)
26+
DB_PASS := $(shell grep -A6 "^db:" config.yaml | grep "password:" | head -1 | cut -d'"' -f2)
27+
DB_NAME := $(shell grep -A6 "^db:" config.yaml | grep "db_name:" | head -1 | cut -d'"' -f2)
1828

19-
.PHONY: help vendor build run dev lint format clean
29+
UP_MIGRATION_FILE := db/migrations/000001_init_schema.up.sql
30+
DOWN_MIGRATION_FILE := db/migrations/000001_init_schema.down.sql
31+
32+
.PHONY: help all vendor build run dev test lint format clean install-tools verify verify-format
33+
34+
.DEFAULT_GOAL := help
2035

2136
help:
22-
@echo "Nymeria make help"
23-
@echo ""
24-
@echo "vendor: Downloads the dependencies in the vendor folder"
25-
@echo "build: Builds the binary of the server"
26-
@echo "run: Runs the binary of the server"
27-
@echo "dev: Combines build and run commands"
28-
@echo "lint: Lints the code using vet and golangci-lint"
29-
@echo "format: Formats the code using fmt and golangci-lint"
30-
@echo "clean: Removes the vendor directory and binary"
37+
@grep -E '^[a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-20s\033[0m %s\n", $$1, $$2}'
38+
39+
all: clean vendor build
3140

3241
vendor:
33-
@${GO} mod tidy
34-
@${GO} mod vendor
35-
@echo "Vendor downloaded successfully"
42+
@$(GO) mod tidy
43+
@$(GO) mod vendor
3644

3745
build:
38-
@${GO} build -o nymeria ./cmd/nymeria/main.go
39-
@echo "Binary built successfully"
46+
@mkdir -p $(BUILD_DIR)
47+
@$(GO) build $(LDFLAGS) -o $(BUILD_DIR)/$(BINARY_NAME) $(CMD_DIR)
48+
49+
build-debug:
50+
@mkdir -p $(BUILD_DIR)
51+
@$(GO) build -gcflags="all=-N -l" -o $(BUILD_DIR)/$(BINARY_NAME)-debug $(CMD_DIR)
52+
53+
run: build
54+
@$(BUILD_DIR)/$(BINARY_NAME)
4055

41-
run:
42-
@./nymeria
56+
dev: install-air
57+
@$(AIR) -c .air.toml
4358

44-
dev:
45-
@$(GOPATH_BIN)/air -c .air.toml
59+
test:
60+
@$(GO) test -v -race -coverprofile=coverage.out $(GO_PACKAGES)
61+
@$(GO) tool cover -html=coverage.out -o coverage.html
62+
63+
test-short:
64+
@$(GO) test -short $(GO_PACKAGES)
65+
66+
install-tools: install-golangci-lint install-goimports install-air
4667

4768
install-golangci-lint:
48-
@echo "=====> Installing golangci-lint..."
49-
@curl -sSfL \
50-
https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | \
51-
sh -s -- -b $(GOPATH_BIN) v1.62.2
69+
@if [ ! -f $(GOLANGCI_LINT) ]; then \
70+
curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | \
71+
sh -s -- -b $(GOPATH_BIN) v1.62.2; \
72+
fi
73+
74+
install-goimports:
75+
@if [ ! -f $(GOIMPORTS) ]; then \
76+
$(GO) install golang.org/x/tools/cmd/goimports@latest; \
77+
fi
78+
79+
install-air:
80+
@if [ ! -f $(AIR) ]; then \
81+
curl -sSfL https://raw.githubusercontent.com/cosmtrek/air/master/install.sh | sh -s -- -b $(GOPATH_BIN); \
82+
fi
5283

5384
lint: install-golangci-lint
5485
@$(GO) vet $(GO_PACKAGES)
5586
@$(GOLANGCI_LINT) run -c golangci.yaml
56-
@echo "Lint successful"
57-
58-
install-goimports:
59-
@echo "=====> Installing formatter..."
60-
@$(GO) install golang.org/x/tools/cmd/goimports@latest
6187

6288
format: install-goimports
63-
@echo "=====> Formatting code..."
64-
@$(GOIMPORTS) -l -w -local ${PACKAGE_BASE} $(SRC)
65-
@echo "Format successful"
89+
@$(GOIMPORTS) -l -w -local $(PACKAGE_BASE) $(SRC)
90+
@$(GO) fmt $(GO_PACKAGES)
6691

67-
## verify: Run format and lint checks
68-
verify: verify-format lint
92+
verify: verify-format lint test-short
6993

70-
## verify-format: Verify the format
7194
verify-format: install-goimports
72-
@echo "=====> Verifying format..."
73-
$(if $(shell $(GOIMPORTS) -l -local ${PACKAGE_BASE} ${SRC}), @echo ERROR: Format verification failed! && $(GOIMPORTS) -l -local ${PACKAGE_BASE} ${SRC} && exit 1)
95+
@if [ -n "$$($(GOIMPORTS) -l -local $(PACKAGE_BASE) $(SRC))" ]; then \
96+
echo "ERROR: Code is not formatted properly!"; \
97+
$(GOIMPORTS) -l -local $(PACKAGE_BASE) $(SRC); \
98+
exit 1; \
99+
fi
74100

75101
clean:
76-
@rm -f nymeria
102+
@rm -rf $(BUILD_DIR)/
77103
@rm -rf vendor/
78-
@echo "Clean successful"
104+
@rm -f coverage.out coverage.html
79105

80-
install-air:
81-
@echo "Make sure your GOPATH and GOPATH_BIN is set"
82-
@curl -sSfL https://raw.githubusercontent.com/cosmtrek/air/master/install.sh | sh -s -- -b $(GOPATH_BIN)
83-
@echo "Air installed successfully"
106+
info:
107+
@echo "Project: $(PROJECT_NAME)"
108+
@echo "Version: $(VERSION)"
109+
@echo "Commit: $(COMMIT)"
110+
@echo "Build Time: $(BUILD_TIME)"
111+
@echo "Go Version: $(shell $(GO) version)"
84112

85113
apply-migration:
86-
@echo "Applying migration..."
87-
@echo "DB_HOST: $(DB_HOST)"
88-
@echo "DB_PORT: $(DB_PORT)"
89-
@echo "DB_USER: $(DB_USER)"
90-
@echo "DB_PASS: $(DB_PASS)"
91-
@echo "DB_NAME: $(DB_NAME)"
92-
PGPASSWORD=$(DB_PASS) psql -h $(DB_HOST) -p $(DB_PORT) -U $(DB_USER) -d $(DB_NAME) -f $(UP_MIGRATION_FILE)
114+
@PGPASSWORD=$(DB_PASS) psql -h $(DB_HOST) -p $(DB_PORT) -U $(DB_USER) -d $(DB_NAME) -f $(UP_MIGRATION_FILE)
93115

94116
rollback-migration:
95-
@echo "Rolling back migration..."
96-
PGPASSWORD=$(DB_PASS) psql -h $(DB_HOST) -p $(DB_PORT) -U $(DB_USER) -d $(DB_NAME) -f $(DOWN_MIGRATION_FILE)
117+
@PGPASSWORD=$(DB_PASS) psql -h $(DB_HOST) -p $(DB_PORT) -U $(DB_USER) -d $(DB_NAME) -f $(DOWN_MIGRATION_FILE)

0 commit comments

Comments
 (0)