Skip to content

Commit

Permalink
Merge pull request #9 from devilbox/improve-linting
Browse files Browse the repository at this point in the history
Improve linting
  • Loading branch information
cytopia authored Oct 24, 2022
2 parents bc0da08 + 1ee8dfd commit 1138af8
Show file tree
Hide file tree
Showing 3 changed files with 146 additions and 11 deletions.
56 changes: 56 additions & 0 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
---

# -------------------------------------------------------------------------------------------------
# Job Name
# -------------------------------------------------------------------------------------------------
name: lint


# -------------------------------------------------------------------------------------------------
# When to run
# -------------------------------------------------------------------------------------------------
on:
# Runs on Pull Requests
pull_request:


# -------------------------------------------------------------------------------------------------
# What to run
# -------------------------------------------------------------------------------------------------
jobs:
# -----------------------------------------------------------------------------------------------
# JOB (1/1): Lint
# -----------------------------------------------------------------------------------------------
lint:
name: lint
runs-on: ubuntu-latest
steps:

- name: "[SETUP] Checkout repository"
uses: actions/checkout@v3
with:
fetch-depth: 0

- name: Lint Files
uses: cytopia/shell-command-retry-action@v0.1.3
with:
command: |
make -f Makefile.lint lint-files
- name: Lint Yaml
uses: cytopia/shell-command-retry-action@v0.1.3
with:
command: |
make -f Makefile.lint lint-yaml
- name: Lint JSON
uses: cytopia/shell-command-retry-action@v0.1.3
with:
command: |
make -f Makefile.lint lint-json
- name: Lint Bash
uses: cytopia/shell-command-retry-action@v0.1.3
with:
command: |
make -f Makefile.lint lint-bash
13 changes: 13 additions & 0 deletions .yamllint
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
---
extends: default

ignore: |
.yamllint


rules:
truthy:
allowed-values: ['true', 'false']
check-keys: False
level: error
line-length: disable
88 changes: 77 additions & 11 deletions Makefile.lint
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,23 @@ endif
# -------------------------------------------------------------------------------------------------
CURRENT_DIR = $(dir $(abspath $(lastword $(MAKEFILE_LIST))))
# File lint
FL_VERSION = 0.4
FL_VERSION = latest
FL_IGNORES = .git/,.github/
# Json lint
JL_VERSION = latest
JL_IGNORES = .git/,.github/
# Yamllint
YL_VERSION = 1.26-0.9
YL_VERSION = latest
# Shellcheck
SC_IGNORES = .git/,.github/
SC_VERSION = stable

# Ensure to use correct docker platform for linting images
LINT_ARCH = linux/amd64
ifeq ($(strip $(shell uname -m)),arm64)
LINT_ARCH = linux/arm64
endif


# -------------------------------------------------------------------------------------------------
# Lint Targets
Expand All @@ -35,25 +41,27 @@ lint: lint-bash


.PHONY: lint-files
lint-files: _lint-files-pull
lint-files:
@echo "################################################################################"
@echo "# Lint Files"
@echo "################################################################################"
@docker run --rm $$(tty -s && echo "-it" || echo) -v $(CURRENT_DIR):/data cytopia/file-lint:$(FL_VERSION) file-cr --text --ignore '$(FL_IGNORES)' --path .
@docker run --rm $$(tty -s && echo "-it" || echo) -v $(CURRENT_DIR):/data cytopia/file-lint:$(FL_VERSION) file-crlf --text --ignore '$(FL_IGNORES)' --path .
@docker run --rm $$(tty -s && echo "-it" || echo) -v $(CURRENT_DIR):/data cytopia/file-lint:$(FL_VERSION) file-trailing-single-newline --text --ignore '$(FL_IGNORES)' --path .
@docker run --rm $$(tty -s && echo "-it" || echo) -v $(CURRENT_DIR):/data cytopia/file-lint:$(FL_VERSION) file-trailing-space --text --ignore '$(FL_IGNORES)' --path .
@docker run --rm $$(tty -s && echo "-it" || echo) -v $(CURRENT_DIR):/data cytopia/file-lint:$(FL_VERSION) file-utf8 --text --ignore '$(FL_IGNORES)' --path .
@docker run --rm $$(tty -s && echo "-it" || echo) -v $(CURRENT_DIR):/data cytopia/file-lint:$(FL_VERSION) file-utf8-bom --text --ignore '$(FL_IGNORES)' --path .
@docker run --platform $(LINT_ARCH) --rm $$(tty -s && echo "-it" || echo) -v $(CURRENT_DIR):/data cytopia/file-lint:$(FL_VERSION) file-cr --text --ignore '$(FL_IGNORES)' --path .
@docker run --platform $(LINT_ARCH) --rm $$(tty -s && echo "-it" || echo) -v $(CURRENT_DIR):/data cytopia/file-lint:$(FL_VERSION) file-crlf --text --ignore '$(FL_IGNORES)' --path .
@docker run --platform $(LINT_ARCH) --rm $$(tty -s && echo "-it" || echo) -v $(CURRENT_DIR):/data cytopia/file-lint:$(FL_VERSION) file-trailing-single-newline --text --ignore '$(FL_IGNORES)' --path .
@docker run --platform $(LINT_ARCH) --rm $$(tty -s && echo "-it" || echo) -v $(CURRENT_DIR):/data cytopia/file-lint:$(FL_VERSION) file-trailing-space --text --ignore '$(FL_IGNORES)' --path .
@docker run --platform $(LINT_ARCH) --rm $$(tty -s && echo "-it" || echo) -v $(CURRENT_DIR):/data cytopia/file-lint:$(FL_VERSION) file-utf8 --text --ignore '$(FL_IGNORES)' --path .
@docker run --platform $(LINT_ARCH) --rm $$(tty -s && echo "-it" || echo) -v $(CURRENT_DIR):/data cytopia/file-lint:$(FL_VERSION) file-utf8-bom --text --ignore '$(FL_IGNORES)' --path .
@echo


