wp-cli start-up script to NOT run wp-cli as root user accidentally.
#! /bin/bash
# Shamelessly addapted from
# https://gist.github.com/aydancoskun/3f14835fdf000b307b89bf7414c0e18d,
# "composer start-up script to NOT run composer as root user accidentally."
# This is wp-cli.phar start-up script to NOT run wp-cli as root user accidentally.
# This script looks at owner of the current directoy and check if the user actually exists.
# If that is affirmative it would then run wp-cli.phar as user John from the current location.
#
# Instructions:
# put the actual wp-cli.phar into /usr/local/bin so its the path and available
# put this file in the same location and just call it wp
# first section checks if its run as a none root user and if so just executes wp-cli.phar as usual
if [ $USER != "root" ]; then
wp-cli.phar "$@"
exit
fi
# from here on we are root
if [[ "$@" == *"cli update"* ]]; then
wp-cli.phar "$@"
exit
fi
# attempting to get the user, this can sometimes fail of course but we can try ;-)
WP_USER=`stat -c '%U' ./`
# check if something was found
if [[ -z "$WP_USER" ]]; then
echo Unable to get user of current directory.
exit 1
fi
# check that the something found is a valid user
ret=true
getent passwd $WP_USER >/dev/null 2>&1 && ret=false
if $ret ; then
echo Unable to get user of current directory.
echo Found \"$WP_USER\" but no passwd entry exist for this user.
exit 1
fi
# all good call composer as the user found
if [ ! -f wp-cli.phar ]; then
echo Executing:"sudo -u $WP_USER /usr/local/bin/wp-cli.phar \"$@\""
sudo -u $WP_USER /usr/local/bin/wp-cli.phar "$@"
else
echo Executing:"sudo -u $WP_USER -i -- /usr/local/bin/wp-cli.phar \"$@\""
sudo -u $WP_USER -i -- /usr/local/bin/wp-cli.phar "$@"
fi
exit