Skip to content

Commit

Permalink
Optimize and structure Makefile
Browse files Browse the repository at this point in the history
  • Loading branch information
marcofranssen committed Jan 28, 2025
1 parent 087356f commit c5693cb
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 56 deletions.
17 changes: 9 additions & 8 deletions .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,20 +33,21 @@ jobs:
shell: bash

- name: Install dependencies
run: |
make download-dependencies
run: make deps

- name: Lint
run: |
make lint
run: make lint

- name: Run gofmt
run: |
diff -u <(echo -n) <(gofmt -d -e .)
- name: Run go vet
run: |
make go-vet
run: make vet

- name: Build plugin
run: |
make build
run: make build

- name: Test
run: |
# Github now comes with kustomize already installed
Expand Down
126 changes: 78 additions & 48 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,7 +1,72 @@
PLUGIN_NAME="ksops"

# Default to installing KSOPS
default: install
GOPATH ?= $(shell go env GOPATH)
STATICCHECK := $(GOPATH)/bin/staticcheck
KUSTOMIZE := $(GOPATH)/bin/kustomize
PREREQS := $(STATICCHECK) $(KUSTOMIZE)

.PHONY: help
help: ## Display this help.
@awk 'BEGIN {FS = ":.*##"; printf "\nUsage:\n make \033[36m<target>\033[0m\n"} /^[a-zA-Z_0-9-]+:.*?##/ { printf " \033[36m%-18s\033[0m %s\n", $$1, $$2 } /^##@/ { printf "\n\033[1m%s\033[0m\n", substr($$0, 5) } ' $(MAKEFILE_LIST)

.PHONY: FORCE
FORCE: ;

##@ Prerequisites:

.PHONY: deps
deps: ## Download go modules
@echo "Downloading go modules…"
@go mod download

$(STATICCHECK): FORCE
@echo "Installing staticcheck…"
go install honnef.co/go/tools/cmd/staticcheck@latest

.PHONY: ci-tools
prereqs: $(PREREQS) ## Install prerequisites

$(KUSTOMIZE): FORCE
./scripts/install-kustomize.sh

##@ Development:

.PHONY: setup
setup: .git/hooks/pre-push .git/hooks/pre-commit $(KUSTOMIZE) sops deps ## Setup the development environment

.PHONY: tidy
tidy: ## Run go mod tidy
@echo "Running go mod tidy…"
@go mod tidy

$(PLUGIN_NAME): FORCE
@echo "Building $(PLUGIN_NAME)"
@go build -o $@

.PHONY: build
build: $(PLUGIN_NAME) ## Compile the plugin

.PHONY: test
test: ## Run go tests
@echo "Running go test…"
@go test -v -race -count=1 ./...

.PHONY: fmt
fmt: ## Run go fmt
@echo "Running go fmt…"
@go fmt .

.PHONY: vet
vet: ## Run go vet
@echo "Running go vet…"
@go vet -v ./...

.PHONY: lint
lint: $(STATICCHECK) ## Run staticcheck linter
@echo "Running staticcheck…"
@$<

##@ Install:

.PHONY: install
install: go-install clean build install-plugin
Expand All @@ -10,34 +75,17 @@ install: go-install clean build install-plugin
install-plugin:
./scripts/install-ksops.sh

.PHONY: build
build:
go build -o $(PLUGIN_NAME)

.PHONY: clean
clean:
rm -f $(PLUGIN_NAME)
rm -rf $(XDG_CONFIG_HOME)/kustomize/plugin/viaduct.ai/v1/ || true
rm -rf $(HOME)/sigs.k8s.io/kustomize/plugin/viaduct.ai/v1/ || true
rm -f $(shell command -v $(PLUGIN_NAME))

.PHONY: kustomize
kustomize:
./scripts/install-kustomize.sh

.PHONY: sops
sops:
go get -u go.mozilla.org/sops/cmd/sops

.PHONY: download-dependencies
download-dependencies:
go mod download
go mod tidy
go install honnef.co/go/tools/cmd/staticcheck@latest

.PHONY: setup
setup: .git/hooks/pre-push .git/hooks/pre-commit kustomize sops download-dependencies

.PHONY: import-test-keys
import-test-keys:
gpg --import test/key.asc
Expand All @@ -53,40 +101,22 @@ setup-test-files:
go-install:
go install

.PHONY: go-test
go-test:
go test -v ./...

.PHONY: go-fmt
go-fmt:
go fmt .

.PHONY: go-vet
go-vet:
go vet -v ./...


.PHONY: lint
lint:
staticcheck
##@ Git Hooks:

install-git-hooks: .git/hooks/pre-push .git/hooks/pre-commit ## Install Git hooks

################################################################################
# Git Hooks
################################################################################
## Git hooks to validate worktree is clean before commit/push
.git/hooks/pre-push: Makefile
# Create Git pre-push hook
echo 'make pre-push' > .git/hooks/pre-push
chmod +x .git/hooks/pre-push
.git/hooks/pre-push:
@echo "Installing pre-push hook…"
@echo 'make pre-push' > .git/hooks/pre-push
@chmod +x .git/hooks/pre-push

.git/hooks/pre-commit: Makefile
# Create Git pre-commit hook
echo 'make pre-commit' > .git/hooks/pre-commit
chmod +x .git/hooks/pre-commit
.git/hooks/pre-commit:
@echo "Installing pre-commit hook"
@echo 'make pre-commit' > .git/hooks/pre-commit
@chmod +x .git/hooks/pre-commit

.PHONY: pre-commit
pre-commit: download-dependencies lint go-fmt go-vet
pre-commit: download-dependencies lint fmt vet

.PHONY: pre-push
pre-push: test

0 comments on commit c5693cb

Please sign in to comment.