.PHONY: lint-yaml
lint-yaml: _lint-yaml-pull
lint-yaml:
@echo "################################################################################"
@echo "# Lint Yaml"
@echo "################################################################################"
@if docker run --rm $$(tty -s && echo "-it" || echo) -v "$(CURRENT_DIR):/data:ro" -w /data cytopia/yamllint:$(YL_VERSION) .; then \
@if docker run --platform $(LINT_ARCH) --rm $$(tty -s && echo "-it" || echo) -v "$(CURRENT_DIR):/data:ro" -w /data cytopia/yamllint:$(YL_VERSION) .; then \
echo "OK"; \
else \
echo "Failed"; \
Expand All @@ -63,11 +71,12 @@ lint-yaml:


.PHONY: lint-json
lint-json: _lint-json-pull
lint-json:
@echo "################################################################################"
@echo "# Lint JSON"
@echo "################################################################################"
@if docker run --rm $$(tty -s && echo "-it" || echo) -v "$(CURRENT_DIR):/data:ro" -w /data cytopia/jsonlint:$(JL_VERSION) \
@if docker run --platform $(LINT_ARCH) --rm $$(tty -s && echo "-it" || echo) -v "$(CURRENT_DIR):/data:ro" -w /data cytopia/jsonlint:$(JL_VERSION) \
-t ' ' -i '$(JL_IGNORES)' '*.json'; then \
echo "OK"; \
else \
Expand All @@ -78,6 +87,7 @@ lint-json:


.PHONY: lint-bash
lint-bash: _lint-bash-pull
lint-bash:
@echo "################################################################################"
@echo "# Lint Bash"
Expand All @@ -89,5 +99,61 @@ lint-bash:
CMD="find . -name '*.sh' $${IGNORES} -print0"; \
echo "$${CMD}"; \
if [ "$$( eval "$${CMD}" )" != "" ]; then \
eval "$${CMD}" | xargs -0 -n1 docker run --rm -v $(CURRENT_DIR):/data -w /data koalaman/shellcheck:$(SC_VERSION) --shell=bash; \
eval "$${CMD}" | xargs -0 -n1 docker run --platform $(LINT_ARCH) --rm -v $(CURRENT_DIR):/data -w /data koalaman/shellcheck:$(SC_VERSION) --shell=bash; \
fi


# -------------------------------------------------------------------------------------------------
# Helper Targets
# -------------------------------------------------------------------------------------------------
.PHONY: _lint-files-pull
_lint-files-pull:
@echo "Pulling cytopia/file-lint:$(FL_VERSION) ($(LINT_ARCH))"; \
SUCC=0; \
for i in $$(seq 10); do \
if docker pull -q --platform $(LINT_ARCH) cytopia/file-lint:$(FL_VERSION); then \
SUCC=1; \
break; \
fi; \
sleep 1; \
done; \
if [ "$${SUCC}" = "0" ]; then echo "FAILED"; exit 1; fi;

.PHONY: _lint-yaml-pull
_lint-yaml-pull:
@echo "Pulling cytopia/yamllint:$(YL_VERSION) ($(LINT_ARCH))"; \
SUCC=0; \
for i in $$(seq 10); do \
if docker pull -q --platform $(LINT_ARCH) cytopia/yamllint:$(YL_VERSION); then \
SUCC=1; \
break; \
fi; \
sleep 1; \
done; \
if [ "$${SUCC}" = "0" ]; then echo "FAILED"; exit 1; fi;

.PHONY: _lint-json-pull
_lint-json-pull:
@echo "Pulling cytopia/jsonlint:$(JL_VERSION) ($(LINT_ARCH))"; \
SUCC=0; \
for i in $$(seq 10); do \
if docker pull -q --platform $(LINT_ARCH) cytopia/jsonlint:$(JL_VERSION); then \
SUCC=1; \
break; \
fi; \
sleep 1; \
done; \
if [ "$${SUCC}" = "0" ]; then echo "FAILED"; exit 1; fi;

.PHONY: _lint-bash-pull
_lint-bash-pull:
@echo "Pulling koalaman/shellcheck:$(SC_VERSION) ($(LINT_ARCH))"; \
SUCC=0; \
for i in $$(seq 10); do \
if docker pull -q --platform $(LINT_ARCH) koalaman/shellcheck:$(SC_VERSION); then \
SUCC=1; \
break; \
fi; \
sleep 1; \
done; \
if [ "$${SUCC}" = "0" ]; then echo "FAILED"; exit 1; fi;

0 comments on commit 1138af8

Please sign in to comment.