Skip to content

Commit

Permalink
Optimize and structure Makefile
Browse files Browse the repository at this point in the history
Signed-off-by: Marco Franssen <[email protected]>
  • Loading branch information
marcofranssen committed Jan 28, 2025
1 parent 294de9d commit fda8d09
Show file tree
Hide file tree
Showing 3 changed files with 120 additions and 78 deletions.
32 changes: 19 additions & 13 deletions .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,24 +32,30 @@ jobs:
echo "$(dirname $GITHUB_WORKSPACE)/bin" >> $GITHUB_PATH
shell: bash

- name: Install dependencies
- name: Install pre-requisites
run: |
make download-dependencies
# Github now comes with kustomize already installed
sudo rm $(command -v kustomize) || true
make prereqs
- name: Install 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: Install
run: make install

- name: Test
run: |
# Github now comes with kustomize already installed
sudo rm $(command -v kustomize) || true
make kustomize
XDG_CONFIG_HOME=$HOME/.config make test
run: XDG_CONFIG_HOME=$HOME/.config make test
160 changes: 101 additions & 59 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,92 +1,134 @@
PLUGIN_NAME="ksops"

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

.PHONY: install
install: go-install clean build install-plugin
GIT_HOOKS := pre-push pre-commit

.PHONY: install-plugin
install-plugin:
./scripts/install-ksops.sh
GO_LD_FLAGS := "-w -s"
GO_BUILD_FLAGS := -trimpath -ldflags $(GO_LD_FLAGS)

.PHONY: build
build:
go build -o $(PLUGIN_NAME)
.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: 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: FORCE
FORCE: ;

.PHONY: kustomize
kustomize:
./scripts/install-kustomize.sh
##@ Prerequisites:

.PHONY: sops
sops:
go get -u go.mozilla.org/sops/cmd/sops
.PHONY: deps
deps: ## Download go modules
@echo "Downloading go modules…"
@go mod download

.PHONY: download-dependencies
download-dependencies:
go mod download
go mod tidy
go install honnef.co/go/tools/cmd/staticcheck@latest
$(STATICCHECK): FORCE
@echo "Installing staticcheck…"
@go install honnef.co/go/tools/cmd/staticcheck@latest

$(GO_MOD_OUTDATED): FORCE
@echo "Installing go-mod-outdated…"
@go install github.com/psampaz/go-mod-outdated@latest

$(SOPS): FORCE
@echo "Installing sops…"
@go install go.mozilla.org/sops/cmd/sops@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 download-dependencies
setup: install-git-hooks $(KUSTOMIZE) $(SOPS) deps ## Setup the development environment

.PHONY: install-git-hooks
install-git-hooks: $(addprefix .git/hooks/,$(GIT_HOOKS)) ## Install Git hooks

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

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

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

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

.PHONY: import-test-keys
import-test-keys:
import-test-keys: ## Import GPG test keys
@echo "Importing GPG test keys…"
gpg --import test/key.asc

.PHONY: test
test: install setup-test-files go-test
.PHONY: fmt
fmt: ## Run go fmt
@echo "Running go fmt…"
@go fmt .

.PHONY: setup-test-files
setup-test-files:
./scripts/setup-test-files.sh
.PHONY: vet
vet: ## Run go vet
@echo "Running go vet…"
@go vet -v ./...

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

.PHONY: go-test
go-test:
go test -v ./...
.PHONY: outdated
outdated: $(GO_MOD_OUTDATED) ## List outdated dependencies.
@go list -u -m -json all | $< -update

.PHONY: go-fmt
go-fmt:
go fmt .
##@ Install:

.PHONY: go-vet
go-vet:
go vet -v ./...
.PHONY: install
install: go-install clean build install-plugin

.PHONY: install-plugin
install-plugin: ## Install the ksops plugin
./scripts/install-ksops.sh

.PHONY: lint
lint:
staticcheck
.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: go-install
go-install:
go install

################################################################################
# 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-commit: Makefile
# Create Git pre-commit hook
echo 'make pre-commit' > .git/hooks/pre-commit
chmod +x .git/hooks/pre-commit
.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:
@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
6 changes: 0 additions & 6 deletions scripts/setup-test-files.sh

This file was deleted.

0 comments on commit fda8d09

Please sign in to comment.