From d37b14acbad53d5d1a0ab168d683017b5bad79f4 Mon Sep 17 00:00:00 2001 From: Eugene Triguba Date: Sat, 11 Oct 2025 00:43:47 -0400 Subject: Add lint and format scripts with lint job --- scripts/format.sh | 96 ++++++++++++++++++++++++++++++++++++++++++++ scripts/lint.sh | 116 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 212 insertions(+) create mode 100755 scripts/format.sh create mode 100755 scripts/lint.sh (limited to 'scripts') diff --git a/scripts/format.sh b/scripts/format.sh new file mode 100755 index 0000000..c91254e --- /dev/null +++ b/scripts/format.sh @@ -0,0 +1,96 @@ +#!/usr/bin/env bash +set -eou pipefail + +source "$(dirname "${BASH_SOURCE[0]}")/lib/log.sh" + +usage() { + echo "Usage: $0 [FORMATTER]" + echo "" + echo "Run code formatters" + echo "" + echo "Arguments:" + echo " FORMATTER Formatter to run (gofmt, shfmt)" + echo " If not specified, runs all formatters" + echo "" + echo "Examples:" + echo " $0 gofmt # Run only gofmt" + echo " $0 # Run all formatters" +} + +main() { + local formatter="${1:-}" + if [[ $formatter == "-h" || $formatter == "--help" ]]; then + usage + exit 0 + fi + + if [[ -n "$formatter" ]]; then + case "$formatter" in + gofmt) + format_gofmt + ;; + shfmt) + format_shfmt + ;; + *) + log_error "Invalid formatter: $formatter" + log_newline + usage + exit 1 + ;; + esac + else + log_header "Running all formatters" + local failed_formatters=() + + if ! format_gofmt; then + failed_formatters+=("gofmt") + fi + + if ! format_shfmt; then + failed_formatters+=("shfmt") + fi + + if [[ ${#failed_formatters[@]} -eq 0 ]]; then + log_success "All formatters completed!" + else + log_error "Failed formatters: ${failed_formatters[*]}" + exit 1 + fi + fi +} + +format_gofmt() { + log_info "Running gofmt..." + local unformatted + unformatted=$(gofmt -l .) + if [[ -n "$unformatted" ]]; then + log_step "Formatting files with gofmt..." + gofmt -w . + log_success "gofmt completed" + return 0 + fi + log_success "All Go files already formatted" + return 0 +} + +format_shfmt() { + log_info "Running shfmt..." + if ! command -v shfmt &>/dev/null; then + log_error "shfmt is not installed" + return 1 + fi + + local unformatted + unformatted=$(shfmt -l -i 2 -ci scripts/) + if [[ -n "$unformatted" ]]; then + log_step "Formatting files with shfmt..." + shfmt -w -i 2 -ci scripts/ + log_success "shfmt completed" + return 0 + fi + log_success "All shell scripts already formatted" + return 0 +} + +main "$@" diff --git a/scripts/lint.sh b/scripts/lint.sh new file mode 100755 index 0000000..6c991e2 --- /dev/null +++ b/scripts/lint.sh @@ -0,0 +1,116 @@ +#!/usr/bin/env bash +set -eou pipefail + +source "$(dirname "${BASH_SOURCE[0]}")/lib/log.sh" + +usage() { + echo "Usage: $0 [LINTER]" + echo "" + echo "Run code linters" + echo "" + echo "Arguments:" + echo " LINTER Linter to run (gofmt, shellcheck, shfmt)" + echo " If not specified, runs all linters" + echo "" + echo "Examples:" + echo " $0 gofmt # Run only gofmt" + echo " $0 # Run all linters" +} + +main() { + local linter="${1:-}" + if [[ $linter == "-h" || $linter == "--help" ]]; then + usage + exit 0 + fi + + if [[ -n "$linter" ]]; then + case "$linter" in + gofmt) + lint_gofmt + ;; + shellcheck) + lint_shellcheck + ;; + shfmt) + lint_shfmt + ;; + *) + log_error "Invalid linter: $linter" + log_newline + usage + exit 1 + ;; + esac + else + log_header "Running all linters" + local failed_linters=() + + if ! lint_gofmt; then + failed_linters+=("gofmt") + fi + + if ! lint_shellcheck; then + failed_linters+=("shellcheck") + fi + + if ! lint_shfmt; then + failed_linters+=("shfmt") + fi + + if [[ ${#failed_linters[@]} -eq 0 ]]; then + log_success "All linters passed!" + else + log_error "Failed linters: ${failed_linters[*]}" + exit 1 + fi + fi +} + +lint_gofmt() { + log_info "Running gofmt..." + local unformatted + unformatted=$(gofmt -l .) + if [[ -n "$unformatted" ]]; then + log_error "The following files are not formatted:" + echo "$unformatted" + return 1 + fi + log_success "gofmt passed" + return 0 +} + +lint_shellcheck() { + log_info "Running shellcheck..." + if ! command -v shellcheck &>/dev/null; then + log_error "shellcheck is not installed" + return 1 + fi + + if shellcheck scripts/*.sh scripts/lib/*.sh; then + log_success "shellcheck passed" + return 0 + else + return 1 + fi +} + +lint_shfmt() { + log_info "Running shfmt..." + if ! command -v shfmt &>/dev/null; then + log_error "shfmt is not installed" + return 1 + fi + + local unformatted + unformatted=$(shfmt -l -i 2 -ci scripts/) + if [[ -n "$unformatted" ]]; then + log_error "The following files are not formatted:" + echo "$unformatted" + return 1 + fi + log_success "shfmt passed" + return 0 +} + +main "$@" -- cgit v1.2.3