Created
April 12, 2025 17:56
-
-
Save adibhanna/f5c85eeb9a708b741685f6c13a8046ad to your computer and use it in GitHub Desktop.
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
#!/bin/bash | |
function go_modernize_interactive() { | |
# Check if bat is installed | |
if ! command -v bat &>/dev/null; then | |
echo "bat is not installed. Please install it first (https://github.com/sharkdp/bat)" | |
echo "You can install it with: " | |
echo " - Homebrew: brew install bat" | |
echo " - Ubuntu/Debian: apt install bat" | |
echo " - Fedora: dnf install bat" | |
return 1 | |
fi | |
# Find all Go files | |
local go_files=$(find . -name "*.go" -not -path "*/vendor/*" | sort) | |
if [[ -z "$go_files" ]]; then | |
echo "No Go files found" | |
return 1 | |
fi | |
# Create a temporary directory for storing analysis results | |
local tmp_dir=$(mktemp -d) | |
trap "rm -rf $tmp_dir" EXIT | |
# Analyze all files to find which ones can be modernized | |
echo "Scanning for files that can be modernized..." | |
local files_with_issues=() | |
while IFS= read -r file; do | |
echo -n "." | |
local output_file="$tmp_dir/$(echo "$file" | tr '/' '_').txt" | |
go run golang.org/x/tools/gopls/internal/analysis/modernize/cmd/modernize@latest "$file" >"$output_file" 2>&1 | |
# Check if there are any issues | |
if [[ -s "$output_file" ]]; then | |
files_with_issues+=("$file") | |
fi | |
done <<<"$go_files" | |
echo "" # New line after progress dots | |
if [[ ${#files_with_issues[@]} -eq 0 ]]; then | |
echo "No modernization issues found in any files." | |
return 0 | |
fi | |
echo "Found ${#files_with_issues[@]} files that can be modernized." | |
# Create a combined preview function | |
preview_cmd="file={}; output_file=\"$tmp_dir/\$(echo \$file | tr '/' '_').txt\"; echo -e \"\033[1;33m=== Modernization Issues ===\033[0m\n\"; cat \"\$output_file\"; echo -e \"\n\033[1;33m=== File Content ===\033[0m\n\"; bat --style=numbers,changes --color=always \"\$file\"" | |
# Select files with issues to fix with full-screen preview | |
local files_to_fix=$(printf "%s\n" "${files_with_issues[@]}" | fzf --multi --preview "$preview_cmd" --preview-window=right:70%:wrap) | |
if [[ -z "$files_to_fix" ]]; then | |
return 0 | |
fi | |
# Ask if user wants to fix the selected files | |
local action=$(echo -e "fix\ncancel" | fzf --height 20% --reverse --prompt="Apply fixes to selected files? ") | |
if [[ "$action" != "fix" ]]; then | |
echo "No changes made." | |
return 0 | |
fi | |
# Apply fixes to selected files | |
echo "Applying modernization fixes to selected files..." | |
echo "$files_to_fix" | xargs go run golang.org/x/tools/gopls/internal/analysis/modernize/cmd/modernize@latest -fix | |
echo "Modernization complete!" | |
} | |
go_modernize_interactive |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment