Skip to content

Commit

Permalink
Adds editorconfig + pre-commit (#403)
Browse files Browse the repository at this point in the history
  • Loading branch information
0sewa0 authored Dec 7, 2021
1 parent 140ca0c commit f6e0e0b
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 0 deletions.
16 changes: 16 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# http://editorconfig.org

root = true

[*]
charset = utf-8
end_of_line = lf
insert_final_newline = true
trim_trailing_whitespace = true

[*.go]
indent_style = tab

[*.yaml]
indent_style = space
indent_size = 2
54 changes: 54 additions & 0 deletions .github/pre-commit
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
#!/bin/bash

# Check linting and unit tests before commiting
# if linting failes the commit will be aborted and the linting will start auto fixing
# if unit tests fail the commit will be aborted

# Linting

echo Shell: $SHELL

## this will retrieve all of the .go files that have been
## changed since the last commit
STAGED_GO_FILES=$(git diff --cached --name-only -- '*.go')

## we can check to see if this is empty
if [[ $STAGED_GO_FILES == "" ]]; then
echo "No Go Files to Update"
exit 0
## otherwise we can do stuff with these changed go files
else
for file in $STAGED_GO_FILES; do
gofmt -w $file
goimports -w $file
gci -w $file
## add any potential changes from our formatting to the
## commit
git add $file
done
fi

golangci-lint run --build-tags integration,containers_image_storage_stub
res=$?
if [[ $res -ne 0 ]]; then
echo "Linting failed"
exit 2
fi
# Unit tests
#exec < /dev/tty
#read -p "Run unit tests[y/n]? " -n 1 -r
#exec <&-
REPLY=y
if [[ $REPLY =~ ^[Yy]$ ]]; then
echo "Run unit tests..."
go test ./...
res=$?
if [[ $res -ne 0 ]]; then
echo "Unit test failed"
exit 2
fi
fi
echo
7 changes: 7 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -213,3 +213,10 @@ bundle-minimal: bundle
.PHONY: bundle-build
bundle-build:
docker build -f bundle.Dockerfile -t $(BUNDLE_IMG) .

setup-pre-commit:
$(info WARNING Make sure that golangci-lint is installed, for more info see https://golangci-lint.run/usage/install/")
GO111MODULE=off go get github.com/daixiang0/gci
GO111MODULE=off go get golang.org/x/tools/cmd/goimports
cp ./.github/pre-commit ./.git/hooks/pre-commit
chmod +x ./.git/hooks/pre-commit

0 comments on commit f6e0e0b

Please sign in to comment.