#!/bin/sh

# This hook formats every Go file before committing them.
# It helps to enforce a consistent style guide for those who forget to format their code properly.

STAGED="$(git diff --cached --name-only -- '*.go')"

if [ -n "$STAGED" ]; then
	for file in $STAGED; do
		if [ ! -e "$file" ]; then
			# file doesn't exist, skip
			continue
		fi

		# format the file
		go fmt "$file"

		# run goimports if it's there
		# it organizes imports
		if command -v goimports >/dev/null 2>&1; then
			goimports -w "$file"
		fi

		git add "$file"
	done
fi