Skip to content

Commit

Permalink
build: add rules for checking header file, golangci-lint and go modules
Browse files Browse the repository at this point in the history
  • Loading branch information
cpanato committed Jan 9, 2021
1 parent ec8b825 commit 2c797c5
Show file tree
Hide file tree
Showing 5 changed files with 149 additions and 2 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
coverage.out
.DS_Store
dist/
bin/
58 changes: 56 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,27 @@
build:
# Copyright 2021 The Kubernetes Authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# If you update this file, please follow
# https://suva.sh/posts/well-documented-makefiles

.DEFAULT_GOAL:=help
SHELL:=/usr/bin/env bash

COLOR:=\\033[36m
NOCOLOR:=\\033[0m

build: ## Build zeitgeist
go build

lint:
Expand All @@ -7,8 +30,39 @@ lint:
go get -u golang.org/x/lint/golint
golint ./...

test:
test: ## Runs unit testing
go test ./... -covermode=count -coverprofile=coverage.out

test-results: test
go tool cover -html=coverage.out

verify: verify-boilerplate verify-golangci-lint verify-go-mod ## Runs verification scripts to ensure correct execution

verify-boilerplate: ## Runs the file header check
./hack/verify-boilerplate.sh

verify-go-mod: ## Runs the go module linter
./hack/verify-go-mod.sh

verify-golangci-lint: ## Runs all golang linters
./hack/verify-golangci-lint.sh

##@ Helpers

.PHONY: help

help: ## Display this help
@awk \
-v "col=${COLOR}" -v "nocol=${NOCOLOR}" \
' \
BEGIN { \
FS = ":.*##" ; \
printf "\nUsage:\n make %s<target>%s\n", col, nocol \
} \
/^[a-zA-Z_-]+:.*?##/ { \
printf " %s%-15s%s %s\n", col, $$1, nocol, $$2 \
} \
/^##@/ { \
printf "\n%s%s%s\n", col, substr($$0, 5), nocol \
} \
' $(MAKEFILE_LIST)
33 changes: 33 additions & 0 deletions hack/verify-boilerplate.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#!/usr/bin/env bash

# Copyright 2020 The Kubernetes Authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

set -o errexit
set -o nounset
set -o pipefail

VERSION=v0.2.0
URL_BASE=https://raw.githubusercontent.com/kubernetes/repo-infra
URL=$URL_BASE/$VERSION/hack/verify_boilerplate.py
BIN_DIR=bin
SCRIPT=$BIN_DIR/verify_boilerplate.py

if [[ ! -f $SCRIPT ]]; then
mkdir -p $BIN_DIR
curl -sfL $URL -o $SCRIPT
chmod +x $SCRIPT
fi

$SCRIPT --boilerplate-dir hack/boilerplate
22 changes: 22 additions & 0 deletions hack/verify-go-mod.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#!/usr/bin/env bash

# Copyright 2020 The Kubernetes Authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

set -o errexit
set -o nounset
set -o pipefail

go mod tidy
git diff --exit-code
37 changes: 37 additions & 0 deletions hack/verify-golangci-lint.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#!/usr/bin/env bash

# Copyright 2020 The Kubernetes Authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

set -o errexit
set -o nounset
set -o pipefail

VERSION=v1.35.0
URL_BASE=https://raw.githubusercontent.com/golangci/golangci-lint
URL=$URL_BASE/$VERSION/install.sh

if [[ ! -f .golangci.yml ]]; then
echo 'ERROR: missing .golangci.yml in repo root' >&2
exit 1
fi

if ! command -v golangci-lint; then
curl -sfL $URL | sh -s $VERSION
PATH=$PATH:bin
fi

golangci-lint version
golangci-lint linters
golangci-lint run "$@"

0 comments on commit 2c797c5

Please sign in to comment.