#!/bin/sh

if [ "$#" -lt 1 ]; then
  printf '%s\n' "e.g.: foreach go-modules go mod tidy" >&2
  exit 1
fi

cmd="$1"
shift

for dir in $(${cmd}); do
  (
    echo "${dir}"
    cd "${dir}" || exit
    # allow multiple commands like "foo && bar"
    # shellcheck disable=SC2294
    eval "$@"
  )
done
