Update fix_tabs.sh

This commit is contained in:
2025-12-23 15:22:02 -06:00
parent 4fce920092
commit 465fc4a79f

View File

@@ -1,85 +1,73 @@
#!/usr/bin/env bash #!/usr/bin/env bash
# ----------------------------------------------------------------------------- # ============================================================================
# Copyright (C) 2025 Moko Consulting <hello@mokoconsulting.tech> # Copyright (C) 2025 Moko Consulting <hello@mokoconsulting.tech>
# #
# This file is part of a Moko Consulting project. # This file is part of a Moko Consulting project.
# #
# SPDX-License-Identifier: GPL-3.0-or-later # SPDX-License-Identifier: GPL-3.0-or-later
# #
# This program is free software; you can redistribute it and/or modify it under # This program is free software; you can redistribute it and/or modify
# the terms of the GNU General Public License as published by the Free Software # it under the terms of the GNU General Public License as published by
# Foundation; either version 3 of the License, or (at your option) any later # the Free Software Foundation; either version 3 of the License, or
# version. # (at your option) any later version.
# #
# This program is distributed in the hope that it will be useful, but WITHOUT # This program is distributed in the hope that it will be useful,
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS # but WITHOUT ANY WARRANTY; without even the implied warranty of
# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
# #
# You should have received a copy of the GNU General Public License (./LICENSE.md).
# -----------------------------------------------------------------------------
# FILE INFORMATION # FILE INFORMATION
# DEFGROUP: MokoStandards # DEFGROUP: MokoStandards
# INGROUP: Generic.Script # INGROUP: GitHub.Actions.Utilities
# REPO: https://github.com/mokoconsulting-tech/MokoStandards # REPO: https://github.com/mokoconsulting-tech/MokoStandards
# PATH: /scripts/fix_tabs.sh # PATH: /scripts/fix_tabs.sh
# VERSION: 03.06.00 # VERSION: 01.00.00
# BRIEF: Replace tab characters with two spaces in text files. # BRIEF: Utility script to replace tab characters with two spaces in YAML files.
# Purpose: # NOTE: Intended for local developer use. Not executed automatically in CI.
# - Replace tab characters with two spaces in text files. # ============================================================================
# - Designed for optional remediation workflows.
# - Skips binary files and version control metadata.
# - Preserves file contents aside from tab replacement.
#
# Usage:
# ./scripts/fix_tabs.sh
# ./scripts/fix_tabs.sh ./src
# =============================================================================
set -euo pipefail set -euo pipefail
ROOT_DIR="${1:-.}" log() {
printf '%s\n' "$*"
info() {
echo "INFO: $*"
} }
warn() { log "[fix_tabs] Scope: *.yml, *.yaml"
echo "WARN: $*" 1>&2 log "[fix_tabs] Action: replace tab characters with two spaces"
}
die() {
echo "ERROR: $*" 1>&2
exit 1
}
command -v find >/dev/null 2>&1 || die "find not available"
command -v sed >/dev/null 2>&1 || die "sed not available"
command -v file >/dev/null 2>&1 || die "file not available"
command -v grep >/dev/null 2>&1 || die "grep not available"
info "Scanning for tab characters under: $ROOT_DIR"
changed=0 changed=0
scanned=0
while IFS= read -r -d '' f; do # Determine file list
scanned=$((scanned + 1)) if command -v git >/dev/null 2>&1 && git rev-parse --is-inside-work-tree >/dev/null 2>&1; then
files=$(git ls-files '*.yml' '*.yaml' 2>/dev/null || true)
else
files=$(find . -type f \( -name '*.yml' -o -name '*.yaml' \) -print 2>/dev/null || true)
fi
if ! file "$f" | grep -qi "text"; then if [ -z "${files}" ]; then
continue log "[fix_tabs] No YAML files found. Nothing to fix."
fi exit 0
fi
if grep -q $'\t' "$f"; then while IFS= read -r f; do
sed -i.bak $'s/\t/ /g' "$f" && rm -f "$f.bak" [ -n "$f" ] || continue
info "Replaced tabs in $f" [ -f "$f" ] || continue
changed=$((changed + 1))
fi
done < <(
find "$ROOT_DIR" \
-type f \
-not -path "*/.git/*" \
-not -path "*/node_modules/*" \
-print0
)
info "Scan complete. Files scanned: $scanned. Files changed: $changed." if LC_ALL=C grep -q $'\t' -- "$f"; then
log "[fix_tabs] Fixing tabs in: $f"
# Replace literal tab characters with exactly two spaces
sed -i 's/\t/ /g' "$f"
changed=1
else
log "[fix_tabs] Clean: $f"
fi
done <<< "${files}"
if [ "$changed" -eq 1 ]; then
log "[fix_tabs] Completed with modifications"
else
log "[fix_tabs] No changes required"
fi