Created
May 26, 2025 08:37
-
-
Save RamLavi/dd38985b1dcb5ba16192c8b57a431ace to your computer and use it in GitHub Desktop.
Script to add linter gradually.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env bash | |
| # ----------------------------------------------------------------------------- | |
| # Linter Toggle Script for golangci-lint (.golangci.yml) | |
| # | |
| # This script automates the process of verifying each individual linter | |
| # listed under the `linters -> enable` section in a golangci-lint YAML config. | |
| # | |
| # It performs the following steps: | |
| # 1. Parses all linters listed (even commented ones) in the `enable:` section. | |
| # 2. Iteratively: | |
| # - Temporarily uncomment each linter. | |
| # - Runs `make lint` (assumes a Makefile target that executes golangci-lint). | |
| # - If linting passes, leaves the linter enabled. | |
| # - If it fails, comments the linter back. | |
| # | |
| # This is useful when expanding linter coverage and validating which linters | |
| # currently pass or fail for your codebase. | |
| # | |
| # Requirements: | |
| # - Assumes `.golangci.yml` is present and properly formatted. | |
| # - Your Makefile must contain a `lint` target. | |
| # ----------------------------------------------------------------------------- | |
| set -euo pipefail | |
| CONFIG_FILE=".golangci.yml" | |
| LINTERS=() | |
| INSIDE_LINTERS=false | |
| INSIDE_ENABLE=false | |
| # Step 1: Collect all linters (commented or not) under linters -> enable | |
| while IFS= read -r line; do | |
| if [[ "$line" =~ ^linters: ]]; then | |
| INSIDE_LINTERS=true | |
| INSIDE_ENABLE=false | |
| continue | |
| fi | |
| if $INSIDE_LINTERS && [[ "$line" =~ ^[[:space:]]*enable: ]]; then | |
| INSIDE_ENABLE=true | |
| continue | |
| fi | |
| if $INSIDE_LINTERS && $INSIDE_ENABLE && [[ "$line" =~ ^[[:space:]]*#?[[:space:]]*-[[:space:]]*([a-zA-Z0-9_-]+) ]]; then | |
| LINTER="${BASH_REMATCH[1]}" | |
| LINTERS+=("$LINTER") | |
| continue | |
| fi | |
| if [[ "$line" =~ ^[[:alpha:]] ]]; then | |
| INSIDE_LINTERS=false | |
| INSIDE_ENABLE=false | |
| fi | |
| done < "$CONFIG_FILE" | |
| # Step 2: For each linter, uncomment it, test, re-comment if it fails | |
| for LINTER in "${LINTERS[@]}"; do | |
| echo "Testing linter: $LINTER" | |
| # Uncomment the linter (preserve indentation) | |
| sed -i -E "s/^([[:space:]]*)#?[[:space:]]*- *$LINTER/\1- $LINTER/" "$CONFIG_FILE" | |
| if make lint > /dev/null 2>&1; then | |
| echo " ✓ $LINTER passed" | |
| else | |
| echo " ✗ $LINTER failed, commenting it back" | |
| sed -i -E "s/^([[:space:]]*)- *$LINTER/\1# - $LINTER/" "$CONFIG_FILE" | |
| fi | |
| done | |
| echo "Linter processing complete. Final result is in: $CONFIG_FILE" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment