Skip to content

Commit

Permalink
add makefiles (#3)
Browse files Browse the repository at this point in the history
  • Loading branch information
MatousJobanek authored Mar 14, 2024
1 parent bdb3246 commit 04f3a45
Show file tree
Hide file tree
Showing 9 changed files with 154 additions and 0 deletions.
5 changes: 5 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# It's necessary to set this because some environments don't link sh -> bash.
SHELL := /bin/bash

include ./make/*.mk
.DEFAULT_GOAL := help
9 changes: 9 additions & 0 deletions make/clean.mk
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
.PHONY: clean
clean: clean-bin
$(Q)-rm -rf ${V_FLAG} ./vendor
$(Q)-rm -rf ($COV_DIR)
$(Q)go clean ${X_FLAG} ./...

.PHONY: clean-bin
clean-bin:
@rm -rf $(BIN_DIR) 2>/dev/null || true
16 changes: 16 additions & 0 deletions make/format.mk
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@

.PHONY: check-go-format
## Exits with an error if there are files that do not match formatting defined by gofmt
check-go-format:
$(Q)gofmt -s -l ${GOFORMAT_FILES} 2>&1 \
| tee $(OUT_DIR)/gofmt-errors \
| read \
&& echo "ERROR: These files differ from gofmt's style (run 'make format-go-code' to fix this):" \
&& cat $(OUT_DIR)/gofmt-errors \
&& exit 1 \
|| true

.PHONY: format-go-code
## Formats any go file that does not match formatting defined by gofmt
format-go-code:
$(Q)gofmt -s -l -w ./
11 changes: 11 additions & 0 deletions make/git.mk
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
GIT_COMMIT_ID := $(shell git rev-parse HEAD)
ifneq ($(shell git status --porcelain --untracked-files=no),)
GIT_COMMIT_ID := $(GIT_COMMIT_ID)-dirty
endif

GIT_COMMIT_ID_SHORT := $(shell git rev-parse --short HEAD)
ifneq ($(shell git status --porcelain --untracked-files=no),)
GIT_COMMIT_ID_SHORT := $(GIT_COMMIT_ID_SHORT)-dirty
endif

BUILD_TIME = `date -u '+%Y-%m-%dT%H:%M:%SZ'`
21 changes: 21 additions & 0 deletions make/go.mk
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
GO_PACKAGE_ORG_NAME ?= kubesaw
GO_PACKAGE_REPO_NAME ?= ksctl
GO_PACKAGE_PATH ?= github.com/${GO_PACKAGE_ORG_NAME}/${GO_PACKAGE_REPO_NAME}

BIN_DIR := bin
.PHONY: build
## Build the operator
build: GO_COMMAND=build
build: GO_EXTRA_FLAGS=-o $(BIN_DIR)/
build: clean-bin run-go

.PHONY: install
## installs the binary executable
install: GO_COMMAND=install
install: run-go

run-go:
$(Q)CGO_ENABLED=0 \
go ${GO_COMMAND} ${V_FLAG} \
-ldflags "-X ${GO_PACKAGE_PATH}/pkg/version.Commit=${GIT_COMMIT_ID} -X ${GO_PACKAGE_PATH}/pkg/version.BuildTime=${BUILD_TIME}" \
${GO_EXTRA_FLAGS} ${GO_PACKAGE_PATH}/cmd/...
24 changes: 24 additions & 0 deletions make/help.mk
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
.PHONY: help
# Based on https://gist.github.com/rcmachado/af3db315e31383502660
## Display this help text
help:/
$(info Available targets)
$(info -----------------)
@awk '/^[a-zA-Z\-%\_0-9]+:/ { \
helpMessage = match(lastLine, /^## (.*)/); \
helpCommand = substr($$1, 0, index($$1, ":")-1); \
if (helpMessage) { \
helpMessage = substr(lastLine, RSTART + 3, RLENGTH); \
gsub(/##/, "\n ", helpMessage); \
printf "%-35s - %s\n", helpCommand, helpMessage; \
lastLine = "" \
} \
} \
{ hasComment = match(lastLine, /^## (.*)/); \
if(hasComment) { \
lastLine=lastLine$$0; \
} \
else { \
lastLine = $$0 \
} \
}' $(MAKEFILE_LIST)
16 changes: 16 additions & 0 deletions make/lint.mk
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
.PHONY: lint
## Runs linters on Go code files and YAML files
lint: lint-go-code lint-yaml

.PHONY: lint-yaml
## runs yamllint on all yaml files
lint-yaml:
$(Q)yamllint -c .yamllint ./

.PHONY: lint-go-code
# Checks the code with golangci-lint
lint-go-code:
ifeq (, $(shell which golangci-lint 2>/dev/null))
$(error "golangci-lint not found in PATH. Please install it using instructions on https://golangci-lint.run/usage/install/#local-installation")
endif
golangci-lint ${V_FLAG} run -c .golangci.yml
28 changes: 28 additions & 0 deletions make/test.mk
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
############################################################
#
# (local) Tests
#
############################################################

.PHONY: test
## runs the tests without coverage and excluding E2E tests
test:
@echo "running the tests without coverage and excluding E2E tests..."
$(Q)go test ${V_FLAG} -parallel 1 -failfast ./...

############################################################
#
# OpenShift CI Tests with Coverage
#
############################################################

# Output directory for coverage information
COV_DIR = out/coverage

.PHONY: test-with-coverage
## runs the tests with coverage
test-with-coverage:
@echo "running the tests with coverage..."
@-mkdir -p $(COV_DIR)
@-rm $(COV_DIR)/coverage.txt
$(Q)go test -parallel=1 -vet off ${V_FLAG} -coverprofile=$(COV_DIR)/coverage.txt -covermode=atomic ./...
24 changes: 24 additions & 0 deletions make/verbose.mk
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# When you run make VERBOSE=1 (the default), executed commands will be printed
# before executed. If you run make VERBOSE=2 verbose flags are turned on and
# quiet flags are turned off for various commands. Use V_FLAG in places where
# you can toggle on/off verbosity using -v. Use Q_FLAG in places where you can
# toggle on/off quiet mode using -q. Use S_FLAG where you want to toggle on/off
# silence mode using -s...
VERBOSE ?= 1
Q = @
Q_FLAG = -q
QUIET_FLAG = --quiet
V_FLAG =
S_FLAG = -s
X_FLAG =
ifeq ($(VERBOSE),1)
Q =
endif
ifeq ($(VERBOSE),2)
Q =
Q_FLAG =
QUIET_FLAG =
S_FLAG =
V_FLAG = -v
X_FLAG = -x
endif

0 comments on commit 04f3a45

Please sign in to comment.