Skip to content

Instantly share code, notes, and snippets.

@mcsf
Created March 9, 2020 10:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mcsf/62c4104b1821d1b37b61d7bafae41c5c to your computer and use it in GitHub Desktop.
Save mcsf/62c4104b1821d1b37b61d7bafae41c5c to your computer and use it in GitHub Desktop.
Given a command such as "docker-compose up", identifies the project root (the nearest ancestor containing e.g. "docker-compose.yml") and runs the command from there
#!/bin/sh
set -e
callee="$1"
invocation="$@"
target=""
case "$callee" in
docker-compose)
target=docker-compose.yml;;
make)
target=Makefile;;
npm)
target=package.json;;
esac
if [ -z "$target" ]; then
echo "Unknown command"
exit 1
fi
while [ ! -f "$target" ]; do
if [[ $(pwd) = "/" ]]; then
echo "No project file found"
exit 1
fi
cd ..
done
echo "$(pwd): $invocation"
eval $invocation
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment