Skip to content

Commit

Permalink
*: Makefile and other general improvements
Browse files Browse the repository at this point in the history
Signed-off-by: Joe Lanford <joe.lanford@gmail.com>
  • Loading branch information
joelanford committed Jan 16, 2021
1 parent c960930 commit e7780fe
Show file tree
Hide file tree
Showing 5 changed files with 102 additions and 54 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ jobs:
- name: Get dependencies
run: go mod download

- name: Build
run: make build
- name: check
run: make fix

- name: Test
run: make test
Expand All @@ -54,7 +54,7 @@ jobs:
- name: Lint
uses: golangci/golangci-lint-action@v2
with:
version: v1.29
version: v1.35.2

deploy:
name: Deploy
Expand Down
39 changes: 30 additions & 9 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,25 +1,46 @@
# Binaries for programs and plugins
/bin
/tools/bin

# Other IDE files
.idea

# Created by https://www.toptal.com/developers/gitignore/api/go,vim
# Edit at https://www.toptal.com/developers/gitignore?templates=go,vim

### Go ###
# Binaries for programs and plugins
*.exe
*.exe~
*.dll
*.so
*.dylib
bin
testbin

# Test binary, build with `go test -c`
# Test binary, built with `go test -c`
*.test

# Output of the go coverage tool, specifically when used with LiteIDE
*.out

# Kubernetes Generated files - skip generated files, except for vendored files
### Vim ###
# Swap
[._]*.s[a-v][a-z]
!*.svg # comment out if you don't need vector files
[._]*.sw[a-p]
[._]s[a-rt-v][a-z]
[._]ss[a-gi-z]
[._]sw[a-p]

!vendor/**/zz_generated.*
# Session
Session.vim
Sessionx.vim

# editor and IDE paraphernalia
.idea
*.swp
*.swo
# Temporary
.netrwhist
*~
# Auto-generated tag files
tags
# Persistent undo
[._]*.un~

# End of https://www.toptal.com/developers/gitignore/api/go,vim
4 changes: 2 additions & 2 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
run:
timeout: 5m
linters:
disable-all: true
enable:
Expand All @@ -13,5 +15,3 @@ linters:
- typecheck
- gofmt
- goconst
run:
deadline: 5m
69 changes: 29 additions & 40 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,64 +1,53 @@

# Image URL to use all building/pushing image targets
IMG ?= quay.io/joelanford/helm-operator

# Build settings
SHELL=/bin/bash
# Get the currently used golang install path (in GOPATH/bin, unless GOBIN is set)
ifeq (,$(shell go env GOBIN))
GOBIN=$(shell go env GOPATH)/bin
else
GOBIN=$(shell go env GOBIN)
endif

# GO_BUILD_ARGS should be set when running 'go build' or 'go install'.
VERSION_PKG = "$(shell go list -m)/internal/version"
SCAFFOLD_VERSION = $(shell git describe --abbrev=0)
GIT_VERSION = $(shell git describe --dirty --tags --always)
GIT_COMMIT = $(shell git rev-parse HEAD)
BUILD_DIR = $(PWD)/bin
GO_BUILD_ARGS = \
-gcflags "all=-trimpath=$(shell dirname $(shell pwd))" \
-asmflags "all=-trimpath=$(shell dirname $(shell pwd))" \
-ldflags " \
-s \
-w \
-X '$(VERSION_PKG).ScaffoldVersion=$(SCAFFOLD_VERSION)' \
-X '$(VERSION_PKG).GitVersion=$(GIT_VERSION)' \
-X '$(VERSION_PKG).GitCommit=$(GIT_COMMIT)' \
" \

export GO111MODULE = on

TOOLS_DIR=$(PWD)/tools
SCRIPTS_DIR=$(TOOLS_DIR)/scripts
export PATH := $(BUILD_DIR):$(TOOlS_DIR)/bin:$(PATH)

# Run tests
ENVTEST_ASSETS_DIR=$(shell pwd)/testbin
test: fmt vet
mkdir -p ${ENVTEST_ASSETS_DIR}
test -f ${ENVTEST_ASSETS_DIR}/setup-envtest.sh || curl -sSLo ${ENVTEST_ASSETS_DIR}/setup-envtest.sh https://raw.githubusercontent.com/kubernetes-sigs/controller-runtime/master/hack/setup-envtest.sh
source ${ENVTEST_ASSETS_DIR}/setup-envtest.sh; fetch_envtest_tools $(ENVTEST_ASSETS_DIR); setup_envtest_env $(ENVTEST_ASSETS_DIR); go test -race -covermode atomic -coverprofile cover.out ./...
.PHONY: test
export KUBEBUILDER_ASSETS := $(PWD)/tools/bin
test:
$(SCRIPTS_DIR)/fetch envtest 0.8.0
go test -race -covermode atomic -coverprofile cover.out ./...

# Build manager binary
build: fmt vet
CGO_ENABLED=0 go build $(GO_BUILD_ARGS) -o bin/helm-operator main.go

# Run go fmt against code
fmt:
.PHONY: build
build:
CGO_ENABLED=0 mkdir -p $(BUILD_DIR) && go build $(GO_BUILD_ARGS) -o $(BUILD_DIR) ./main.go

# Run go fmt and go mod tidy, and check for clean git tree
.PHONY: fix
fix:
go mod tidy
go fmt ./...
git diff --exit-code

# Run go vet against code
vet:
go vet ./...

lint: golangci-lint
$(GOLANGCI_LINT) run
lint-fix: golangci-lint ## Run golangci-lint linter and perform fixes
$(GOLANGCI_LINT) run --fix
# Run various checks against code
.PHONY: lint
lint:
$(SCRIPTS_DIR)/fetch golangci-lint 1.35.2 && golangci-lint run

# find or download controller-gen
# download controller-gen if necessary
golangci-lint:
ifeq (, $(shell which golangci-lint))
@{ \
set -e ;\
curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(shell go env GOPATH)/bin v1.27.0 ;\
}
GOLANGCI_LINT=$(shell go env GOPATH)/bin/golangci-lint
else
GOLANGCI_LINT=$(shell which golangci-lint)
endif
.PHONY: clean
clean:
rm -rf $(TOOLS_DIR)/bin $(BUILD_DIR)
38 changes: 38 additions & 0 deletions tools/scripts/fetch
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#!/usr/bin/env bash

ROOT="$(git rev-parse --show-toplevel)"
DEST="${ROOT}/tools/bin"

fetch() {
local tool=$1; shift
local ver=$1; shift

local ver_cmd=""
local fetch_cmd=""
case "$tool" in
"golangci-lint")
ver_cmd="${DEST}/golangci-lint --version 2>/dev/null | cut -d\" \" -f4"
fetch_cmd="curl -sSfL \"https://raw.githubusercontent.com/golangci/golangci-lint/v${ver}/install.sh\" | sh -s -- -b \"${DEST}\" \"v${ver}\""
;;
"goreleaser")
ver_cmd="${DEST}/goreleaser --version 2>/dev/null | grep version | cut -d' ' -f3"
fetch_cmd="curl -sSfL https://install.goreleaser.com/github.com/goreleaser/goreleaser.sh | sh -s -- -b \"${DEST}\" -d \"v${ver}\""
;;
"envtest")
ver_cmd="cat ${DEST}/.envtest_version 2>/dev/null"
fetch_cmd="(test -f ${DEST}/setup-envtest.sh || curl -sSLo ${DEST}/setup-envtest.sh https://raw.githubusercontent.com/kubernetes-sigs/controller-runtime/v${ver}/hack/setup-envtest.sh) && (source ${DEST}/setup-envtest.sh; fetch_envtest_tools ${DEST}/../) && echo ${ver} > ${DEST}/.envtest_version"
;;
*)
echo "unknown tool $tool"
return 1
;;
esac

if [[ "${ver}" != "$(eval ${ver_cmd})" ]]; then
echo "${tool} missing or not version '${ver}', downloading..."
mkdir -p ${DEST}
eval ${fetch_cmd}
fi
}

fetch $@

0 comments on commit e7780fe

Please sign in to comment.