Skip to content

Commit

Permalink
Add sudo option on functions with default to 'true'
Browse files Browse the repository at this point in the history
  • Loading branch information
evertramos committed Jul 8, 2021
1 parent 4d1e334 commit 73e5e18
Show file tree
Hide file tree
Showing 7 changed files with 91 additions and 26 deletions.
10 changes: 6 additions & 4 deletions common/common-create-folder.sh
Original file line number Diff line number Diff line change
Expand Up @@ -31,20 +31,22 @@

common_create_folder()
{
local LOCAL_FOLDER LOCAL_STOP_EXECUTION_ON_ERROR
local LOCAL_FOLDER LOCAL_STOP_EXECUTION_ON_ERROR LOCAL_ALLOW_RUN_WITH_SUDO LOCAL_RUN_WITH_SUDO

LOCAL_FOLDER=${1}
LOCAL_STOP_EXECUTION_ON_ERROR=${2:-true}
LOCAL_ALLOW_RUN_WITH_SUDO=${ALLOW_RUN_WITH_SUDO:-true}

[[ $LOCAL_FOLDER == "" ]] && echoerror "You must inform the required argument(s) to the function: '${FUNCNAME[0]}'"

[[ "$DEBUG" == true ]] && echo "Creating folder '$LOCAL_FOLDER'"

mkdir -p $LOCAL_FOLDER > /dev/null 2>&1
# Allows 'sudo' to run this function if destination path it's not owned by the current user
[[ "$LOCAL_ALLOW_RUN_WITH_SUDO" == true ]] && ! system_check_user_folder_owner ${LOCAL_FOLDER%/*} && LOCAL_RUN_WITH_SUDO=sudo

[[ ! -d "$LOCAL_FOLDER" ]] && sudo mkdir -p $LOCAL_FOLDER > /dev/null 2>&1
$LOCAL_RUN_WITH_SUDO mkdir -p $LOCAL_FOLDER > /dev/null 2>&1

[[ ! -d "$LOCAL_FOLDER" ]] && [[ "$LOCAL_STOP_EXECUTION_ON_ERROR" == true ]] && echoerror "The folder '$LOCAL_FOLDER' could not be created."
[[ ! -d "$LOCAL_FOLDER" ]] && [[ "$LOCAL_STOP_EXECUTION_ON_ERROR" == true ]] && echoerror "The folder '$LOCAL_FOLDER' could not be created. \nCheck folder permissions."

[[ ! -d "$LOCAL_FOLDER" ]] && COMMON_CREATE_FOLDER_ERROR = true

Expand Down
6 changes: 4 additions & 2 deletions env/env-create-if-not-exists.sh
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,11 @@ env_create_if_not_exists()

[[ $LOCAL_FULL_PATH == "" || $LOCAL_FULL_PATH == null ]] && echoerror "You must inform the required argument(s) to the function: '${FUNCNAME[0]}'"

[[ -f "$LOCAL_FULL_PATH" ]] && return 0
[[ "$DEBUG" == true ]] && echo "[env_create_if_not_exists] Checking if file '$LOCAL_ENV_FINAL_FILE' already exists."
[[ -f "$LOCAL_ENV_FINAL_FILE" ]] && return 0

[[ "$DEBUG" == true ]] && echo "Creating file '$LOCAL_FULL_PATH'"
[[ "$DEBUG" == true ]] && echo "Creating file '$LOCAL_ENV_FINAL_FILE' from '$LOCAL_ENV_EXAMPLE_FILE'."

cp $LOCAL_ENV_EXAMPLE_FILE $LOCAL_ENV_FINAL_FILE
[[ ! -f "$LOCAL_ENV_FINAL_FILE" ]] && sudo cp $LOCAL_ENV_EXAMPLE_FILE $LOCAL_ENV_FINAL_FILE
}
13 changes: 3 additions & 10 deletions env/env-update-variable.sh
Original file line number Diff line number Diff line change
Expand Up @@ -45,16 +45,9 @@ env_update_variable()

[[ $LOCAL_NEW_VALUE == "" || $LOCAL_NEW_VALUE == null ]] && echoerror "You must inform the required argument(s) to the function: '${FUNCNAME[0]}' \nReplace string: $LOCAL_VARIABLE"

[[ "$DEBUG" == true ]] && echo "Updating in '$LOCAL_ENV_FINAL_FILE' the variable '$LOCAL_VARIABLE' with value '$LOCAL_NEW_VALUE'"

[[ "$DEBUG" == true ]] && echo "[env_update_variable] Create '$LOCAL_ENV_FINAL_FILE' at '$LOCAL_FULL_PATH' if it does not exists."
[[ "$LOCAL_CREATE_IF_NOT_EXIST" == true ]] && env_create_if_not_exists $LOCAL_FULL_PATH

if [[ ! -f "$LOCAL_ENV_FINAL_FILE" ]]; then
REPONSE_ENV_UPDATE_VARIABLE="File '$LOCAL_ENV_FINAL_FILE' does not exist."
return 0
fi

sed -i '/'"$LOCAL_VARIABLE"'/c\'"$LOCAL_VARIABLE=$LOCAL_NEW_VALUE"'' $LOCAL_ENV_FINAL_FILE

return 0
[[ "$DEBUG" == true ]] && echo "[env_update_variable] Updating in '$LOCAL_ENV_FINAL_FILE' the variable '$LOCAL_VARIABLE' with value '$LOCAL_NEW_VALUE'"
file_update_file $LOCAL_ENV_FINAL_FILE $LOCAL_VARIABLE $LOCAL_NEW_VALUE true true
}
14 changes: 9 additions & 5 deletions file/file-update-file.sh
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,14 @@

file_update_file()
{
local LOCAL_FULL_FILE_PATH LOCAL_FROM_STRING LOCAL_TO_STRING LOCAL_STOP_EXECUTION_ON_ERROR LOCAL_UPDATE_VARIABLE_VALUE
local LOCAL_FULL_FILE_PATH LOCAL_FROM_STRING LOCAL_TO_STRING LOCAL_STOP_EXECUTION_ON_ERROR LOCAL_UPDATE_VARIABLE_VALUE LOCAL_ALLOW_RUN_WITH_SUDO LOCAL_RUN_WITH_SUDO

LOCAL_FULL_FILE_PATH=${1:-null}
LOCAL_FROM_STRING=${2:-null}
LOCAL_TO_STRING=${3:-null}
LOCAL_STOP_EXECUTION_ON_ERROR=${4:-false}
LOCAL_UPDATE_VARIABLE_VALUE=${5:-false}
LOCAL_ALLOW_RUN_WITH_SUDO=${ALLOW_RUN_WITH_SUDO:-true}

# Check required
[[ $LOCAL_TO_STRING == "" || $LOCAL_TO_STRING == null ]] && echoerror "You must inform the required argument(s) to the function: '${FUNCNAME[0]}'"
Expand All @@ -54,14 +55,17 @@ file_update_file()

# Debug message
[[ "$DEBUG" == true ]] && echo "Replace '$LOCAL_FROM_STRING' to '$LOCAL_TO_STRING' in '$LOCAL_FULL_FILE_PATH'."


# Allows 'sudo' to run this function if destination path it's not owned by the current user
[[ "$LOCAL_ALLOW_RUN_WITH_SUDO" == true ]] && ! system_check_user_folder_owner ${LOCAL_FULL_FILE_PATH%/*} && LOCAL_RUN_WITH_SUDO=sudo

cd $(dirname $LOCAL_FULL_FILE_PATH) >/dev/null 2>&1
if [[ "$LOCAL_UPDATE_VARIABLE_VALUE" == true ]]; then
# To remove dots (.) use the line below
# sed -i "/$LOCAL_FROM_STRING=/c\\$LOCAL_FROM_STRING=${LOCAL_TO_STRING//.}" $LOCAL_FULL_FILE_PATH
sed -i "/$LOCAL_FROM_STRING=/c\\$LOCAL_FROM_STRING=$LOCAL_TO_STRING" $LOCAL_FULL_FILE_PATH
# $LOCAL_RUN_WITH_SUDO sed -i "/$LOCAL_FROM_STRING=/c\\$LOCAL_FROM_STRING=${LOCAL_TO_STRING//.}" $LOCAL_FULL_FILE_PATH
$LOCAL_RUN_WITH_SUDO sed -i "/$LOCAL_FROM_STRING=/c\\$LOCAL_FROM_STRING=$LOCAL_TO_STRING" $LOCAL_FULL_FILE_PATH
else
sed -i -e "s/$LOCAL_FROM_STRING/$LOCAL_TO_STRING/g" $LOCAL_FULL_FILE_PATH
$LOCAL_RUN_WITH_SUDO sed -i -e "s/$LOCAL_FROM_STRING/$LOCAL_TO_STRING/g" $LOCAL_FULL_FILE_PATH
fi
cd - > /dev/null 2>&1
}
17 changes: 14 additions & 3 deletions git/git-clone-repo.sh
Original file line number Diff line number Diff line change
Expand Up @@ -53,21 +53,32 @@ git_clone_repo()
# Prepare the FULL_PATH with LOCAL_GIT_FOLDER_NAME
[[ $LOCAL_GIT_FOLDER_NAME != "" ]] && LOCAL_FULL_PATH=${LOCAL_FULL_PATH%/}"/"$LOCAL_GIT_FOLDER_NAME

[[ "$DEBUG" == true ]] && echo "Cloning repo '$LOCAL_GIT_REPO' branch '$LOCAL_REPO_BRANCH' to '$LOCAL_FULL_PATH'."

[[ "$DEBUG" == true ]] && echo "[git_clone_repo] Testing if repo '$LOCAL_GIT_REPO' on branch '$LOCAL_REPO_BRANCH' is accesible."
LOCAL_GIT_REPO_ONLINE=$(git ls-remote --exit-code --quiet ${LOCAL_GIT_REPO} --tags ${LOCAL_REPO_BRANCH})
LOCAL_GIT_REPO_ONLINE=$?
if [[ $LOCAL_GIT_REPO_ONLINE != 0 ]]; then
RESPONSE_GIT_CLONE_REPO="Git repo $LOCAL_GIT_REPO with tag ${LOCAL_REPO_BRANCH} is not reachable."
return 0
fi

mkdir -p $LOCAL_FULL_PATH
[[ "$DEBUG" == true ]] && echo "[git_clone_repo] Creating folder '$LOCAL_FULL_PATH'."
mkdir -p $LOCAL_FULL_PATH > /dev/null 2>&1
[[ ! -d "$LOCAL_FULL_PATH" ]] && sudo mkdir -p $LOCAL_FULL_PATH > /dev/null 2>&1
[[ ! -d "$LOCAL_FULL_PATH" ]] && RESPONSE_GIT_CLONE_REPO="Error creating folder '$LOCAL_FULL_PATH'" && return 0

[[ "$DEBUG" == true ]] && echo "[git_clone_repo] Cloning repo '$LOCAL_GIT_REPO' branch '$LOCAL_REPO_BRANCH' to '$LOCAL_FULL_PATH'."
if [[ "$LOCAL_FULL_REPO" == true ]]; then
git clone --branch $LOCAL_REPO_BRANCH $LOCAL_GIT_REPO $LOCAL_FULL_PATH > /dev/null 2>&1
else
git clone --depth 1 --branch $LOCAL_REPO_BRANCH $LOCAL_GIT_REPO $LOCAL_FULL_PATH > /dev/null 2>&1
fi
if [[ ! -f "${LOCAL_FULL_PATH%/}/.env" ]]; then
if [[ "$LOCAL_FULL_REPO" == true ]]; then
sudo git clone --branch $LOCAL_REPO_BRANCH $LOCAL_GIT_REPO $LOCAL_FULL_PATH > /dev/null 2>&1
else
sudo git clone --depth 1 --branch $LOCAL_REPO_BRANCH $LOCAL_GIT_REPO $LOCAL_FULL_PATH > /dev/null 2>&1
fi
fi

return 0
}
8 changes: 6 additions & 2 deletions system/run-function.sh
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,9 @@ run_function()

# Check $SILENT mode
if [[ "$SILENT" == true ]]; then
if [[ ! -z $5 ]]; then
if [[ ! -z $6 ]]; then
$1 "$2" "$3" "$4" "$5" "$6"
elif [[ ! -z $5 ]]; then
$1 "$2" "$3" "$4" "$5"
elif [[ ! -z $4 ]]; then
$1 "$2" "$3" "$4"
Expand All @@ -55,7 +57,9 @@ run_function()
else
# Call the specified function
if [[ -n "$(type -t "$1")" ]] && [[ "$(type -t "$1")" = function ]]; then
if [[ ! -z $5 ]]; then
if [[ ! -z $6 ]]; then
$1 "$2" "$3" "$4" "$5" "$6"
elif [[ ! -z $5 ]]; then
$1 "$2" "$3" "$4" "$5"
elif [[ ! -z $4 ]]; then
$1 "$2" "$3" "$4"
Expand Down
49 changes: 49 additions & 0 deletions system/system-check-user-folder-owner.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
#-----------------------------------------------------------------------
#
# Basescript function
#
# The basescript functions were designed to work as abstract function,
# so it could be used in many different contexts executing specific job
# always remembering Unix concept DOTADIW - "Do One Thing And Do It Well"
#
# Developed by
# Evert Ramos <[email protected]>
#
# Copyright Evert Ramos
#
#-----------------------------------------------------------------------
#
# Be careful when editing this file, it is part of a bigger script!
#
# Basescript - https://github.com/evertramos/basescript
#
#-----------------------------------------------------------------------

#-----------------------------------------------------------------------
# This function has one main objective:
# 1. Check if user has permission on specific folder
#
# You must/might inform the parameters below:
# 1. Full path of the folder you are checking
#
#-----------------------------------------------------------------------

system_check_user_folder_owner()
{
local LOCAL_PATH

LOCAL_PATH=${1:-null}

[[ $LOCAL_PATH == "" || $LOCAL_PATH == null ]] && echoerror "You must inform the folder path to the function: '${FUNCNAME[0]}'"

[[ "$DEBUG" == true ]] && echo "Checking permissions in path '$LOCAL_PATH'."

if [[ "$(stat -c '%u' ${LOCAL_PATH})" == "$UID" ]]; then
SYSTEM_CHECK_USER_FOLDER_OWNER=true
return 0
else
SYSTEM_CHECK_USER_FOLDER_OWNER=false
return 1
fi
}

0 comments on commit 73e5e18

Please sign in to comment.