| 1 |
## |
| 2 |
# Console Colors |
| 3 |
## |
| 4 |
GREEN := $(shell printf "\033[0;32m") |
| 5 |
YELLOW := $(shell printf "\033[0;33m") |
| 6 |
WHITE := $(shell printf "\033[0;37m") |
| 7 |
CYAN := $(shell printf "\033[0;36m") |
| 8 |
RESET := $(shell printf "\033[0m") |
| 9 |
|
| 10 |
# Get the current working directory |
| 11 |
CURRENT_DIR := $(CURDIR) |
| 12 |
|
| 13 |
# Get the directory name of the current working directory |
| 14 |
PROJECT_NAME := $(notdir $(CURRENT_DIR)) |
| 15 |
|
| 16 |
# Set the local OpenVPN plugin output path |
| 17 |
PLUGIN_PATH := $(CURRENT_DIR)/$(PROJECT_NAME).so |
| 18 |
PLUGIN_HEADER_PATH := $(CURRENT_DIR)/$(PROJECT_NAME).h |
| 19 |
|
| 20 |
# Set the integration test image |
| 21 |
IT_IMAGE := ghcr.io/jkroepke/openvpn-auth-oauth2/it:latest |
| 22 |
|
| 23 |
# Get the GOOS value |
| 24 |
GOOS := $(shell go env GOOS) |
| 25 |
|
| 26 |
# Get the GOARCH value |
| 27 |
GOARCH := $(shell go env GOARCH) |
| 28 |
|
| 29 |
# Build a Linux plugin for the Linux integration test container |
| 30 |
ifeq ($(GOOS),linux) |
| 31 |
PLUGIN_BUILD_ENV := CGO_ENABLED=1 |
| 32 |
else ifeq ($(GOARCH),amd64) |
| 33 |
PLUGIN_BUILD_ENV := GOOS=linux GOARCH=amd64 CGO_ENABLED=1 CC="zig cc -target x86_64-linux-gnu.2.17" |
| 34 |
else ifeq ($(GOARCH),arm64) |
| 35 |
PLUGIN_BUILD_ENV := GOOS=linux GOARCH=arm64 CGO_ENABLED=1 CC="zig cc -target aarch64-linux-gnu.2.17" |
| 36 |
endif |
| 37 |
|
| 38 |
# Disable CGO for static binaries |
| 39 |
CGO_ENABLED := 0 |
| 40 |
|
| 41 |
# Determine the output file extension based on the GOOS value |
| 42 |
ifeq ($(GOOS),windows) |
| 43 |
EXT := .exe |
| 44 |
else |
| 45 |
EXT := |
| 46 |
endif |
| 47 |
|
| 48 |
## |
| 49 |
# Targets |
| 50 |
## |
| 51 |
.PHONY: help |
| 52 |
help: ## show this help. |
| 53 |
@echo "Project: $(PROJECT_NAME)" |
| 54 |
@echo 'Usage:' |
| 55 |
@echo " ${GREEN}make${RESET} ${YELLOW}<target>${RESET}" |
| 56 |
@echo '' |
| 57 |
@echo 'Targets:' |
| 58 |
@awk 'BEGIN {FS = ":.*?## "} { \ |
| 59 |
if (/^[a-zA-Z_-]+:.*?##.*$$/) {printf " ${GREEN}%-21s${YELLOW}%s${RESET}\n", $$1, $$2} \ |
| 60 |
else if (/^## .*$$/) {printf " ${CYAN}%s${RESET}\n", substr($$1,4)} \ |
| 61 |
}' $(MAKEFILE_LIST) | sort |
| 62 |
|
| 63 |
.PHONY: clean |
| 64 |
clean: ## clean builds dir |
| 65 |
@rm -rf "$(PROJECT_NAME)" "$(PROJECT_NAME).exe" "$(PLUGIN_PATH)" "$(PLUGIN_HEADER_PATH)" dist/ |
| 66 |
|
| 67 |
.PHONY: check |
| 68 |
check: test lint golangci ## Run all checks locally |
| 69 |
|
| 70 |
.PHONY: update |
| 71 |
update: ## Run dependency updates |
| 72 |
@go get -u ./... |
| 73 |
@go mod tidy |
| 74 |
|
| 75 |
.PHONY: renovate |
| 76 |
renovate: SHELL := /bin/bash |
| 77 |
renovate: ## Update Renovate test-only Go dependencies |
| 78 |
@set -euo pipefail; \ |
| 79 |
production="$$(mktemp)"; \ |
| 80 |
tests="$$(mktemp)"; \ |
| 81 |
direct="$$(mktemp)"; \ |
| 82 |
test_only="$$(mktemp)"; \ |
| 83 |
config="$$(mktemp)"; \ |
| 84 |
trap 'rm -f "$$production" "$$tests" "$$direct" "$$test_only" "$$config"' EXIT; \ |
| 85 |
go list -deps -f '{{with .Module}}{{.Path}}{{end}}' ./... | sort -u > "$$production"; \ |
| 86 |
go list -deps -test -f '{{with .Module}}{{.Path}}{{end}}' ./... | sort -u > "$$tests"; \ |
| 87 |
go list -m -f '{{if not .Indirect}}{{.Path}}{{end}}' all | sort -u > "$$direct"; \ |
| 88 |
comm -13 "$$production" "$$tests" > "$$test_only"; \ |
| 89 |
test_dependencies="$$(comm -12 "$$test_only" "$$direct")"; \ |
| 90 |
jq --arg dependencies "$$test_dependencies" \ |
| 91 |
'(.packageRules[] | select(.groupName == "Go test dependencies").matchPackageNames) = \ |
| 92 |
($$dependencies | split("\n") | map(select(length > 0)))' \ |
| 93 |
renovate.json > "$$config"; \ |
| 94 |
cp "$$config" renovate.json |
| 95 |
|
| 96 |
.PHONY: build ## Build the project |
| 97 |
build: clean $(PROJECT_NAME) |
| 98 |
|
| 99 |
$(PROJECT_NAME): |
| 100 |
@go build -tags no_otel -o $(PROJECT_NAME)$(EXT) ./cmd/openvpn-auth-oauth2 |
| 101 |
|
| 102 |
.PHONY: test |
| 103 |
test: ## Test the project |
| 104 |
@GOEXPERIMENT=cgocheck2 go test -race ./... |
| 105 |
|
| 106 |
.PHONY: test-it |
| 107 |
test-it: ## Run integration tests |
| 108 |
@$(PLUGIN_BUILD_ENV) go build -buildvcs=false -buildmode=c-shared -o "$(PLUGIN_PATH)" ./lib/openvpn-auth-oauth2 |
| 109 |
@GOEXPERIMENT=cgocheck2 OPENVPN_IT_TEST=1 PLUGIN_IT_TEST=1 PLUGIN_IT_PLUGIN_PATH="$(PLUGIN_PATH)" \ |
| 110 |
go test -race -count=1 -timeout=15m -run '^TestIT(EnforceUniqueUser)?$$' ./internal ./lib/openvpn-auth-oauth2 |
| 111 |
|
| 112 |
.PHONY: lint |
| 113 |
lint: golangci ## Run linter |
| 114 |
|
| 115 |
.PHONY: textlint |
| 116 |
textlint: ## Run textlint |
| 117 |
npx --yes --package textlint --package textlint-rule-terminology textlint --rule terminology . |
| 118 |
|
| 119 |
.PHONY: fmt ## Format code |
| 120 |
fmt: |
| 121 |
@go mod tidy |
| 122 |
# ignoring exit codes from linter, because they exited non zero, if they format code. |
| 123 |
@-go fmt ./... |
| 124 |
@-go run github.com/daixiang0/[email protected] write . |
| 125 |
@-go run mvdan.cc/[email protected] -l -w . |
| 126 |
@-go run golang.org/x/tools/cmd/[email protected] -l -w . |
| 127 |
@-go run github.com/bombsimon/wsl/v5/cmd/[email protected] -fix ./... |
| 128 |
@-go run github.com/catenacyber/[email protected] --fix ./... |
| 129 |
@-go run github.com/tetafro/godot/cmd/[email protected] -w . |
| 130 |
@-go run github.com/4meepo/tagalign/cmd/[email protected] -fix -sort ./... |
| 131 |
@-go run github.com/t34-dev/go-field-alignment/v2/cmd/[email protected] --files . -fix |
| 132 |
@-go run golang.org/x/tools/go/analysis/passes/fieldalignment/cmd/[email protected] -test=false -fix ./... |
| 133 |
@-go run github.com/golangci/golangci-lint/v2/cmd/[email protected] run ./... |
| 134 |
|
| 135 |
.PHONY: golangci |
| 136 |
golangci: |
| 137 |
@go run github.com/golangci/golangci-lint/v2/cmd/[email protected] run ./... |
| 138 |
|
| 139 |
.PHONY: 3rdpartylicenses |
| 140 |
3rdpartylicenses: |
| 141 |
@go run github.com/google/go-licenses/[email protected] save ./cmd/openvpn-auth-oauth2 --save_path=3rdpartylicenses |