#!/usr/bin/env bash
#
# Format all TOML files in the project.
#
# Copyright 2025 Google LLC
# SPDX-License-Identifier: Apache-2.0
set -euo pipefail
TOP_DIR=$(git rev-parse --show-toplevel)
if command -v taplo >/dev/null 2>&1; then
if [ ! -f "${TOP_DIR}/taplo.toml" ]; then
echo "error: config file not found at ${TOP_DIR}/taplo.toml"
exit 1
fi
FORMATTER_COMMAND="taplo format --config ${TOP_DIR}/taplo.toml"
if command -v rust-parallel >/dev/null 2>&1; then
FORMATTER_COMMAND="rust-parallel -j4 ${FORMATTER_COMMAND}"
else
echo "warning: it is recommended to install https://crates.io/crates/rust-parallel for faster formatting"
fi
pushd "${TOP_DIR}"
if command -v fd >/dev/null 2>&1; then
echo "Using fd"
fd -e toml \
--exclude '**/*.egg-info/**' \
--exclude '**/.dist/**' \
--exclude '**/.next/**' \
--exclude '**/.output/**' \
--exclude '**/.pytest_cache/**' \
--exclude '**/.venv/**' \
--exclude '**/__pycache__/**' \
--exclude '**/bazel-*/**' \
--exclude '**/build/**' \
--exclude '**/develop-eggs/**' \
--exclude '**/dist/**' \
--exclude '**/eggs/**' \
--exclude '**/node_modules/**' \
--exclude '**/sdist/**' \
--exclude '**/site/**' \
--exclude '**/target/**' \
--exclude '**/venv/**' \
--exclude '**/wheels/**' |
${FORMATTER_COMMAND}
else
echo "Please install https://github.com/sharkdp/fd to find files to format."
fi
popd
else
echo "Please install https://github.com/tamasfe/taplo to format TOML files."
fi