Skip to content

Instantly share code, notes, and snippets.

@aisamanra
Created January 23, 2015 22:30
Show Gist options
  • Save aisamanra/440daf3995dc1150638b to your computer and use it in GitHub Desktop.
Save aisamanra/440daf3995dc1150638b to your computer and use it in GitHub Desktop.
Wrapper for making cabal refuse to work outside of sandboxes
#!/bin/sh
# This is a wrapper around cabal which will fail to execute if
# it's not invoked in a sandbox, although this behaviour can be
# worked around (with a warning message) using the command-line
# flag `--trust-me`.
contains_escape() {
# Find out whether the arguments contain --trust-me
ARR=$@
if [[ $ARR = ${ARR[@]//--trust-me/} ]]
then return 1
else return 0
fi
}
red_echo() {
# This will print its arguments in a bright red iff
# connected to a terminal (and otherwise won't, so
# it won't throw off cat with weird terminal escapes)
if [ -t 1 ]; then
printf "\033[91m"
fi
echo $@
if [ -t 1 ]; then
printf "\033[39m"
fi
}
if [ -e $(pwd)/cabal.sandbox.config ] || \
[ -e $CABAL_SANDBOX_CONFIG/cabal.sandbox.config ]
then
# If we're in a dir with a sandbox, or the env variable
# CABAL_SANDBOX_CONFIG exists and there's a config there,
# just exec cabal.
exec cabal $@
elif contains_escape $@
then
# If we aren't, but we've passed the --trust-me flag, then
# remove that flag from the arguments and then exec into
# cabal.
red_echo "Executing despite not being in a sandbox..."
ARGS=$@
exec cabal ${ARGS[@]//--trust-me/}
else
# Otherwise, print an appropriately ominous message and exit
# with an error.
red_echo "cabal: Not currently in a sandbox! To force execution, try"
printf " "
red_echo "cabal $@ --trust-me"
exit 2
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment