Sidhu Alluri
4 min readOct 26, 2022

a

#################################################
# Proto rules
#################################################

.PHONY: clean-proto install-protogen

install-protogen:
@$(MAKE) -C ${MONO_HOME}/go/dev/protogen

clean-proto:

some stuff

abc

#################################################
# Proto rules
#################################################

.PHONY: clean-proto install-protogen

install-protogen:
@$(MAKE) -C ${MONO_HOME}/go/dev/protogen

clean-proto:
@echo ">>> Cleaning proto directory..."
@cd ${MONO_HOME}/go/proto && git clean -fdx .

#################################################
# Common app rules
#
# These are overridable because they affect the
# domain space of the app. There might be
# specific dependencies or the builds have to
# target specific hardware.
#################################################

.PHONY: build-default deps-default generate-default install-default test-default vet-default

proto-default:
@echo "Not defined"

deps-default: golangci-dep
@echo "Not defined"

generate-default:
@echo "Not defined"

install-default:
go install

build-default:
go build

test-default:
go test ./...

# TODO (eduardo): should not be be override-able
coverage-default:
go test -v -coverprofile /tmp/coverage.out -coverpkg=./... ./...
go tool cover -html=/tmp/coverage.out -o /tmp/coverage.html
open /tmp/coverage.html

# TODO (eduardo): should not be be override-able
vet-default:
go vet ./...

#################################################
# More common app rules
#
# These are rules that we intentionally don't
# allow apps to override (for consistency's sake)
#################################################

.PHONY: check fmt go-mod-tidy-check install-tools lint tidy
.PHONY: golangci-dep golangci-check

golangciVer=1.50.0
golangciInstallerVer=c0532b3a5f7abd4a87d8c40804d1d9fe20e7b913

golangci-dep:
@if [ ! -f "$$(go env GOPATH)/bin/golangci-lint" ]; then \
echo ">>> Installing golangci-lint..."; \
curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/$(golangciInstallerVer)/install.sh | sh -s -- -b "$$(go env GOPATH)/bin" v$(golangciVer); \
elif [ "$$(golangci-lint --version | grep -o '[0-9]*\.[0-9]*\.[0-9]*')" != $(golangciVer) ]; then \
echo ">>> Updating golangci-lint..."; \
curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/$(golangciInstallerVer)/install.sh | sh -s -- -b "$$(go env GOPATH)/bin" v$(golangciVer); \
else \
echo ">>> golangci-lint is already installed"; \
fi

# The linter was updated with a lot of new rules at this revision, so we want to only be strict with the changes since then.
golangci-check:
golangci-lint run --new-from-rev origin/main

install-goimports:
go install golang.org/x/tools/cmd/goimports@v0.1.10

fmt-imports: install-goimports
find ./ -name '*.go' -not -path './vendor/*' -not -path './/vendor/*' -not -name '*_gen.go' -exec goimports -l -w -local medium.com/ {} \;

fmt: fmt-imports
find ./ -name '*.go' -not -path './vendor/*' -not -path './/vendor/*' -not -name '*_gen.go' -exec gofmt -l -s -w {} \;

stricter-fmt: fmt-imports
find ./ -name '*.go' -not -path './vendor/*' -not -path './/vendor/*' -not -name '*_gen.go' -exec golines --shorten-comments -w {} \;
find ./ -name '*.go' -not -path './vendor/*' -not -path './/vendor/*' -not -name '*_gen.go' -exec gofumpt -l -s -w {} \;

go-mod-tidy-check:
@echo "go mod tidy"
@diffOut=$$(go mod tidy &>/dev/null && git diff --exit-code -- go.mod go.sum); \
if [ $$? -eq 1 ]; then \
echo "❌ FAILED: Uncommitted go.mod or go.sum changes"; echo "$${diffOut}"; \
echo "👉 Run 'make tidy' and commit the changes. (consider '~mono/go/gomodtidy.sh' for large changes)"; \
exit 1; \
fi

lint: install-goimports
@echo "find ./ -name '*.go' -not -path './vendor/*' -not -path './/vendor/*' -not -name '*_gen.go' -not -name '*_mock_test.go' -exec goimports -d -local medium.com/ {} \;"
@gofmtDiff=$$(find ./ -name '*.go' -not -path './vendor/*' -not -path './/vendor/*' -not -name '*_gen.go' -not -name '*_mock_test.go' -exec goimports -d -local medium.com/ {} \;); \
if [ -n "$${gofmtDiff}" ]; then \
echo "❌ goimports FAILED:"; echo "$${gofmtDiff}"; echo; \
echo "👉 Run 'make fmt'. (minimum version required: '$$(go version)')"; \
exit 1; \
fi
@echo "find ./ -name '*.go' -not -path './vendor/*' -not -path './/vendor/*' -not -name '*_gen.go' -not -name '*_mock_test.go' -exec gofmt -d -s {} \;"
@gofmtDiff=$$(find ./ -name '*.go' -not -path './vendor/*' -not -path './/vendor/*' -not -name '*_gen.go' -not -name '*_mock_test.go' -exec gofmt -d -s {} \;); \
if [ -n "$${gofmtDiff}" ]; then \
echo "❌ gofmt FAILED:"; echo "$${gofmtDiff}"; echo; \
echo "👉 Run 'make fmt'. (minimum version required: '$$(go version)')"; \
exit 1; \
fi

check: go-mod-tidy-check lint vet golangci-check

install-tools: golangci-dep
@echo ">>> Installing tools from tools.go..."
@cat tools.go 2>/dev/null | grep _ | awk -F'"' '{print $$2}' | xargs -tI % go install %

tidy: clean-proto deps generate
go mod tidy

check-deps:
@echo ">>> Checking dependency list in app.yml"
go run ../../pkg/legacy/ci/scripts/check_deps/main.go

#################################################
# General helper rules
#################################################

# HACK: do not rely on this. it will stop working in the future
.PHONY: get-tools
get-tools:
# this method of installing binaries will stop working in the future: https://github.com/golang/go/issues/43684
@echo ">>> Fetching tools from tools.go..."
@cat tools.go 2>/dev/null | grep _ | awk -F'"' '{print $$2}' | xargs -tI % go get %

.PHONY: all-default

all-default:
@echo "Define the 'all' target in the Makefile to run 'make' without arguments."

# Allows services to re-define certain rules.
%: %-default
@

# Useful to check for the existence of a target.
%-rule-exists:
@$(MAKE) -n $* > /dev/null 2>&1

.DEFAULT_GOAL:=all

abc

Sidhu Alluri
Sidhu Alluri

Responses (1)