Last active
August 4, 2025 15:44
-
-
Save jaxlo/5cade67a0361661bddfe47954e4dd126 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 | |
| WATCH_EXTENSIONS=("go" "html" "css") | |
| COLOR='\033[0;35m' # Purple (change this to change the color) | |
| NC='\033[0m' # No Color | |
| PID="" | |
| # Check if inotify-tools is installed and install if not | |
| check_inotify() { | |
| if ! command -v inotifywait &> /dev/null; then | |
| echo -e "${COLOR}inotify-tools not found. Installing...${NC}" | |
| if apt-get update && apt-get install -y inotify-tools; then | |
| echo -e "${COLOR}inotify-tools installed successfully!${NC}" | |
| else | |
| echo -e "${COLOR}Failed to install inotify-tools. This script assumes apt as a package manager. Change the script? ${NC}" | |
| exit 1 | |
| fi | |
| else | |
| echo -e "${COLOR}inotify-tools is already installed.${NC}" | |
| fi | |
| } | |
| # Kill existing Go process | |
| kill_go() { | |
| if [ ! -z "$PID" ]; then | |
| kill $PID 2>/dev/null | |
| PID="" | |
| fi | |
| } | |
| # Start Go app | |
| start_go() { | |
| kill_go | |
| echo -e "${COLOR}Building and running Go application${NC}" | |
| # Use go build (go run does not integrate changes in go:embed unless Go code changed) | |
| go build -o run-bin . | |
| if [ $? -eq 0 ]; then | |
| echo -e "${COLOR}Starting Go application${NC}" | |
| ./run-bin & | |
| PID=$! | |
| rm run-bin | |
| else | |
| echo -e "${COLOR}Build failed${NC}" | |
| fi | |
| } | |
| # Build the regex pattern from the extensions array | |
| build_pattern() { | |
| local pattern="" | |
| for ext in "${WATCH_EXTENSIONS[@]}"; do | |
| if [ -z "$pattern" ]; then | |
| pattern="\\.$ext\$" | |
| else | |
| pattern="$pattern|\\.$ext\$" | |
| fi | |
| done | |
| echo "$pattern" | |
| } | |
| # Clean on exit | |
| cleanup() { | |
| kill_go | |
| if [ -f "run-bin" ]; then | |
| rm -f run-bin # Remove the binary used | |
| echo -e "\n" # Add a newline for readability in the terminal | |
| fi | |
| exit | |
| } | |
| trap 'cleanup' INT TERM | |
| # Check if ioutils is installed and install it if it is not present | |
| check_inotify | |
| # Show what we're watching | |
| echo -e "${COLOR}Watching for changes in:${NC} ${WATCH_EXTENSIONS[*]}" | |
| echo -e "${COLOR}Press Ctrl+C to stop${NC}" | |
| # Start app initially | |
| start_go | |
| # Build the include pattern | |
| PATTERN=$(build_pattern) | |
| # Watch for file changes and restart | |
| inotifywait -m -r -e modify --include "$PATTERN" . 2>/dev/null | while read path event file; do | |
| echo -e "${COLOR}File changed: $file${NC}" | |
| echo -e "${COLOR}Restarting app...${NC}" | |
| start_go | |
| done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment